source: tailor/vcpx/config.py @ 603

Revision 603, 2.7 KB checked in by lele@…, 8 years ago (diff)

Make Tailorizer descend from Project

Line 
1# -*- mode: python; coding: utf-8 -*-
2# :Progetto: vcpx -- Configuration bits
3# :Creato:   sab 30 lug 2005 20:51:28 CEST
4# :Autore:   Lele Gaifax <lele@nautilus.homeip.net>
5# :Licenza:  GNU General Public License
6#
7
8"""
9Handle the configuration details.
10"""
11
12__docformat__ = 'reStructuredText'
13
14from ConfigParser import SafeConfigParser
15
16class ConfigurationError(Exception):
17    """Configuration error"""
18
19class Config(SafeConfigParser):
20    """
21    Syntactic sugar around standard ConfigParser, for easier access to
22    the configuration. To access any single project use the configuration
23    as a dictionary.
24
25    The file may be a full fledged Python script, starting
26    with the usual "#!..." notation: in this case, it gets evaluated and
27    its documentation becomes the actual configuration, while the functions
28    it defines may be referenced by the 'before-commit' and 'after-commit'
29    slots.
30    """
31
32    def __init__(self, fp, defaults):
33        from cStringIO import StringIO
34
35        SafeConfigParser.__init__(self, defaults)
36        self.namespace = {}
37        if fp:
38            if fp.read(2) == '#!':
39                fp.seek(0)
40                exec fp.read() in globals(), self.namespace
41                config = StringIO(self.namespace['__doc__'])
42                self.readfp(config)
43            else:
44                fp.seek(0)
45                self.readfp(fp)
46
47    def projects(self):
48        """
49        Return either the default projects or all the projects in the
50        in the configuration.
51        """
52
53        defaultp = self.getTuple('DEFAULT', 'projects')
54        return defaultp or [s for s in self.sections() if not ':' in s]
55
56    def get(self, section, option, default=None):
57        """
58        Return the requested option value if present, otherwise the default.
59        """
60        if self.has_option(section, option):
61            value = SafeConfigParser.get(self, section, option)
62            if value == 'None':
63                return default
64            elif value == 'True':
65                return True
66            elif value == 'False':
67                return False
68            else:
69                return value
70        else:
71            return default
72
73    def getTuple(self, section, option, default=None):
74        """
75        Parse the requested option as a tuple, if its value starts with
76        an open bracket, otherwise consider the value a single item
77        tuple.
78        """
79
80        value = self.get(section, option, default)
81        if value:
82            if value.startswith('('):
83                items = value.strip()[1:-1]
84            else:
85                items = value
86            return [i.strip() for i in items.split(',')]
87        else:
88            return []
Note: See TracBrowser for help on using the repository browser.