diff twisted/plugins/libervia.py @ 415:fadbba1d793f

server_side: added support for SSL and related parameters: Full parameters list: -t, --connection_type= 'http', 'https' or 'both' (to launch both servers). [default: both] -p, --port= The port number to listen HTTP on. [default: 8080] -s, --port_https= The port number to listen HTTPS on. [default: 8443] -c, --ssl_certificate= PEM certificate with both private and public parts. [default: libervia.pem] -r, --redirect_to_https= automatically redirect from HTTP to HTTPS. [default: 0] -w, --security_warning= warn user that he is about to connect on HTTP. [default: 1]
author souliane <souliane@mailoo.org>
date Tue, 18 Mar 2014 15:59:38 +0100
parents 9834136b15ed
children e9bc7854bce6
line wrap: on
line diff
--- a/twisted/plugins/libervia.py	Fri Mar 21 09:20:27 2014 +0100
+++ b/twisted/plugins/libervia.py	Tue Mar 18 15:59:38 2014 +0100
@@ -22,38 +22,51 @@
 from twisted.python import usage
 from twisted.plugin import IPlugin
 from twisted.application.service import IServiceMaker
-from twisted.application import internet
 
 from xdg.BaseDirectory import save_config_path
-from ConfigParser import SafeConfigParser, NoSectionError
+from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
 from os.path import expanduser
 
 try:
     from libervia_server import Libervia
+    opt_params = Libervia.OPT_PARAMETERS
 except (ImportError, SystemExit):
-    pass  # avoid raising an error when you call twisted and sat is not launched
+    # avoid raising an error when you call twisted and sat is not launched
+    opt_params = []
 
 
 class Options(usage.Options):
-    optParameters = [['port', 'p', 8080, 'The port number to listen on.']]
+
+    # optArgs is not really useful in our case, we need more than a flag
+    optParameters = opt_params
+
+    def __init__(self):
+        """You want to read SàT configuration file now in order to overwrite the hard-coded default values.
+        This is because the priority is (form lowest to highest):
+        - use hard-coded default values
+        - use the values from SàT configuration files
+        - use the values passes on the command line
+        If you do it later: after the command line options have been parsed, there's no good way to know
+        if the  options values are the hard-coded ones or if they have been passed on the command line.
+        """
+        for index in xrange(0, len(self.optParameters)):
+            name = self.optParameters[index][0]
+            try:
+                value = config.get('libervia', name)
+                self.optParameters[index][2] = self.optParameters[index][4](value)
+            except (NoSectionError, NoOptionError):
+                pass
+        usage.Options.__init__(self)
 
 
 class LiberviaMaker(object):
     implements(IServiceMaker, IPlugin)
     tapname = 'libervia'
-    description = 'The web frontend of Salut à Toi'
+    description = u'The web frontend of Salut à Toi'
     options = Options
 
     def makeService(self, options):
-        if not isinstance(options['port'], int):
-            port = int(options['port'])
-        else:
-            try:
-                port = config.getint('libervia', 'port')
-            except NoSectionError:
-                port = 8080
-        return Libervia(port=port)
-
+        return Libervia(dict(options))  # get rid of the usage.Option surcharge
 
 config_path = save_config_path('sat')
 config = SafeConfigParser()