annotate twisted/plugins/libervia_server.py @ 1259:0b269d4a46a3

task (js_modules): handle `extra_init`: if `extra_init` is found in module metadata's `brython_map`, it is added at the end of the generated module, and `{build_dir}` is replaced by actual build dir if present.
author Goffi <goffi@goffi.org>
date Sun, 03 May 2020 19:32:20 +0200
parents f511f8fbbf8a
children 334d044f2713
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1239
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1221
diff changeset
1 #!/usr/bin/env python3
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1221
diff changeset
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
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
f2170536ba23 date update
Goffi <goffi@goffi.org>
parents: 990
diff changeset
5 # Copyright (C) 2013-2018 Jérôme Poisson <goffi@goffi.org>
818
f8a7a046ff9c copyright update
Goffi <goffi@goffi.org>
parents: 817
diff changeset
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
437
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
22 from twisted.internet import defer
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
23
437
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
24 if defer.Deferred.debug:
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
25 # 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
26 try:
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
27 import ipdb
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
28 import pdb
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
29
437
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
30 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
31 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
32 except ImportError:
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
33 pass
fa3b65b68971 server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents: 433
diff changeset
34
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
35 import os.path
856
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
36 import libervia
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
37 import sat
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
38
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 446
diff changeset
39 from libervia.server.constants import Const as C
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
40
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
41 from sat.core.i18n import _
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
42 from sat.tools import config
438
582c435dab6b server side: new log system is used
Goffi <goffi@goffi.org>
parents: 437
diff changeset
43
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
44 from zope.interface import implementer
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
45
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 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
47 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
48 from twisted.application.service import IServiceMaker
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
49 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
50
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
51
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
52 CONFIG_SECTION = C.APP_NAME.lower()
856
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
53 if libervia.__version__ != sat.__version__:
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
54 import sys
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
55
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
56 sys.stderr.write(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
57 """sat module version ({sat_version}) and {current_app} version ({current_version}) mismatch
856
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
58
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
59 sat module is located at {sat_path}
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
60 libervia module is located at {libervia_path}
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
61
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
62 Please be sure to have the same version running
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
63 """.format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
64 sat_version=sat.__version__,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
65 current_app=C.APP_NAME,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
66 current_version=libervia.__version__,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
67 sat_path=os.path.dirname(sat.__file__),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
68 libervia_path=os.path.dirname(libervia.__file__),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
69 ).encode(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
70 "utf-8"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
71 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
72 )
856
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
73 sys.stderr.flush()
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
74 # we call os._exit to avoid help to be printed by twisted
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
75 import os
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
76
856
fd438e51bda8 server: version check:
Goffi <goffi@goffi.org>
parents: 818
diff changeset
77 os._exit(1)
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
78
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
79
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
80 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
81 assert isinstance(value, str)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
82 allowed_values = ("http", "https", "both")
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
83 if value not in allowed_values:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
84 raise ValueError(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
85 "%(given)s not in %(expected)s"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
86 % {"given": value, "expected": str(allowed_values)}
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
87 )
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
88 return value
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
89
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
90
870
a05f3b24f2ec server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents: 863
diff changeset
91 def coerceBool(value):
a05f3b24f2ec server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents: 863
diff changeset
92 return C.bool(value)
a05f3b24f2ec server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents: 863
diff changeset
93
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
94
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
95 def coerceUnicode(value):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
96 assert isinstance(value, str)
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
97 # 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
98 # 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
99 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
100
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
101
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
102 DATA_DIR_DEFAULT = ''
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
103 # 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
104 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
105 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' "
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
106 "(to launch both servers).").encode('utf-8'),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
107 coerceConnectionType],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
108 ['port', 'p', 8080,
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
109 _('The port number to listen HTTP on.').encode('utf-8'), int],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
110 ['port_https', 's', 8443,
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
111 _('The port number to listen HTTPS on.').encode('utf-8'), int],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
112 ['port_https_ext', 'e', 0, _('The external port number used for '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
113 'HTTPS (0 means port_https value).').encode('utf-8'), int],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
114 ['tls_private_key', '', '', _('TLS certificate private key (PEM '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
115 'format)').encode('utf-8'), coerceUnicode],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
116 ['tls_certificate', 'c', 'libervia.pem', _('TLS public '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
117 'certificate or private key and public certificate combined '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
118 '(PEM format)').encode('utf-8'), coerceUnicode],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
119 ['tls_chain', '', '', _('TLS certificate intermediate chain (PEM '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
120 'format)').encode('utf-8'), coerceUnicode],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
121 ['redirect_to_https', 'r', True, _('Automatically redirect from '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
122 'HTTP to HTTPS.').encode('utf-8'), coerceBool],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
123 ['security_warning', 'w', True, _('Warn user that he is about to '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
124 'connect on HTTP.').encode('utf-8'), coerceBool],
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
125 ['passphrase', 'k', '', (_("Passphrase for the SàT profile "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
126 "named '%s'") % C.SERVICE_PROFILE).encode('utf-8'),
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
127 coerceUnicode],
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
128 ['allow_registration', '', True, _('Allow user to register new '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
129 'account').encode('utf-8'), coerceBool],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
130 ['base_url_ext', '', '',
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
131 _('The external URL to use as base URL').encode('utf-8'),
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
132 coerceUnicode],
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
133 ['dev_mode', 'D', False, _('Developer mode, automatically reload'
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
134 'modified pages').encode('utf-8'), coerceBool],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
135 ]
857
e17b15f1f260 server: added url_redirections_dict and url_redirections_profile in options
Goffi <goffi@goffi.org>
parents: 856
diff changeset
136 # 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
137 OPT_PARAMETERS_CFG = [
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
138 ["empty_password_allowed_warning_dangerous_list", None, "", None],
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1125
diff changeset
139 ["vhosts_dict", None, {}, None],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
140 ["url_redirections_dict", None, {}, None],
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
141 ["menu_json", None, {'': C.DEFAULT_MENU}, None],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
142 ["tickets_trackers_json", None, None, None],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
143 ["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
144 ]
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
145
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
146
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
147 def initialise(options):
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
148 """Method to initialise global modules"""
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
149 # 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
150 # so here is the best place.
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
151 from sat.core import log_config
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
152
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
153 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C, backend_data=options)
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
154 from libervia.server import server
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
155
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
156 # we can't import this file from libervia.server.server because it's not a true module
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
157 # (there is no __init__.py file, as required by twistd plugin system), so we set the
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
158 # global values from here
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
159 server.DATA_DIR_DEFAULT = DATA_DIR_DEFAULT
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
160 server.OPT_PARAMETERS_BOTH = OPT_PARAMETERS_BOTH
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
161 server.OPT_PARAMETERS_CFG = OPT_PARAMETERS_CFG
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
162
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
163
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
164 class Options(usage.Options):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
165 # optArgs is not really useful in our case, we need more than a flag
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
166 optParameters = OPT_PARAMETERS_BOTH
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
167
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
168 def __init__(self):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
169 """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
170
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
171 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
172 - hard-coded default values
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
173 - values from SàT configuration files
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
174 - 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
175 """
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
176 # 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
177 # there's no good way to know
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
178 # 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
179 # on the command line.
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
180
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
181 # FIXME: must be refactored + code can be factorised with backend
1221
331ada56cdca misc: renamed SafeConfigParser in libervia_server twisted plugin after python 3 port
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
182 config_parser = configparser.ConfigParser()
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
183 config_parser.read(C.CONFIG_FILES)
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
184 self.handleDeprecated(config_parser)
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
185 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
186 name = param[0]
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
187 try:
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
188 value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
189 # if isinstance(value, str):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
190 # value = value.encode("utf-8")
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
191 try:
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
192 param[2] = param[4](value)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
193 except IndexError: # the coerce method is optional
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 466
diff changeset
194 param[2] = value
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
195 except (configparser.NoSectionError, configparser.NoOptionError):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
196 pass
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
197 usage.Options.__init__(self)
817
cf1812a4445e server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents: 814
diff changeset
198 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
199 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
200
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
201 def handleDeprecated(self, config_parser):
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
202 """display warning and/or change option when a deprecated option if found
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
203
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
204 param config_parser(ConfigParser): read ConfigParser instance for sat.conf
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
205 """
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
206 replacements = (("ssl_certificate", "tls_certificate"),)
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
207 for old, new in replacements:
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
208 try:
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
209 value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
210 except (configparser.NoSectionError, configparser.NoOptionError):
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
211 pass
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
212 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
213 print(("\n/!\\ Use of {old} is deprecated, please use {new} instead\n"
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
214 .format(old=old, new=new)))
811
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
215 config_parser.set(CONFIG_SECTION, new, value)
b1826adbeeff server: tls_certificate option:
Goffi <goffi@goffi.org>
parents: 810
diff changeset
216
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
217
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
218 @implementer(IServiceMaker, IPlugin)
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
219 class LiberviaMaker(object):
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 446
diff changeset
220
512
750db9ff8525 server side: launching script improvments:
Goffi <goffi@goffi.org>
parents: 474
diff changeset
221 tapname = C.APP_NAME_FILE
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
222 description = _("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
223 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
224
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
225 def makeService(self, options):
1135
4426c328eb83 core: fixed reactor crash by installing reactor only in makeService
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
226 from twisted.internet import gireactor
4426c328eb83 core: fixed reactor crash by installing reactor only in makeService
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
227 gireactor.install()
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
228 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
229 # 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
230 # 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
231 # 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
232 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
233 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
234 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
235 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
236 if coerce_cb == coerceUnicode:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
237 if not isinstance(options[opt[0]], str):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
238 print(f"FIXME: {opt[0]} is not unicode")
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
239 options[opt[0]] = options[opt[0]].decode("utf-8")
514
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
240 initialise(options.parent)
530c88c1deee server_side: plugin refactoring:
Goffi <goffi@goffi.org>
parents: 512
diff changeset
241 from libervia.server import server
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1081
diff changeset
242
810
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 712
diff changeset
243 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
244
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
245
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
246 # 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
247 serviceMaker = LiberviaMaker()