Mercurial > libervia-web
comparison src/twisted/plugins/libervia_server.py @ 514:530c88c1deee
server_side: plugin refactoring:
twisted.plugins.libervia_server is refactored for the same reason as for SàT backend (see SàT commit message for revision adea30ca0b51), and there is an additionnal trick: as we need to use some variables in both twisted.plugins.libervia_server and server.server, we can't import server.server before initialise() is called, and we can't neigher import twisted.plugins.libervia_server from server.server (there is no __init__.py file as requested by Twisted plugin systeme), these variable have been moved from server.server to twisted.plugins.libervia_server, and are set in server.server from the latter. This not super clean to read, but it solve the import order issues.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 25 Aug 2014 17:35:41 +0200 |
parents | 750db9ff8525 |
children | 2a4e071633f7 |
comparison
equal
deleted
inserted
replaced
513:ecec2cc13b33 | 514:530c88c1deee |
---|---|
26 pdb.set_trace = ipdb.set_trace | 26 pdb.set_trace = ipdb.set_trace |
27 pdb.post_mortem = ipdb.post_mortem | 27 pdb.post_mortem = ipdb.post_mortem |
28 except ImportError: | 28 except ImportError: |
29 pass | 29 pass |
30 | 30 |
31 # XXX: We need to configure logs before any log method is used, so here is the best place. | 31 import os.path |
32 | |
32 from libervia.server.constants import Const as C | 33 from libervia.server.constants import Const as C |
33 from sat.core import log_config | |
34 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C) | |
35 | |
36 | |
37 | 34 |
38 from sat.core.i18n import _ | 35 from sat.core.i18n import _ |
39 from sat.tools.config import getConfig | 36 from sat.tools.config import getConfig |
40 | 37 |
41 from zope.interface import implements | 38 from zope.interface import implements |
43 from twisted.python import usage | 40 from twisted.python import usage |
44 from twisted.plugin import IPlugin | 41 from twisted.plugin import IPlugin |
45 from twisted.application.service import IServiceMaker | 42 from twisted.application.service import IServiceMaker |
46 | 43 |
47 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError | 44 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError |
48 try: | 45 |
49 from libervia.server.server import Libervia | 46 |
50 opt_params = Libervia.OPT_PARAMETERS_BOTH | 47 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS |
51 cfg_params = Libervia.OPT_PARAMETERS_CFG | 48 allowed_values = ('http', 'https', 'both') |
52 except (ImportError, SystemExit): | 49 if value not in allowed_values: |
53 # avoid raising an error when you call twisted and sat is not launched | 50 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)}) |
54 opt_params = [] | 51 return value |
55 cfg_params = [] | 52 |
53 def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS | |
54 html = os.path.join(value, C.HTML_DIR) | |
55 if not os.path.isfile(os.path.join(html, 'libervia.html')): | |
56 raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html)) | |
57 server_css = os.path.join(value, C.SERVER_CSS_DIR) | |
58 if not os.path.isfile(os.path.join(server_css, 'blog.css')): | |
59 raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(server_css)) | |
60 return value | |
61 | |
62 DATA_DIR_DEFAULT = '' | |
63 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers).").encode('utf-8'), coerceConnectionType], | |
64 ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int], | |
65 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int], | |
66 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int], | |
67 ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str], | |
68 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], | |
69 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], | |
70 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], | |
71 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], | |
72 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html | |
73 OPT_PARAMETERS_CFG = [['empty_password_allowed_warning_dangerous_list', None, '', None]] # Options which are in sat.conf only | |
74 | |
75 def initialise(options): | |
76 """Method to initialise global modules""" | |
77 from twisted.internet import glib2reactor | |
78 glib2reactor.install() | |
79 # XXX: We need to configure logs before any log method is used, so here is the best place. | |
80 from sat.core import log_config | |
81 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C, backend_data=options) | |
82 from libervia.server import server | |
83 # we can't import this file from libervia.server.server because it's not a true module | |
84 # (there is no __init__.py file, as required by twistd plugin system), so we set the | |
85 # global values from here | |
86 server.DATA_DIR_DEFAULT = DATA_DIR_DEFAULT | |
87 server.OPT_PARAMETERS_BOTH = OPT_PARAMETERS_BOTH | |
88 server.OPT_PARAMETERS_CFG = OPT_PARAMETERS_CFG | |
89 server.coerceDataDir = coerceDataDir | |
56 | 90 |
57 | 91 |
58 class Options(usage.Options): | 92 class Options(usage.Options): |
59 | |
60 # optArgs is not really useful in our case, we need more than a flag | 93 # optArgs is not really useful in our case, we need more than a flag |
61 optParameters = opt_params | 94 optParameters = OPT_PARAMETERS_BOTH |
62 | 95 |
63 def __init__(self): | 96 def __init__(self): |
64 """Read SàT configuration file in order to overwrite the hard-coded default values. | 97 """Read SàT configuration file in order to overwrite the hard-coded default values. |
65 | 98 |
66 Priority for the usage of the values is (from lowest to highest): | 99 Priority for the usage of the values is (from lowest to highest): |
72 # if the options values are the hard-coded ones or if they have been passed on the command line. | 105 # if the options values are the hard-coded ones or if they have been passed on the command line. |
73 | 106 |
74 # FIXME: must be refactored + code can be factorised with backend | 107 # FIXME: must be refactored + code can be factorised with backend |
75 config = SafeConfigParser() | 108 config = SafeConfigParser() |
76 config.read(C.CONFIG_FILES) | 109 config.read(C.CONFIG_FILES) |
77 for param in self.optParameters + cfg_params: | 110 for param in self.optParameters + OPT_PARAMETERS_CFG: |
78 name = param[0] | 111 name = param[0] |
79 try: | 112 try: |
80 value = getConfig(config, 'libervia', name) | 113 value = getConfig(config, 'libervia', name) |
81 try: | 114 try: |
82 param[2] = param[4](value) | 115 param[2] = param[4](value) |
89 | 122 |
90 class LiberviaMaker(object): | 123 class LiberviaMaker(object): |
91 implements(IServiceMaker, IPlugin) | 124 implements(IServiceMaker, IPlugin) |
92 | 125 |
93 tapname = C.APP_NAME_FILE | 126 tapname = C.APP_NAME_FILE |
94 description = _(u'The web frontend of Salut à Toi').encode('utf-8') | 127 description = _(u'The web frontend of Salut à Toi') |
95 options = Options | 128 options = Options |
96 | 129 |
97 def makeService(self, options): | 130 def makeService(self, options): |
98 return Libervia(**dict(options)) # get rid of the usage.Option overload | 131 initialise(options.parent) |
132 from libervia.server import server | |
133 return server.Libervia(**dict(options)) # get rid of the usage.Option overload | |
99 | 134 |
100 | 135 |
101 # affectation to some variable is necessary for twisted introspection to work | 136 # affectation to some variable is necessary for twisted introspection to work |
102 serviceMaker = LiberviaMaker() | 137 serviceMaker = LiberviaMaker() |