source: tailor/vcpx/config.py @ 489

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

Moved the exception in the config and used

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.read(2) == '#!':
41            fp.seek(0)
42            exec fp.read() in globals(), self.namespace
43            config = StringIO(self.namespace['__doc__'])
44            self.readfp(config)
45        else:
46            fp.seek(0)
47            self.readfp(fp)
48
49    def projects(self):
50        """
51        Return either the default projects or all the projects in the
52        in the configuration.
53        """
54
55        defaultp = self.getTuple('DEFAULT', 'projects')
56        return defaultp or [s for s in self.sections() if not ':' in s]
57
58    def get(self, section, option, default=None):
59        """
60        Return the requested option value if present, otherwise the default.
61        """
62        if self.has_option(section, option):
63            return SafeConfigParser.get(self, section, option)
64        else:
65            return default
66
67    def getTuple(self, section, option, default=None):
68        """
69        Parse the requested option as a tuple, if its value starts with
70        an open bracket, otherwise consider the value a single item
71        tuple.
72        """
73
74        value = self.get(section, option, default)
75        if value:
76            if value.startswith('('):
77                items = value.strip()[1:-1]
78            else:
79                items = value
80            return [i.strip() for i in items.split(',')]
81        else:
82            return []
83
84    def __getitem__(self, name):
85        from project import Project
86
87        if not self.has_section(name):
88            raise UnknownProjectError("'%s' is not a known project" % name)
89
90        return Project(name, self)
Note: See TracBrowser for help on using the repository browser.