comparison twisted/plugins/libervia.py @ 415:fadbba1d793f

server_side: added support for SSL and related parameters: Full parameters list: -t, --connection_type= 'http', 'https' or 'both' (to launch both servers). [default: both] -p, --port= The port number to listen HTTP on. [default: 8080] -s, --port_https= The port number to listen HTTPS on. [default: 8443] -c, --ssl_certificate= PEM certificate with both private and public parts. [default: libervia.pem] -r, --redirect_to_https= automatically redirect from HTTP to HTTPS. [default: 0] -w, --security_warning= warn user that he is about to connect on HTTP. [default: 1]
author souliane <souliane@mailoo.org>
date Tue, 18 Mar 2014 15:59:38 +0100
parents 9834136b15ed
children e9bc7854bce6
comparison
equal deleted inserted replaced
414:ae598511850d 415:fadbba1d793f
20 from zope.interface import implements 20 from zope.interface import implements
21 21
22 from twisted.python import usage 22 from twisted.python import usage
23 from twisted.plugin import IPlugin 23 from twisted.plugin import IPlugin
24 from twisted.application.service import IServiceMaker 24 from twisted.application.service import IServiceMaker
25 from twisted.application import internet
26 25
27 from xdg.BaseDirectory import save_config_path 26 from xdg.BaseDirectory import save_config_path
28 from ConfigParser import SafeConfigParser, NoSectionError 27 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
29 from os.path import expanduser 28 from os.path import expanduser
30 29
31 try: 30 try:
32 from libervia_server import Libervia 31 from libervia_server import Libervia
32 opt_params = Libervia.OPT_PARAMETERS
33 except (ImportError, SystemExit): 33 except (ImportError, SystemExit):
34 pass # avoid raising an error when you call twisted and sat is not launched 34 # avoid raising an error when you call twisted and sat is not launched
35 opt_params = []
35 36
36 37
37 class Options(usage.Options): 38 class Options(usage.Options):
38 optParameters = [['port', 'p', 8080, 'The port number to listen on.']] 39
40 # optArgs is not really useful in our case, we need more than a flag
41 optParameters = opt_params
42
43 def __init__(self):
44 """You want to read SàT configuration file now in order to overwrite the hard-coded default values.
45 This is because the priority is (form lowest to highest):
46 - use hard-coded default values
47 - use the values from SàT configuration files
48 - use the values passes on the command line
49 If you do it later: after the command line options have been parsed, there's no good way to know
50 if the options values are the hard-coded ones or if they have been passed on the command line.
51 """
52 for index in xrange(0, len(self.optParameters)):
53 name = self.optParameters[index][0]
54 try:
55 value = config.get('libervia', name)
56 self.optParameters[index][2] = self.optParameters[index][4](value)
57 except (NoSectionError, NoOptionError):
58 pass
59 usage.Options.__init__(self)
39 60
40 61
41 class LiberviaMaker(object): 62 class LiberviaMaker(object):
42 implements(IServiceMaker, IPlugin) 63 implements(IServiceMaker, IPlugin)
43 tapname = 'libervia' 64 tapname = 'libervia'
44 description = 'The web frontend of Salut à Toi' 65 description = u'The web frontend of Salut à Toi'
45 options = Options 66 options = Options
46 67
47 def makeService(self, options): 68 def makeService(self, options):
48 if not isinstance(options['port'], int): 69 return Libervia(dict(options)) # get rid of the usage.Option surcharge
49 port = int(options['port'])
50 else:
51 try:
52 port = config.getint('libervia', 'port')
53 except NoSectionError:
54 port = 8080
55 return Libervia(port=port)
56
57 70
58 config_path = save_config_path('sat') 71 config_path = save_config_path('sat')
59 config = SafeConfigParser() 72 config = SafeConfigParser()
60 config.read(map(expanduser, ['/etc/sat.conf', config_path + '/sat.conf', 'sat.conf', '.sat.conf'])) 73 config.read(map(expanduser, ['/etc/sat.conf', config_path + '/sat.conf', 'sat.conf', '.sat.conf']))
61 74