source: tailor/vcpx/config.py @ 491

Revision 491, 2.8 KB checked in by lele@…, 8 years ago (diff)

Allow the creation of a Config object with no file associated

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 UnknownProjectError(Exception):
20    "Project does not exist"
21
22class Config(SafeConfigParser):
23    """
24    Syntactic sugar around standard ConfigParser, for easier access to
25    the configuration. To access any single project use the configuration
26    as a dictionary.
27
28    The file may be a full fledged Python script, starting
29    with the usual "#!..." notation: in this case, it gets evaluated and
30    its documentation becomes the actual configuration, while the functions
31    it defines may be referenced by the 'before-commit' and 'after-commit'
32    slots.
33    """
34
35    def __init__(self, fp, defaults):
36        from cStringIO import StringIO
37
38        SafeConfigParser.__init__(self, defaults)
39        self.namespace = {}
40        if fp:
41            if fp.read(2) == '#!':
42                fp.seek(0)
43                exec fp.read() in globals(), self.namespace
44                config = StringIO(self.namespace['__doc__'])
45                self.readfp(config)
46            else:
47                fp.seek(0)
48                self.readfp(fp)
49
50    def projects(self):
51        """
52        Return either the default projects or all the projects in the
53        in the configuration.
54        """
55
56        defaultp = self.getTuple('DEFAULT', 'projects')
57        return defaultp or [s for s in self.sections() if not ':' in s]
58
59    def get(self, section, option, default=None):
60        """
61        Return the requested option value if present, otherwise the default.
62        """
63        if self.has_option(section, option):
64            value = SafeConfigParser.get(self, section, option)
65            if value == 'None':
66                return default
67            else:
68                return value
69        else:
70            return default
71
72    def getTuple(self, section, option, default=None):
73        """
74        Parse the requested option as a tuple, if its value starts with
75        an open bracket, otherwise consider the value a single item
76        tuple.
77        """
78
79        value = self.get(section, option, default)
80        if value:
81            if value.startswith('('):
82                items = value.strip()[1:-1]
83            else:
84                items = value
85            return [i.strip() for i in items.split(',')]
86        else:
87            return []
88
89    def __getitem__(self, name):
90        from project import Project
91
92        if not self.has_section(name):
93            raise UnknownProjectError("'%s' is not a known project" % name)
94
95        return Project(name, self)
Note: See TracBrowser for help on using the repository browser.