diff src/server/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 e588335b6aa8
children d58d4dd0cefe
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)