Mercurial > libervia-web
annotate twisted/plugins/libervia_server.py @ 1132:0cafb79ced6d
server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 18 Sep 2018 21:16:53 +0200 |
parents | 6414fd795df4 |
children | 4426c328eb83 |
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 |
1054 | 5 # Copyright (C) 2013-2018 Jérôme Poisson <goffi@goffi.org> |
818 | 6 # Copyright (C) 2013-2016 Adrien Cossa <souliane@mailoo.org> |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
7 # 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
|
8 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
9 # 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
|
10 # 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
|
11 # 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
|
12 # (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
|
13 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
14 # 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
|
15 # 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
|
16 # 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
|
17 # 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
|
18 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
19 # 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
|
20 # 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
|
21 |
1125
56ace2d45783
server (twisted): install reactor before doing anything else
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
22 from twisted.internet import gireactor |
56ace2d45783
server (twisted): install reactor before doing anything else
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
23 gireactor.install() |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
24 from twisted.internet import defer |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
25 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
26 if defer.Deferred.debug: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
27 # 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
|
28 try: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
29 import ipdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
30 import pdb |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
31 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
32 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
|
33 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
|
34 except ImportError: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
35 pass |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
36 |
514 | 37 import os.path |
856 | 38 import libervia |
39 import sat | |
514 | 40 |
449
981ed669d3b3
/!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents:
446
diff
changeset
|
41 from libervia.server.constants import Const as C |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
42 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
43 from sat.core.i18n import _ |
811 | 44 from sat.tools import config |
438
582c435dab6b
server side: new log system is used
Goffi <goffi@goffi.org>
parents:
437
diff
changeset
|
45 |
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
|
46 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
|
47 |
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
|
48 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
|
49 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
|
50 from twisted.application.service import IServiceMaker |
811 | 51 import ConfigParser |
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
|
52 |
811 | 53 |
54 CONFIG_SECTION = C.APP_NAME.lower() | |
856 | 55 if libervia.__version__ != sat.__version__: |
56 import sys | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
57 |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
58 sys.stderr.write( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
59 u"""sat module version ({sat_version}) and {current_app} version ({current_version}) mismatch |
856 | 60 |
61 sat module is located at {sat_path} | |
62 libervia module is located at {libervia_path} | |
63 | |
64 Please be sure to have the same version running | |
65 """.format( | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
66 sat_version=sat.__version__, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
67 current_app=C.APP_NAME, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
68 current_version=libervia.__version__, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
69 sat_path=os.path.dirname(sat.__file__), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
70 libervia_path=os.path.dirname(libervia.__file__), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
71 ).encode( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
72 "utf-8" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
73 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
74 ) |
856 | 75 sys.stderr.flush() |
76 # we call os._exit to avoid help to be printed by twisted | |
77 import os | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
78 |
856 | 79 os._exit(1) |
514 | 80 |
81 | |
82 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
83 allowed_values = ("http", "https", "both") |
514 | 84 if value not in allowed_values: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
85 raise ValueError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
86 "%(given)s not in %(expected)s" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
87 % {"given": value, "expected": str(allowed_values)} |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
88 ) |
514 | 89 return value |
90 | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
91 |
514 | 92 def coerceDataDir(value): # called from Libervia.OPT_PARAMETERS |
1124
28e3eb3bb217
files reorganisation and installation rework:
Goffi <goffi@goffi.org>
parents:
1117
diff
changeset
|
93 if not value: |
28e3eb3bb217
files reorganisation and installation rework:
Goffi <goffi@goffi.org>
parents:
1117
diff
changeset
|
94 # we ignore missing values |
28e3eb3bb217
files reorganisation and installation rework:
Goffi <goffi@goffi.org>
parents:
1117
diff
changeset
|
95 return u'' |
863
20fbca3c949a
server: fixed bad encoding handling in options parsing
Goffi <goffi@goffi.org>
parents:
857
diff
changeset
|
96 if isinstance(value, unicode): |
20fbca3c949a
server: fixed bad encoding handling in options parsing
Goffi <goffi@goffi.org>
parents:
857
diff
changeset
|
97 # XXX: if value comes from sat.conf, it's unicode, |
20fbca3c949a
server: fixed bad encoding handling in options parsing
Goffi <goffi@goffi.org>
parents:
857
diff
changeset
|
98 # and we need byte str here (for twisted) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
99 value = value.encode("utf-8") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
100 value = value.encode("utf-8") |
514 | 101 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
|
102 if not os.path.isfile(os.path.join(html, C.LIBERVIA_MAIN_PAGE)): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
103 raise ValueError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
104 "%s is not a Libervia's browser HTML directory" % os.path.realpath(html) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
105 ) |
704
5319110a862c
server_side: static blog uses the default template
souliane <souliane@mailoo.org>
parents:
570
diff
changeset
|
106 themes_dir = os.path.join(value, C.THEMES_DIR) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
107 if not os.path.isfile(os.path.join(themes_dir, "default/styles/blog.css")): |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
108 raise ValueError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
109 "%s is not a Libervia's server data directory" % os.path.realpath(themes_dir) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
110 ) |
514 | 111 return value |
112 | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
113 |
870
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
114 def coerceBool(value): |
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
115 return C.bool(value) |
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
116 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
117 |
883
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
118 def coerceUnicode(value): |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
119 # XXX: we use this method to check which value to convert to Unicode |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
120 # but we don't do the conversion here as Twisted expect str |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
121 return value |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
122 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
123 |
514 | 124 DATA_DIR_DEFAULT = '' |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
125 # options which are in sat.conf and on command line, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
126 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
127 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' " |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
128 "(to launch both servers).").encode('utf-8'), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
129 coerceConnectionType], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
130 ['port', 'p', 8080, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
131 _(u'The port number to listen HTTP on.').encode('utf-8'), int], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
132 ['port_https', 's', 8443, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
133 _(u'The port number to listen HTTPS on.').encode('utf-8'), int], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
134 ['port_https_ext', 'e', 0, _(u'The external port number used for ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
135 u'HTTPS (0 means port_https value).').encode('utf-8'), int], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
136 ['tls_private_key', '', '', _(u'TLS certificate private key (PEM ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
137 u'format)').encode('utf-8'), coerceUnicode], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
138 ['tls_certificate', 'c', 'libervia.pem', _(u'TLS public ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
139 u'certificate or private key and public certificate combined ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
140 u'(PEM format)').encode('utf-8'), coerceUnicode], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
141 ['tls_chain', '', '', _(u'TLS certificate intermediate chain (PEM ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
142 u'format)').encode('utf-8'), coerceUnicode], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
143 ['redirect_to_https', 'r', True, _(u'Automatically redirect from ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
144 u'HTTP to HTTPS.').encode('utf-8'), coerceBool], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
145 ['security_warning', 'w', True, _(u'Warn user that he is about to ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
146 u'connect on HTTP.').encode('utf-8'), coerceBool], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
147 ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile " |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
148 u"named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
149 coerceUnicode], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
150 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for ' |
1124
28e3eb3bb217
files reorganisation and installation rework:
Goffi <goffi@goffi.org>
parents:
1117
diff
changeset
|
151 u'Libervia legacy').encode('utf-8'), coerceDataDir], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
152 ['allow_registration', '', True, _(u'Allow user to register new ' |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
153 u'account').encode('utf-8'), coerceBool], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
154 ['base_url_ext', '', '', |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
155 _(u'The external URL to use as base URL').encode('utf-8'), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
156 coerceUnicode], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
157 ] |
857
e17b15f1f260
server: added url_redirections_dict and url_redirections_profile in options
Goffi <goffi@goffi.org>
parents:
856
diff
changeset
|
158 # Options which are in sat.conf only |
e17b15f1f260
server: added url_redirections_dict and url_redirections_profile in options
Goffi <goffi@goffi.org>
parents:
856
diff
changeset
|
159 OPT_PARAMETERS_CFG = [ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
160 ["empty_password_allowed_warning_dangerous_list", None, "", None], |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1125
diff
changeset
|
161 ["vhosts_dict", None, {}, None], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
162 ["url_redirections_dict", None, {}, None], |
1132
0cafb79ced6d
server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents:
1128
diff
changeset
|
163 ["menu_json", None, {u'': C.DEFAULT_MENU}, None], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
164 ["tickets_trackers_json", None, None, None], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
165 ["mr_handlers_json", None, None, None], |
857
e17b15f1f260
server: added url_redirections_dict and url_redirections_profile in options
Goffi <goffi@goffi.org>
parents:
856
diff
changeset
|
166 ] |
514 | 167 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
168 |
514 | 169 def initialise(options): |
170 """Method to initialise global modules""" | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
171 # XXX: We need to configure logs before any log method is used, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
172 # so here is the best place. |
514 | 173 from sat.core import log_config |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
174 |
514 | 175 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C, backend_data=options) |
176 from libervia.server import server | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
177 |
514 | 178 # we can't import this file from libervia.server.server because it's not a true module |
179 # (there is no __init__.py file, as required by twistd plugin system), so we set the | |
180 # global values from here | |
181 server.DATA_DIR_DEFAULT = DATA_DIR_DEFAULT | |
182 server.OPT_PARAMETERS_BOTH = OPT_PARAMETERS_BOTH | |
183 server.OPT_PARAMETERS_CFG = OPT_PARAMETERS_CFG | |
184 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
|
185 |
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
|
186 |
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
|
187 class Options(usage.Options): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
188 # optArgs is not really useful in our case, we need more than a flag |
514 | 189 optParameters = OPT_PARAMETERS_BOTH |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
190 |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
191 def __init__(self): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
192 """Read SàT configuration file in order to overwrite the hard-coded default values |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
193 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
194 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
|
195 - hard-coded default values |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
196 - values from SàT configuration files |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
197 - 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
|
198 """ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
199 # If we do it the reading later: after the command line options have been parsed, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
200 # there's no good way to know |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
201 # if the options values are the hard-coded ones or if they have been passed |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
202 # on the command line. |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
203 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
204 # FIXME: must be refactored + code can be factorised with backend |
811 | 205 config_parser = ConfigParser.SafeConfigParser() |
206 config_parser.read(C.CONFIG_FILES) | |
207 self.handleDeprecated(config_parser) | |
514 | 208 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
|
209 name = param[0] |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
210 try: |
811 | 211 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception) |
883
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
212 if isinstance(value, unicode): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
213 value = value.encode("utf-8") |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
214 try: |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
215 param[2] = param[4](value) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
216 except IndexError: # the coerce method is optional |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
217 param[2] = value |
811 | 218 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
219 pass |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
220 usage.Options.__init__(self) |
817
cf1812a4445e
server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents:
814
diff
changeset
|
221 for opt_data in OPT_PARAMETERS_CFG: |
cf1812a4445e
server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents:
814
diff
changeset
|
222 self[opt_data[0]] = opt_data[2] |
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
|
223 |
811 | 224 def handleDeprecated(self, config_parser): |
225 """display warning and/or change option when a deprecated option if found | |
226 | |
227 param config_parser(ConfigParser): read ConfigParser instance for sat.conf | |
228 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
229 replacements = (("ssl_certificate", "tls_certificate"),) |
811 | 230 for old, new in replacements: |
231 try: | |
232 value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception) | |
233 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): | |
234 pass | |
235 else: | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
236 print(u"\n/!\\ Use of {old} is deprecated, please use {new} instead\n" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
237 .format(old=old, new=new)) |
811 | 238 config_parser.set(CONFIG_SECTION, new, value) |
239 | |
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
|
240 |
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
|
241 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
|
242 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
|
243 |
512
750db9ff8525
server side: launching script improvments:
Goffi <goffi@goffi.org>
parents:
474
diff
changeset
|
244 tapname = C.APP_NAME_FILE |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
245 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
|
246 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
|
247 |
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
|
248 def makeService(self, options): |
883
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
249 for opt in OPT_PARAMETERS_BOTH: |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
250 # FIXME: that's a ugly way to get unicode in Libervia |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
251 # from command line or sat.conf |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
252 # we should move to argparse and handle options this properly |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
253 try: |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
254 coerce_cb = opt[4] |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
255 except IndexError: |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
256 continue |
74be6217d913
server (options): Q&D trick to have unicode value from command line arguments and sat.conf
Goffi <goffi@goffi.org>
parents:
881
diff
changeset
|
257 if coerce_cb == coerceUnicode: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
258 options[opt[0]] = options[opt[0]].decode("utf-8") |
514 | 259 initialise(options.parent) |
260 from libervia.server import server | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
261 |
810 | 262 return server.Libervia(options) |
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
|
263 |
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
|
264 |
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
|
265 # 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
|
266 serviceMaker = LiberviaMaker() |