Mercurial > libervia-web
annotate twisted/plugins/libervia_server.py @ 1514:16228994ca3b
server: fix hot reloading of modules in dev mode
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 22 May 2023 11:57:49 +0200 |
parents | 106bae41f5c8 |
children | eb00d593801d |
rev | line source |
---|---|
1239 | 1 #!/usr/bin/env python3 |
2 | |
1397
ed037818d6de
core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
3 # Libervia Web: Libervia web frontend |
ed037818d6de
core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
4 # Copyright (C) 2013-2021 Jérôme Poisson <goffi@goffi.org> |
818 | 5 # 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
|
6 # 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
|
7 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # (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
|
12 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # 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
|
17 |
339
2067d6241927
fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents:
331
diff
changeset
|
18 # 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
|
19 # 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
|
20 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
21 from twisted.internet import defer |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
22 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
23 if defer.Deferred.debug: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
24 # 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
|
25 try: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
26 import ipdb |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
27 import pdb |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
28 |
437
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
29 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
|
30 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
|
31 except ImportError: |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
32 pass |
fa3b65b68971
server side: ipdb is used instead of pdb in debug mode
Goffi <goffi@goffi.org>
parents:
433
diff
changeset
|
33 |
1449
7e63fdb272e5
misc: don't take post version into account when comparing versions
Goffi <goffi@goffi.org>
parents:
1445
diff
changeset
|
34 import re |
514 | 35 import os.path |
856 | 36 import libervia |
37 import sat | |
514 | 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 | 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 | 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 | 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 | 51 |
1449
7e63fdb272e5
misc: don't take post version into account when comparing versions
Goffi <goffi@goffi.org>
parents:
1445
diff
changeset
|
52 RE_VER_POST = re.compile(r"\.post[0-9]+") |
7e63fdb272e5
misc: don't take post version into account when comparing versions
Goffi <goffi@goffi.org>
parents:
1445
diff
changeset
|
53 |
7e63fdb272e5
misc: don't take post version into account when comparing versions
Goffi <goffi@goffi.org>
parents:
1445
diff
changeset
|
54 if RE_VER_POST.sub("", libervia.__version__) != RE_VER_POST.sub("", sat.__version__): |
856 | 55 import sys |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
56 |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
57 sys.stderr.write( |
1216 | 58 """sat module version ({sat_version}) and {current_app} version ({current_version}) mismatch |
856 | 59 |
60 sat module is located at {sat_path} | |
61 libervia module is located at {libervia_path} | |
62 | |
63 Please be sure to have the same version running | |
64 """.format( | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
65 sat_version=sat.__version__, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
66 current_app=C.APP_NAME, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
67 current_version=libervia.__version__, |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
68 sat_path=os.path.dirname(sat.__file__), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
69 libervia_path=os.path.dirname(libervia.__file__), |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
70 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
71 ) |
856 | 72 sys.stderr.flush() |
73 # we call os._exit to avoid help to be printed by twisted | |
74 import os | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
75 |
856 | 76 os._exit(1) |
514 | 77 |
78 | |
79 def coerceConnectionType(value): # called from Libervia.OPT_PARAMETERS | |
1216 | 80 assert isinstance(value, str) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
81 allowed_values = ("http", "https", "both") |
514 | 82 if value not in allowed_values: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
83 raise ValueError( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
84 "%(given)s not in %(expected)s" |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
85 % {"given": value, "expected": str(allowed_values)} |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
86 ) |
514 | 87 return value |
88 | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
89 |
870
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
90 def coerceBool(value): |
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
91 return C.bool(value) |
a05f3b24f2ec
server: use C.bool for bool options instead of simple int
Goffi <goffi@goffi.org>
parents:
863
diff
changeset
|
92 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
93 |
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
|
94 def coerceUnicode(value): |
1216 | 95 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
|
96 # 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
|
97 # 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
|
98 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
|
99 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
100 |
514 | 101 DATA_DIR_DEFAULT = '' |
1363
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
102 # prefix used for environment variables |
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
103 ENV_PREFIX = "LIBERVIA_" |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
104 # 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
|
105 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html |
1216 | 106 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' " |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
107 "(to launch both servers)."), |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
108 coerceConnectionType], |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
109 ['port', 'p', 8080, |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
110 _('The port number to listen HTTP on.'), int], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
111 ['port_https', 's', 8443, |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
112 _('The port number to listen HTTPS on.'), int], |
1216 | 113 ['port_https_ext', 'e', 0, _('The external port number used for ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
114 'HTTPS (0 means port_https value).'), int], |
1216 | 115 ['tls_private_key', '', '', _('TLS certificate private key (PEM ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
116 'format)'), coerceUnicode], |
1216 | 117 ['tls_certificate', 'c', 'libervia.pem', _('TLS public ' |
118 'certificate or private key and public certificate combined ' | |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
119 '(PEM format)'), coerceUnicode], |
1216 | 120 ['tls_chain', '', '', _('TLS certificate intermediate chain (PEM ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
121 'format)'), coerceUnicode], |
1216 | 122 ['redirect_to_https', 'r', True, _('Automatically redirect from ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
123 'HTTP to HTTPS.'), coerceBool], |
1216 | 124 ['security_warning', 'w', True, _('Warn user that he is about to ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
125 'connect on HTTP.'), coerceBool], |
1216 | 126 ['passphrase', 'k', '', (_("Passphrase for the SàT profile " |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
127 "named '%s'") % C.SERVICE_PROFILE), |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
128 coerceUnicode], |
1216 | 129 ['allow_registration', '', True, _('Allow user to register new ' |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
130 'account'), coerceBool], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
131 ['base_url_ext', '', '', |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
132 _('The external URL to use as base URL'), |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
133 coerceUnicode], |
1367
ce5cfe004d4e
server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents:
1366
diff
changeset
|
134 ['bridge-retries', '', 10, |
ce5cfe004d4e
server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents:
1366
diff
changeset
|
135 _('Number of tries to connect to bridge before giving up'), int], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
136 ] |
1366
f61e95438050
server: don't encode options help messages, this is not needed anymore since we moved to Python 3
Goffi <goffi@goffi.org>
parents:
1365
diff
changeset
|
137 |
857
e17b15f1f260
server: added url_redirections_dict and url_redirections_profile in options
Goffi <goffi@goffi.org>
parents:
856
diff
changeset
|
138 # 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
|
139 OPT_PARAMETERS_CFG = [ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
140 ["empty_password_allowed_warning_dangerous_list", None, "", None], |
1128
6414fd795df4
server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents:
1125
diff
changeset
|
141 ["vhosts_dict", None, {}, None], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
142 ["url_redirections_dict", None, {}, None], |
1216 | 143 ["menu_json", None, {'': C.DEFAULT_MENU}, None], |
1359
2da573bf3f8b
server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents:
1275
diff
changeset
|
144 ["menu_extra_json", None, {}, None], |
1378
e3e303a30a74
pages (tickets): renamed "tickets" to "lists":
Goffi <goffi@goffi.org>
parents:
1368
diff
changeset
|
145 ["lists_directory_json", None, None, None], |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
146 ["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
|
147 ] |
514 | 148 |
1364
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
149 # Flags are in command line only |
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
150 OPT_FLAGS = [ |
1368 | 151 ['build-only', 'B', _("Only build website, don't run the server")], |
1364
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
152 ['dev-mode', 'D', _('Developer mode, automatically reload modified pages')], |
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
153 ] |
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
154 |
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
155 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
156 |
514 | 157 def initialise(options): |
158 """Method to initialise global modules""" | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
159 # 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
|
160 # so here is the best place. |
514 | 161 from sat.core import log_config |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
162 |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1449
diff
changeset
|
163 log_config.sat_configure(C.LOG_BACKEND_TWISTED, C, backend_data=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
|
164 |
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
|
165 |
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
|
166 class Options(usage.Options): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
167 # optArgs is not really useful in our case, we need more than a flag |
514 | 168 optParameters = OPT_PARAMETERS_BOTH |
1364
df40708c4c76
server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents:
1363
diff
changeset
|
169 optFlags = OPT_FLAGS |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
170 |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
171 def __init__(self): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
172 """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
|
173 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
174 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
|
175 - hard-coded default values |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
176 - values from SàT configuration files |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
177 - 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
|
178 """ |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
179 # 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
|
180 # there's no good way to know |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
181 # 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
|
182 # on the command line. |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
183 |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
184 # FIXME: must be refactored + code can be factorised with backend |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1449
diff
changeset
|
185 config_parser = config.parse_main_conf() |
811 | 186 self.handleDeprecated(config_parser) |
514 | 187 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
|
188 name = param[0] |
1363
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
189 env_name = f"{ENV_PREFIX}{name.upper()}" |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
190 try: |
1363
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
191 value = os.getenv(env_name) |
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
192 if value is None: |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1449
diff
changeset
|
193 value = config.config_get( |
1363
c3dac1e11341
server: options can now be specified with environment variables:
Goffi <goffi@goffi.org>
parents:
1359
diff
changeset
|
194 config_parser, C.CONFIG_SECTION, name, Exception) |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
195 try: |
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
196 param[2] = param[4](value) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
197 except IndexError: # the coerce method is optional |
470
34ce41e014c4
server side: options managing improvments:
Goffi <goffi@goffi.org>
parents:
466
diff
changeset
|
198 param[2] = value |
1216 | 199 except (configparser.NoSectionError, configparser.NoOptionError): |
415
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
200 pass |
fadbba1d793f
server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents:
360
diff
changeset
|
201 usage.Options.__init__(self) |
817
cf1812a4445e
server: fixed empty_password_allowed_warning_dangerous_list option
Goffi <goffi@goffi.org>
parents:
814
diff
changeset
|
202 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
|
203 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
|
204 |
811 | 205 def handleDeprecated(self, config_parser): |
206 """display warning and/or change option when a deprecated option if found | |
207 | |
208 param config_parser(ConfigParser): read ConfigParser instance for sat.conf | |
209 """ | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
210 replacements = (("ssl_certificate", "tls_certificate"),) |
811 | 211 for old, new in replacements: |
212 try: | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1449
diff
changeset
|
213 value = config.config_get(config_parser, C.CONFIG_SECTION, old, Exception) |
1216 | 214 except (configparser.NoSectionError, configparser.NoOptionError): |
811 | 215 pass |
216 else: | |
1216 | 217 print(("\n/!\\ Use of {old} is deprecated, please use {new} instead\n" |
218 .format(old=old, new=new))) | |
1275
334d044f2713
server: default theme can now be specified in site section of `sat.conf` with `theme` key
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
219 config_parser.set(C.CONFIG_SECTION, new, value) |
811 | 220 |
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
|
221 |
1216 | 222 @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
|
223 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
|
224 |
512
750db9ff8525
server side: launching script improvments:
Goffi <goffi@goffi.org>
parents:
474
diff
changeset
|
225 tapname = C.APP_NAME_FILE |
1397
ed037818d6de
core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents:
1378
diff
changeset
|
226 description = _("The web frontend of Libervia") |
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
|
227 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
|
228 |
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
|
229 def makeService(self, options): |
1135
4426c328eb83
core: fixed reactor crash by installing reactor only in makeService
Goffi <goffi@goffi.org>
parents:
1132
diff
changeset
|
230 from twisted.internet import gireactor |
4426c328eb83
core: fixed reactor crash by installing reactor only in makeService
Goffi <goffi@goffi.org>
parents:
1132
diff
changeset
|
231 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
|
232 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
|
233 # 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
|
234 # 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
|
235 # 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 if coerce_cb == coerceUnicode: |
1216 | 241 if not isinstance(options[opt[0]], str): |
242 print(f"FIXME: {opt[0]} is not unicode") | |
243 options[opt[0]] = options[opt[0]].decode("utf-8") | |
514 | 244 initialise(options.parent) |
245 from libervia.server import server | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1081
diff
changeset
|
246 |
810 | 247 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
|
248 |
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
|
249 |
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
|
250 # 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
|
251 serviceMaker = LiberviaMaker() |