comparison twisted/plugins/libervia_server.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents 106bae41f5c8
children a44f77559279
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
31 except ImportError: 31 except ImportError:
32 pass 32 pass
33 33
34 import re 34 import re
35 import os.path 35 import os.path
36 import libervia 36 import libervia.web
37 import sat 37 import libervia.backend
38 38
39 from libervia.server.constants import Const as C 39 from libervia.web.server.constants import Const as C
40 40
41 from sat.core.i18n import _ 41 from libervia.backend.core.i18n import _
42 from sat.tools import config 42 from libervia.backend.tools import config
43 43
44 from zope.interface import implementer 44 from zope.interface import implementer
45 45
46 from twisted.python import usage 46 from twisted.python import usage
47 from twisted.plugin import IPlugin 47 from twisted.plugin import IPlugin
49 import configparser 49 import configparser
50 50
51 51
52 RE_VER_POST = re.compile(r"\.post[0-9]+") 52 RE_VER_POST = re.compile(r"\.post[0-9]+")
53 53
54 if RE_VER_POST.sub("", libervia.__version__) != RE_VER_POST.sub("", sat.__version__): 54 if RE_VER_POST.sub("", libervia.web.__version__) != RE_VER_POST.sub("", libervia.backend.__version__):
55 import sys 55 import sys
56 56
57 sys.stderr.write( 57 sys.stderr.write(
58 """sat module version ({sat_version}) and {current_app} version ({current_version}) mismatch 58 """libervia.backend module version ({sat_version}) and {current_app} version ({current_version}) mismatch
59 59
60 sat module is located at {sat_path} 60 libervia.backend module is located at {sat_path}
61 libervia module is located at {libervia_path} 61 libervia module is located at {libervia_path}
62 62
63 Please be sure to have the same version running 63 Please be sure to have the same version running
64 """.format( 64 """.format(
65 sat_version=sat.__version__, 65 sat_version=libervia.backend.__version__,
66 current_app=C.APP_NAME, 66 current_app=C.APP_NAME,
67 current_version=libervia.__version__, 67 current_version=libervia.web.__version__,
68 sat_path=os.path.dirname(sat.__file__), 68 sat_path=os.path.dirname(libervia.backend.__file__),
69 libervia_path=os.path.dirname(libervia.__file__), 69 libervia_path=os.path.dirname(libervia.web.__file__),
70 ) 70 )
71 ) 71 )
72 sys.stderr.flush() 72 sys.stderr.flush()
73 # we call os._exit to avoid help to be printed by twisted 73 # we call os._exit to avoid help to be printed by twisted
74 import os 74 import os
99 99
100 100
101 DATA_DIR_DEFAULT = '' 101 DATA_DIR_DEFAULT = ''
102 # prefix used for environment variables 102 # prefix used for environment variables
103 ENV_PREFIX = "LIBERVIA_" 103 ENV_PREFIX = "LIBERVIA_"
104 # options which are in sat.conf and on command line, 104 # options which are in libervia.conf and on command line,
105 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html 105 # see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
106 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' " 106 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _("'http', 'https' or 'both' "
107 "(to launch both servers)."), 107 "(to launch both servers)."),
108 coerceConnectionType], 108 coerceConnectionType],
109 ['port', 'p', 8080, 109 ['port', 'p', 8080,
133 coerceUnicode], 133 coerceUnicode],
134 ['bridge-retries', '', 10, 134 ['bridge-retries', '', 10,
135 _('Number of tries to connect to bridge before giving up'), int], 135 _('Number of tries to connect to bridge before giving up'), int],
136 ] 136 ]
137 137
138 # Options which are in sat.conf only 138 # Options which are in libervia.conf only
139 OPT_PARAMETERS_CFG = [ 139 OPT_PARAMETERS_CFG = [
140 ["empty_password_allowed_warning_dangerous_list", None, "", None], 140 ["empty_password_allowed_warning_dangerous_list", None, "", None],
141 ["vhosts_dict", None, {}, None], 141 ["vhosts_dict", None, {}, None],
142 ["url_redirections_dict", None, {}, None], 142 ["url_redirections_dict", None, {}, None],
143 ["menu_json", None, {'': C.DEFAULT_MENU}, None], 143 ["menu_json", None, {'': C.DEFAULT_MENU}, None],
156 156
157 def initialise(options): 157 def initialise(options):
158 """Method to initialise global modules""" 158 """Method to initialise global modules"""
159 # XXX: We need to configure logs before any log method is used, 159 # XXX: We need to configure logs before any log method is used,
160 # so here is the best place. 160 # so here is the best place.
161 from sat.core import log_config 161 from libervia.backend.core import log_config
162 162
163 log_config.sat_configure(C.LOG_BACKEND_TWISTED, C, backend_data=options) 163 log_config.sat_configure(C.LOG_BACKEND_TWISTED, C, backend_data=options)
164 164
165 165
166 class Options(usage.Options): 166 class Options(usage.Options):
203 self[opt_data[0]] = opt_data[2] 203 self[opt_data[0]] = opt_data[2]
204 204
205 def handleDeprecated(self, config_parser): 205 def handleDeprecated(self, config_parser):
206 """display warning and/or change option when a deprecated option if found 206 """display warning and/or change option when a deprecated option if found
207 207
208 param config_parser(ConfigParser): read ConfigParser instance for sat.conf 208 param config_parser(ConfigParser): read ConfigParser instance for libervia.conf
209 """ 209 """
210 replacements = (("ssl_certificate", "tls_certificate"),) 210 replacements = (("ssl_certificate", "tls_certificate"),)
211 for old, new in replacements: 211 for old, new in replacements:
212 try: 212 try:
213 value = config.config_get(config_parser, C.CONFIG_SECTION, old, Exception) 213 value = config.config_get(config_parser, C.CONFIG_SECTION, old, Exception)
229 def makeService(self, options): 229 def makeService(self, options):
230 from twisted.internet import gireactor 230 from twisted.internet import gireactor
231 gireactor.install() 231 gireactor.install()
232 for opt in OPT_PARAMETERS_BOTH: 232 for opt in OPT_PARAMETERS_BOTH:
233 # FIXME: that's a ugly way to get unicode in Libervia 233 # FIXME: that's a ugly way to get unicode in Libervia
234 # from command line or sat.conf 234 # from command line or libervia.conf
235 # we should move to argparse and handle options this properly 235 # we should move to argparse and handle options this properly
236 try: 236 try:
237 coerce_cb = opt[4] 237 coerce_cb = opt[4]
238 except IndexError: 238 except IndexError:
239 continue 239 continue
240 if coerce_cb == coerceUnicode: 240 if coerce_cb == coerceUnicode:
241 if not isinstance(options[opt[0]], str): 241 if not isinstance(options[opt[0]], str):
242 print(f"FIXME: {opt[0]} is not unicode") 242 print(f"FIXME: {opt[0]} is not unicode")
243 options[opt[0]] = options[opt[0]].decode("utf-8") 243 options[opt[0]] = options[opt[0]].decode("utf-8")
244 initialise(options.parent) 244 initialise(options.parent)
245 from libervia.server import server 245 from libervia.web.server import server
246 246
247 return server.Libervia(options) 247 return server.LiberviaWeb(options)
248 248
249 249
250 # affectation to some variable is necessary for twisted introspection to work 250 # affectation to some variable is necessary for twisted introspection to work
251 serviceMaker = LiberviaMaker() 251 serviceMaker = LiberviaMaker()