Mercurial > libervia-web
changeset 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 | ecec2cc13b33 |
children | da690ef8019e |
files | src/server/server.py src/twisted/plugins/libervia_server.py |
diffstat | 2 files changed, 57 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/src/server/server.py Sun Aug 24 18:43:45 2014 +0200 +++ b/src/server/server.py Mon Aug 25 17:35:41 2014 +0200 @@ -18,8 +18,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from twisted.application import service -from twisted.internet import glib2reactor -glib2reactor.install() from twisted.internet import reactor, defer from twisted.web import server from twisted.web.static import File @@ -59,6 +57,8 @@ from libervia.server.constants import Const as C from libervia.server.blog import MicroBlog +# following value are set from twisted.plugins.libervia_server initialise (see the comment there) +DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = coerceDataDir = None class ISATSession(Interface): profile = Attribute("Sat profile") @@ -990,48 +990,21 @@ return ("setAvatar", filepath, profile) -def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS - 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)}) - return value - - -def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS - html = os.path.join(value, C.HTML_DIR) - if not os.path.isfile(os.path.join(html, 'libervia.html')): - raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html)) - server_css = os.path.join(value, C.SERVER_CSS_DIR) - if not os.path.isfile(os.path.join(server_css, 'blog.css')): - raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(server_css)) - return value class Libervia(service.Service): - 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], - ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str], - ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], - ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], - ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], - ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], - ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html - OPT_PARAMETERS_CFG = [['empty_password_allowed_warning_dangerous_list', None, '', None]] # Options which are in sat.conf only def __init__(self, *args, **kwargs): self.initialised = defer.Deferred() # options managing - for opt in self.OPT_PARAMETERS_BOTH + self.OPT_PARAMETERS_CFG: + for opt in OPT_PARAMETERS_BOTH + OPT_PARAMETERS_CFG: opt_name = opt[0] setattr(self, opt_name, kwargs.get(opt_name, opt[2])) if not self.port_https_ext: self.port_https_ext = self.port_https - if self.data_dir == Libervia.DATA_DIR_DEFAULT: + if self.data_dir == DATA_DIR_DEFAULT: coerceDataDir(self.data_dir) # this is not done when using the default value self.html_dir = os.path.join(self.data_dir, C.HTML_DIR)
--- a/src/twisted/plugins/libervia_server.py Sun Aug 24 18:43:45 2014 +0200 +++ b/src/twisted/plugins/libervia_server.py Mon Aug 25 17:35:41 2014 +0200 @@ -28,12 +28,9 @@ except ImportError: pass -# XXX: We need to configure logs before any log method is used, so here is the best place. +import os.path + from libervia.server.constants import Const as C -from sat.core import log_config -log_config.satConfigure(C.LOG_BACKEND_TWISTED, C) - - from sat.core.i18n import _ from sat.tools.config import getConfig @@ -45,20 +42,56 @@ from twisted.application.service import IServiceMaker from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError -try: - from libervia.server.server import Libervia - opt_params = Libervia.OPT_PARAMETERS_BOTH - cfg_params = Libervia.OPT_PARAMETERS_CFG -except (ImportError, SystemExit): - # avoid raising an error when you call twisted and sat is not launched - opt_params = [] - cfg_params = [] + + +def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS + 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)}) + return value + +def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS + html = os.path.join(value, C.HTML_DIR) + if not os.path.isfile(os.path.join(html, 'libervia.html')): + raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html)) + server_css = os.path.join(value, C.SERVER_CSS_DIR) + if not os.path.isfile(os.path.join(server_css, 'blog.css')): + raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(server_css)) + 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], + ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str], + ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int], + ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int], + ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str], + ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia').encode('utf-8'), coerceDataDir], + ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html +OPT_PARAMETERS_CFG = [['empty_password_allowed_warning_dangerous_list', None, '', None]] # Options which are in sat.conf only + +def initialise(options): + """Method to initialise global modules""" + from twisted.internet import glib2reactor + glib2reactor.install() + # XXX: We need to configure logs before any log method is used, so here is the best place. + from sat.core import log_config + log_config.satConfigure(C.LOG_BACKEND_TWISTED, C, backend_data=options) + from libervia.server import server + # we can't import this file from libervia.server.server because it's not a true module + # (there is no __init__.py file, as required by twistd plugin system), so we set the + # global values from here + server.DATA_DIR_DEFAULT = DATA_DIR_DEFAULT + server.OPT_PARAMETERS_BOTH = OPT_PARAMETERS_BOTH + server.OPT_PARAMETERS_CFG = OPT_PARAMETERS_CFG + server.coerceDataDir = coerceDataDir class Options(usage.Options): - # optArgs is not really useful in our case, we need more than a flag - optParameters = opt_params + optParameters = OPT_PARAMETERS_BOTH def __init__(self): """Read SàT configuration file in order to overwrite the hard-coded default values. @@ -74,7 +107,7 @@ # FIXME: must be refactored + code can be factorised with backend config = SafeConfigParser() config.read(C.CONFIG_FILES) - for param in self.optParameters + cfg_params: + for param in self.optParameters + OPT_PARAMETERS_CFG: name = param[0] try: value = getConfig(config, 'libervia', name) @@ -91,11 +124,13 @@ implements(IServiceMaker, IPlugin) tapname = C.APP_NAME_FILE - description = _(u'The web frontend of Salut à Toi').encode('utf-8') + description = _(u'The web frontend of Salut à Toi') options = Options def makeService(self, options): - return Libervia(**dict(options)) # get rid of the usage.Option overload + initialise(options.parent) + from libervia.server import server + return server.Libervia(**dict(options)) # get rid of the usage.Option overload # affectation to some variable is necessary for twisted introspection to work