diff src/twisted/plugins/libervia_server.py @ 883:74be6217d913

server (options): Q&D trick to have unicode value from command line arguments and sat.conf
author Goffi <goffi@goffi.org>
date Wed, 09 Mar 2016 20:40:27 +0100
parents 6bdee34fa2f4
children fd4eae654182
line wrap: on
line diff
--- a/src/twisted/plugins/libervia_server.py	Wed Mar 09 20:40:25 2016 +0100
+++ b/src/twisted/plugins/libervia_server.py	Wed Mar 09 20:40:27 2016 +0100
@@ -70,10 +70,6 @@
 
 
 def coerceConnectionType(value):  # called from Libervia.OPT_PARAMETERS
-    if isinstance(value, unicode):
-        # XXX: if value comes from sat.conf, it's unicode,
-        # and we need byte str here (for twisted)
-        value = value.encode('utf-8')
     allowed_values = ('http', 'https', 'both')
     if value not in allowed_values:
         raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)})
@@ -96,20 +92,25 @@
 def coerceBool(value):
     return C.bool(value)
 
+def coerceUnicode(value):
+    # XXX: we use this method to check which value to convert to Unicode
+    #      but we don't do the conversion here as Twisted expect str
+    return value
+
 DATA_DIR_DEFAULT = ''
 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType],
                        ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int],
                        ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int],
                        ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int],
-                       ['tls_private_key', '', '', _(u'TLS certificate private key (PEM format)').encode('utf-8'), str],
-                       ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), str],
-                       ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM format)').encode('utf-8'), str],
+                       ['tls_private_key', '', '', _(u'TLS certificate private key (PEM format)').encode('utf-8'), coerceUnicode],
+                       ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), coerceUnicode],
+                       ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM format)').encode('utf-8'), coerceUnicode],
                        ['redirect_to_https', 'r', True, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), coerceBool],
                        ['security_warning', 'w', True, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), coerceBool],
-                       ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str],
+                       ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), coerceUnicode],
                        ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir],
                        ['allow_registration', '', True, _(u'Allow user to register new account').encode('utf-8'), coerceBool],
-                       ['base_url_ext', '', '', _(u'The external URL to use as base URL').encode('utf-8'), str],
+                       ['base_url_ext', '', '', _(u'The external URL to use as base URL').encode('utf-8'), coerceUnicode],
                       ]  # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
 # Options which are in sat.conf only
 OPT_PARAMETERS_CFG = [
@@ -158,6 +159,8 @@
             name = param[0]
             try:
                 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception)
+                if isinstance(value, unicode):
+                    value = value.encode('utf-8')
                 try:
                     param[2] = param[4](value)
                 except IndexError: # the coerce method is optional
@@ -192,6 +195,16 @@
     options = Options
 
     def makeService(self, options):
+        for opt in OPT_PARAMETERS_BOTH:
+            # FIXME: that's a ugly way to get unicode in Libervia
+            #        from command line or sat.conf
+            #        we should move to argparse and handle options this properly
+            try:
+                coerce_cb = opt[4]
+            except IndexError:
+                continue
+            if coerce_cb == coerceUnicode:
+                options[opt[0]] = options[opt[0]].decode('utf-8')
         initialise(options.parent)
         from libervia.server import server
         return server.Libervia(options)