Просмотр исходного кода

Add command-line options, user configuration and daemon mode.

Guillaume Ayoub 16 лет назад
Родитель
Сommit
1b8608021f
3 измененных файлов с 43 добавлено и 13 удалено
  1. 2 2
      TODO
  2. 37 10
      radicale.py
  3. 4 1
      radicale/config.py

+ 2 - 2
TODO

@@ -15,8 +15,8 @@
 ===
 
 * SSL connections and authentications
-* Daemon mode
-* User configuration
+* [DONE] Daemon mode
+* [DONE] User configuration
 
 
 0.5

+ 37 - 10
radicale.py

@@ -19,22 +19,49 @@
 # You should have received a copy of the GNU General Public License
 # along with Radicale.  If not, see <http://www.gnu.org/licenses/>.
 
-# 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)

+ 4 - 1
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"))