source: tailor/vcpx/config.py @ 612

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

Pass vars to ConfigParser?.get() to override defaults with project's settings

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, vars=None):
57        """
58        Return the requested option value if present, otherwise the default.
59        """
60
61        if self.has_option(section, option) or (vars and option in vars):
62            value = SafeConfigParser.get(self, section, option, vars=vars)
63            if value == 'None':
64                return default
65            elif value == 'True':
66                return True
67            elif value == 'False':
68                return False
69            else:
70                return value
71        else:
72            return default
73
74    def getTuple(self, section, option, default=None):
75        """
76        Parse the requested option as a tuple, if its value starts with
77        an open bracket, otherwise consider the value a single item
78        tuple.
79        """
80
81        value = self.get(section, option, default)
82        if value:
83            if value.startswith('('):
84                items = value.strip()[1:-1]
85            else:
86                items = value
87            return [i.strip() for i in items.split(',')]
88        else:
89            return []
Note: See TracBrowser for help on using the repository browser.