annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
1 #!/usr/bin/python
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
2 # -*- coding: utf-8 -*-
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
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 331
diff changeset
5 # 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
6
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 331
diff changeset
7 # 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
8 # 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
9 # 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
10 # (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
11
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 331
diff changeset
12 # 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
13 # 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
14 # 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
15 # 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
16
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 331
diff changeset
17 # 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
18 # 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
19
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 from zope.interface import implements
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
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
22 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
23 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
24 from twisted.application.service import IServiceMaker
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
25
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
26 from xdg.BaseDirectory import save_config_path
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
27 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
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
28 from os.path import expanduser
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
29
360
9834136b15ed added setup.py for the installation with setuptools
souliane <souliane@mailoo.org>
parents: 339
diff changeset
30 try:
9834136b15ed added setup.py for the installation with setuptools
souliane <souliane@mailoo.org>
parents: 339
diff changeset
31 from libervia_server import Libervia
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
32 opt_params = Libervia.OPT_PARAMETERS
360
9834136b15ed added setup.py for the installation with setuptools
souliane <souliane@mailoo.org>
parents: 339
diff changeset
33 except (ImportError, SystemExit):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
34 # avoid raising an error when you call twisted and sat is not launched
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
35 opt_params = []
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
36
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
37
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
38 class Options(usage.Options):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
39
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
40 # optArgs is not really useful in our case, we need more than a flag
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
41 optParameters = opt_params
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
42
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
43 def __init__(self):
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
44 """You want to read SàT configuration file now in order to overwrite the hard-coded default values.
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
45 This is because the priority is (form lowest to highest):
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
46 - use hard-coded default values
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
47 - use the values from SàT configuration files
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
48 - use the values passes on the command line
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
49 If you do it later: after the command line options have been parsed, there's no good way to know
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
50 if the options values are the hard-coded ones or if they have been passed on the command line.
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
51 """
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
52 for index in xrange(0, len(self.optParameters)):
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
53 name = self.optParameters[index][0]
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
54 try:
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
55 value = config.get('libervia', name)
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
56 self.optParameters[index][2] = self.optParameters[index][4](value)
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
57 except (NoSectionError, NoOptionError):
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
58 pass
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
59 usage.Options.__init__(self)
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
60
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
61
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
62 class LiberviaMaker(object):
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
63 implements(IServiceMaker, 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
64 tapname = 'libervia'
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
65 description = u'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
66 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
67
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
68 def makeService(self, options):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 360
diff changeset
69 return Libervia(dict(options)) # get rid of the usage.Option surcharge
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
70
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
71 config_path = save_config_path('sat')
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
72 config = SafeConfigParser()
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
73 config.read(map(expanduser, ['/etc/sat.conf', config_path + '/sat.conf', 'sat.conf', '.sat.conf']))
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
74
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
75 serviceMaker = LiberviaMaker()