comparison src/twisted/plugins/libervia_server.py @ 470:34ce41e014c4

server side: options managing improvments: - cleaned a bit the code / comments - i18n - added Libervia.OPT_PARAMETERS_CFG and moved Libervia.OPT_PARAMETERS to Libervia.OPT_PARAMETERS_BOTH, to allow options to be only in sat.conf - use of new constant ASCII_APP_NAME for tap name
author Goffi <goffi@goffi.org>
date Tue, 10 Jun 2014 15:38:47 +0200
parents 01880aa8ea2d
children 6700386291f1
comparison
equal deleted inserted replaced
469:305044acb6f0 470:34ce41e014c4
30 30
31 # XXX: We need to configure logs before any log method is used, so here is the best place. 31 # XXX: We need to configure logs before any log method is used, so here is the best place.
32 from libervia.server.constants import Const as C 32 from libervia.server.constants import Const as C
33 from sat.core import log_config 33 from sat.core import log_config
34 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C) 34 log_config.satConfigure(C.LOG_BACKEND_TWISTED, C)
35
36
37
38 from sat.core.i18n import _
35 from sat.tools.config import getConfig 39 from sat.tools.config import getConfig
36 40
37 from zope.interface import implements 41 from zope.interface import implements
38 42
39 from twisted.python import usage 43 from twisted.python import usage
41 from twisted.application.service import IServiceMaker 45 from twisted.application.service import IServiceMaker
42 46
43 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError 47 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
44 try: 48 try:
45 from libervia.server.server import Libervia 49 from libervia.server.server import Libervia
46 opt_params = Libervia.OPT_PARAMETERS 50 opt_params = Libervia.OPT_PARAMETERS_BOTH
51 cfg_params = Libervia.OPT_PARAMETERS_CFG
47 except (ImportError, SystemExit): 52 except (ImportError, SystemExit):
48 # avoid raising an error when you call twisted and sat is not launched 53 # avoid raising an error when you call twisted and sat is not launched
49 opt_params = [] 54 opt_params = []
55 cfg_params = []
50 56
51 57
52 class Options(usage.Options): 58 class Options(usage.Options):
53 59
54 # optArgs is not really useful in our case, we need more than a flag 60 # optArgs is not really useful in our case, we need more than a flag
55 optParameters = opt_params 61 optParameters = opt_params
56 62
57 def __init__(self): 63 def __init__(self):
58 """You want to read SàT configuration file now in order to overwrite the hard-coded default values. 64 """Read SàT configuration file in order to overwrite the hard-coded default values.
59 This is because the priority for the usage of the values is (from lowest to highest): 65
60 - hard-coded default values 66 Priority for the usage of the values is (from lowest to highest):
61 - values from SàT configuration files 67 - hard-coded default values
62 - values passed on the command line 68 - values from SàT configuration files
63 If you do it later: after the command line options have been parsed, there's no good way to know 69 - values passed on the command line
64 if the options values are the hard-coded ones or if they have been passed on the command line.
65 """ 70 """
71 # If we do it the reading later: after the command line options have been parsed, there's no good way to know
72 # if the options values are the hard-coded ones or if they have been passed on the command line.
73
74 # FIXME: must be refactored + code can be factorised with backend
66 config = SafeConfigParser() 75 config = SafeConfigParser()
67 config.read(C.CONFIG_FILES) 76 config.read(C.CONFIG_FILES)
68 for index, param in list(enumerate(self.optParameters)): 77 for param in self.optParameters + cfg_params:
69 # index is only used to not modify the loop variable "param"
70 name = param[0] 78 name = param[0]
71 try: 79 try:
72 value = getConfig(config, 'libervia', name) 80 value = getConfig(config, 'libervia', name)
73 self.optParameters[index][2] = param[4](value) 81 try:
82 param[2] = param[4](value)
83 except IndexError: # the coerce method is optional
84 param[2] = value
74 except (NoSectionError, NoOptionError): 85 except (NoSectionError, NoOptionError):
75 pass 86 pass
76 usage.Options.__init__(self) 87 usage.Options.__init__(self)
77 88
78 89
79 class LiberviaMaker(object): 90 class LiberviaMaker(object):
80 implements(IServiceMaker, IPlugin) 91 implements(IServiceMaker, IPlugin)
81 92
82 tapname = 'libervia' 93 tapname = C.ASCII_APP_NAME
83 # FIXME: twistd bugs when printing 'à' on "Unknown command" error (works on normal command listing) 94 description = _(u'The web frontend of Salut à Toi')
84 description = u'The web frontend of Salut a Toi'
85 options = Options 95 options = Options
86 96
87 def makeService(self, options): 97 def makeService(self, options):
88 return Libervia(**dict(options)) # get rid of the usage.Option overload 98 return Libervia(**dict(options)) # get rid of the usage.Option overload
89 99