view twisted/plugins/libervia.py @ 416:e9bc7854bce6

server_side: bug fix passing twistd arguments to Libervia
author souliane <souliane@mailoo.org>
date Mon, 24 Mar 2014 17:12:14 +0100
parents fadbba1d793f
children 2bd609d7dd65
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Libervia: a Salut à Toi frontend
# Copyright (C) 2013  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from zope.interface import implements

from twisted.python import usage
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker

from xdg.BaseDirectory import save_config_path
from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
from os.path import expanduser

try:
    from libervia_server import Libervia
    opt_params = Libervia.OPT_PARAMETERS
except (ImportError, SystemExit):
    # avoid raising an error when you call twisted and sat is not launched
    opt_params = []


class Options(usage.Options):

    # optArgs is not really useful in our case, we need more than a flag
    optParameters = opt_params

    def __init__(self):
        """You want to read SàT configuration file now in order to overwrite the hard-coded default values.
        This is because the priority is (form lowest to highest):
        - use hard-coded default values
        - use the values from SàT configuration files
        - use the values passes on the command line
        If you do it later: after the command line options have been parsed, there's no good way to know
        if the  options values are the hard-coded ones or if they have been passed on the command line.
        """
        for index in xrange(0, len(self.optParameters)):
            name = self.optParameters[index][0]
            try:
                value = config.get('libervia', name)
                self.optParameters[index][2] = self.optParameters[index][4](value)
            except (NoSectionError, NoOptionError):
                pass
        usage.Options.__init__(self)


class LiberviaMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = 'libervia'
    description = u'The web frontend of Salut à Toi'
    options = Options

    def makeService(self, options):
        return Libervia(**dict(options))  # get rid of the usage.Option overload

config_path = save_config_path('sat')
config = SafeConfigParser()
config.read(map(expanduser, ['/etc/sat.conf', config_path + '/sat.conf', 'sat.conf', '.sat.conf']))

serviceMaker = LiberviaMaker()