From 1b8608021f3933d79fabb2a219818a6e19157912 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Mon, 18 Jan 2010 10:48:06 +0100 Subject: [PATCH] Add command-line options, user configuration and daemon mode. --- TODO | 4 ++-- radicale.py | 47 ++++++++++++++++++++++++++++++++++++---------- radicale/config.py | 5 ++++- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 33e072a..9a3564c 100644 --- a/TODO +++ b/TODO @@ -15,8 +15,8 @@ === * SSL connections and authentications -* Daemon mode -* User configuration +* [DONE] Daemon mode +* [DONE] User configuration 0.5 diff --git a/radicale.py b/radicale.py index 406c58d..1be60c5 100755 --- a/radicale.py +++ b/radicale.py @@ -19,22 +19,49 @@ # You should have received a copy of the GNU General Public License # along with Radicale. If not, see . -# TODO: Manage depth and calendars/collections (see xmlutils) -# TODO: Manage smart and configurable logs -# TODO: Manage authentication -# TODO: Magage command-line options - """ Radicale Server entry point. -Launch the Radicale Server according to the configuration. +Launch the Radicale Serve according to configuration and command-line +arguments. """ +# TODO: Manage depth and calendars/collections (see xmlutils) +# TODO: Manage smart and configurable logs +# TODO: Manage authentication + +import os +import sys +import optparse + import radicale -if radicale.config.get("server", "protocol") == "http": +parser = optparse.OptionParser() +parser.add_option( + "-d", "--daemon", action="store_true", + default=radicale.config.getboolean("server", "daemon"), + help="launch as daemon") +parser.add_option( + "-n", "--name", + default=radicale.config.get("server", "name"), + help="set server name") +parser.add_option( + "-p", "--port", + default=radicale.config.getint("server", "port"), + help="set server port") +parser.add_option( + "-P", "--protocol", + default=radicale.config.get("server", "protocol"), + help="set server protocol") +options, args = parser.parse_args() + +if options.daemon: + if os.fork(): + sys.exit() + sys.stdout = sys.stderr = open(os.devnull, "w") +if options.protocol == "http": server = radicale.server.HTTPServer( - (radicale.config.get("server", "name"), - radicale.config.getint("server", "port")), - radicale.CalendarHandler) + (options.name, options.port), radicale.CalendarHandler) server.serve_forever() +else: + raise StandardError("%s: unsupported protocol" % options.protocol) diff --git a/radicale/config.py b/radicale/config.py index c54faae..b2cb452 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -26,6 +26,7 @@ Give a configparser-like interface to read and write configuration. # TODO: Use abstract filenames for other platforms +import os try: from configparser import RawConfigParser as ConfigParser except ImportError: @@ -45,6 +46,7 @@ _initial = { "protocol": "http", "name": "", "port": "5232", + "daemon": "False", #"certificate": "/etc/apache2/ssl/server.crt", #"privatekey": "/etc/apache2/ssl/server.key", #"log": "/var/www/radicale/server.log", @@ -64,7 +66,7 @@ _initial = { }, "support": { "type": "plain", - "folder": "~/.config/radicale", + "folder": os.path.expanduser("~/.config/radicale"), "calendar": "radicale/calendar", }, } @@ -75,3 +77,4 @@ for section, values in _initial.items(): _config.set(section, key, value) _config.read("/etc/radicale/config") +_config.read(os.path.expanduser("~/.config/radicale/config"))