Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
882:d3d2b97aa12c | 883:74be6217d913 |
---|---|
68 import os | 68 import os |
69 os._exit(1) | 69 os._exit(1) |
70 | 70 |
71 | 71 |
72 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | 72 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS |
73 if isinstance(value, unicode): | |
74 # XXX: if value comes from sat.conf, it's unicode, | |
75 # and we need byte str here (for twisted) | |
76 value = value.encode('utf-8') | |
77 allowed_values = ('http', 'https', 'both') | 73 allowed_values = ('http', 'https', 'both') |
78 if value not in allowed_values: | 74 if value not in allowed_values: |
79 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)}) | 75 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)}) |
80 return value | 76 return value |
81 | 77 |
94 return value | 90 return value |
95 | 91 |
96 def coerceBool(value): | 92 def coerceBool(value): |
97 return C.bool(value) | 93 return C.bool(value) |
98 | 94 |
95 def coerceUnicode(value): | |
96 # XXX: we use this method to check which value to convert to Unicode | |
97 # but we don't do the conversion here as Twisted expect str | |
98 return value | |
99 | |
99 DATA_DIR_DEFAULT = '' | 100 DATA_DIR_DEFAULT = '' |
100 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], | 101 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], |
101 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], | 102 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], |
102 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], | 103 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], |
103 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], | 104 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], |
104 ['tls_private_key', '', '', _(u'TLS certificate private key (PEM format)').encode('utf-8'), str], | 105 ['tls_private_key', '', '', _(u'TLS certificate private key (PEM format)').encode('utf-8'), coerceUnicode], |
105 ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), str], | 106 ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), coerceUnicode], |
106 ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM format)').encode('utf-8'), str], | 107 ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM format)').encode('utf-8'), coerceUnicode], |
107 ['redirect_to_https', 'r', True, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), coerceBool], | 108 ['redirect_to_https', 'r', True, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), coerceBool], |
108 ['security_warning', 'w', True, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), coerceBool], | 109 ['security_warning', 'w', True, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), coerceBool], |
109 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], | 110 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), coerceUnicode], |
110 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], | 111 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], |
111 ['allow_registration', '', True, _(u'Allow user to register new account').encode('utf-8'), coerceBool], | 112 ['allow_registration', '', True, _(u'Allow user to register new account').encode('utf-8'), coerceBool], |
112 ['base_url_ext', '', '', _(u'The external URL to use as base URL').encode('utf-8'), str], | 113 ['base_url_ext', '', '', _(u'The external URL to use as base URL').encode('utf-8'), coerceUnicode], |
113 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html | 114 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html |
114 # Options which are in sat.conf only | 115 # Options which are in sat.conf only |
115 OPT_PARAMETERS_CFG = [ | 116 OPT_PARAMETERS_CFG = [ |
116 ['empty_password_allowed_warning_dangerous_list', None, '', None], | 117 ['empty_password_allowed_warning_dangerous_list', None, '', None], |
117 ['url_redirections_profile', None, '', None], | 118 ['url_redirections_profile', None, '', None], |
156 self.handleDeprecated(config_parser) | 157 self.handleDeprecated(config_parser) |
157 for param in self.optParameters + OPT_PARAMETERS_CFG: | 158 for param in self.optParameters + OPT_PARAMETERS_CFG: |
158 name = param[0] | 159 name = param[0] |
159 try: | 160 try: |
160 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception) | 161 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception) |
162 if isinstance(value, unicode): | |
163 value = value.encode('utf-8') | |
161 try: | 164 try: |
162 param[2] = param[4](value) | 165 param[2] = param[4](value) |
163 except IndexError: # the coerce method is optional | 166 except IndexError: # the coerce method is optional |
164 param[2] = value | 167 param[2] = value |
165 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): | 168 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
190 tapname = C.APP_NAME_FILE | 193 tapname = C.APP_NAME_FILE |
191 description = _(u'The web frontend of Salut à Toi') | 194 description = _(u'The web frontend of Salut à Toi') |
192 options = Options | 195 options = Options |
193 | 196 |
194 def makeService(self, options): | 197 def makeService(self, options): |
198 for opt in OPT_PARAMETERS_BOTH: | |
199 # FIXME: that's a ugly way to get unicode in Libervia | |
200 # from command line or sat.conf | |
201 # we should move to argparse and handle options this properly | |
202 try: | |
203 coerce_cb = opt[4] | |
204 except IndexError: | |
205 continue | |
206 if coerce_cb == coerceUnicode: | |
207 options[opt[0]] = options[opt[0]].decode('utf-8') | |
195 initialise(options.parent) | 208 initialise(options.parent) |
196 from libervia.server import server | 209 from libervia.server import server |
197 return server.Libervia(options) | 210 return server.Libervia(options) |
198 | 211 |
199 | 212 |