Mercurial > libervia-web
annotate src/twisted/plugins/libervia_server.py @ 749:7168a9873dde
server_side (blog): fixes avatar callback
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 23 Nov 2015 22:02:18 +0100 |
parents | bf562fb9c273 |
children | 3905bc24eb17 |
rev | line source |
---|---|
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 #!/usr/bin/python |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
4 # Libervia: a Salut à Toi frontend |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
5 # Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
10 # (at your option) any later version. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
15 # GNU Affero General Public License for more details. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
20 from twisted.internet import defer |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
21 if defer.Deferred.debug: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
22 # if we are in debug mode, we want to use ipdb instead of pdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
23 try: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
24 import ipdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
25 import pdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
26 pdb.set_trace = ipdb.set_trace |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
27 pdb.post_mortem = ipdb.post_mortem |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
28 except ImportError: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
29 pass |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
30 |
514 | 31 import os.path |
32 | |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
33 from libervia.server.constants import Const as C |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
34 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
35 from sat.core.i18n import _ |
466
01880aa8ea2d
twisted plugin: fixes the parsing of directory parameter
souliane <souliane@mailoo.org>
parents:
451
diff
changeset
|
36 from sat.tools.config import getConfig |
438
582c435dab6b
server side: new log system is used
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
37 |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 from zope.interface import implements |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 from twisted.python import usage |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 from twisted.plugin import IPlugin |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 from twisted.application.service import IServiceMaker |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
44 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError |
514 | 45 |
46 | |
47 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | |
48 allowed_values = ('http', 'https', 'both') | |
49 if value not in allowed_values: | |
50 raise ValueError("%(given)s not in %(expected)s" % {'given': value, 'expected': str(allowed_values)}) | |
51 return value | |
52 | |
53 def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS | |
54 html = os.path.join(value, C.HTML_DIR) | |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
55 if not os.path.isfile(os.path.join(html, C.LIBERVIA_MAIN_PAGE)): |
514 | 56 raise ValueError("%s is not a Libervia's browser HTML directory" % os.path.realpath(html)) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
57 themes_dir = os.path.join(value, C.THEMES_DIR) |
712
bf562fb9c273
server_side: use Jinja2 template engine for static blog
souliane <souliane@mailoo.org>
parents:
704
diff
changeset
|
58 if not os.path.isfile(os.path.join(themes_dir, 'default/styles/blog.css')): |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
59 raise ValueError("%s is not a Libervia's server data directory" % os.path.realpath(themes_dir)) |
514 | 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 | |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 class Options(usage.Options): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
93 # optArgs is not really useful in our case, we need more than a flag |
514 | 94 optParameters = OPT_PARAMETERS_BOTH |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
95 |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
96 def __init__(self): |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
97 """Read SàT configuration file in order to overwrite the hard-coded default values. |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
98 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
99 Priority for the usage of the values is (from lowest to highest): |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
100 - hard-coded default values |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
101 - values from SàT configuration files |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
102 - values passed on the command line |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
103 """ |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
104 # If we do it the reading later: after the command line options have been parsed, there's no good way to know |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
105 # if the options values are the hard-coded ones or if they have been passed on the command line. |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
106 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
107 # FIXME: must be refactored + code can be factorised with backend |
421
39b07289ff42
server_side: added parameter port_https_ext (external port for HTTPS, used for example for the redirection)
souliane <souliane@mailoo.org>
parents:
417
diff
changeset
|
108 config = SafeConfigParser() |
433
bbdbee25123a
import constants.Const as C (according to the coding rules)
souliane <souliane@mailoo.org>
parents:
432
diff
changeset
|
109 config.read(C.CONFIG_FILES) |
514 | 110 for param in self.optParameters + OPT_PARAMETERS_CFG: |
432
8ecc5a7062e4
browser and server sides: fixes module import + use enumerate instead of xrange
souliane <souliane@mailoo.org>
parents:
421
diff
changeset
|
111 name = param[0] |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
112 try: |
570
2a4e071633f7
server side (twisted plugin): fixed bad use of default value in getConfig
Goffi <goffi@goffi.org>
parents:
514
diff
changeset
|
113 value = getConfig(config, 'libervia', name, Exception) |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
114 try: |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
115 param[2] = param[4](value) |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
116 except IndexError: # the coerce method is optional |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
117 param[2] = value |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
118 except (NoSectionError, NoOptionError): |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
119 pass |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
120 usage.Options.__init__(self) |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
122 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 class LiberviaMaker(object): |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 implements(IServiceMaker, IPlugin) |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
125 |
512
750db9ff8525
server side: launching script improvments:
Goffi <goffi@goffi.org>
parents:
474
diff
changeset
|
126 tapname = C.APP_NAME_FILE |
514 | 127 description = _(u'The web frontend of Salut à Toi') |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 options = Options |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 def makeService(self, options): |
514 | 131 initialise(options.parent) |
132 from libervia.server import server | |
133 return server.Libervia(**dict(options)) # get rid of the usage.Option overload | |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 |
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 |
421
39b07289ff42
server_side: added parameter port_https_ext (external port for HTTPS, used for example for the redirection)
souliane <souliane@mailoo.org>
parents:
417
diff
changeset
|
136 # affectation to some variable is necessary for twisted introspection to work |
331
06a48d805547
server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
137 serviceMaker = LiberviaMaker() |