| 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 | """ |
|---|
| 9 | Handle the configuration details. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from ConfigParser import SafeConfigParser |
|---|
| 15 | |
|---|
| 16 | class ConfigurationError(Exception): |
|---|
| 17 | """Configuration error""" |
|---|
| 18 | |
|---|
| 19 | class 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 [] |
|---|