~roadmr/ubuntu/precise/checkbox/0.13.1

« back to all changes in this revision

Viewing changes to checkbox/config.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 18:55:20 UTC
  • Revision ID: james.westby@ubuntu.com-20090120185520-s18m2hninrt53fki
Tags: 0.5
* New upstream version:
  * Added concept of hyper text view to display clickable links.
  * Added concept of properties to components.
  * Added pci information to launchpad report.
  * Added dmi information to launchpad report.
  * Added text area to keyboard test.
  * Removed sourcing of base postrm script.
  * Updated translations from Launchpad.
* Fixed handling of interrupt signal (LP: #327810)
* Fixed display of text in graphical interface (LP: #240374)
* Fixed support for regexes in blacklist and whitelist (LP: #327177)
* Fixed opening of subunit log file (LP: #325737)
* Fixed internet test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (c) 2008 Canonical
3
 
#
4
 
# Written by Marc Tardif <marc@interunion.ca>
5
 
#
6
 
# This file is part of Checkbox.
7
 
#
8
 
# Checkbox is free software: you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License as published by
10
 
# the Free Software Foundation, either version 3 of the License, or
11
 
# (at your option) any later version.
12
 
#
13
 
# Checkbox is distributed in the hope that it will be useful,
14
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
# GNU General Public License for more details.
17
 
#
18
 
# You should have received a copy of the GNU General Public License
19
 
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
import os
22
 
import re
23
 
import posixpath
24
 
 
25
 
from ConfigParser import ConfigParser
26
 
 
27
 
 
28
 
class IncludeDict(dict):
29
 
 
30
 
    def __init__(self, parser):
31
 
        super(IncludeDict, self).__init__()
32
 
        self._parser = parser
33
 
 
34
 
        for (key, value) in os.environ.items():
35
 
            super(IncludeDict, self).__setitem__(key.lower(), value)
36
 
 
37
 
    def __setitem__(self, key, value):
38
 
        if key == "includes":
39
 
            for path in re.split(r"\s+", value):
40
 
                path = self._parser._interpolate("DEFAULT", None, path, self)
41
 
                if not posixpath.exists(path):
42
 
                    raise Exception, "No such configuration file: %s" % path
43
 
                self._parser.read(path)
44
 
 
45
 
        # Environment has precedence over configuration
46
 
        elif key.upper() not in os.environ.keys():
47
 
            super(IncludeDict, self).__setitem__(key, value)
48
 
 
49
 
 
50
 
class ConfigSection(object):
51
 
 
52
 
    def __init__(self, parent, name, attributes={}):
53
 
        self.parent = parent
54
 
        self.name = name
55
 
        self.attributes = attributes
56
 
 
57
 
    def _get_value(self, name):
58
 
        return self.attributes.get(name)
59
 
 
60
 
    def __getattr__(self, name):
61
 
        if name in self.attributes:
62
 
            return self._get_value(name)
63
 
 
64
 
        raise AttributeError, name
65
 
 
66
 
 
67
 
class ConfigDefaults(ConfigSection):
68
 
 
69
 
    def _get_value(self, name):
70
 
        return os.environ.get(name.upper()) \
71
 
            or os.environ.get(name.lower()) \
72
 
            or super(ConfigDefaults, self)._get_value(name)
73
 
 
74
 
    def __getattr__(self, name):
75
 
        if name in self.attributes:
76
 
            return self._get_value(name)
77
 
 
78
 
        raise AttributeError, name
79
 
 
80
 
 
81
 
class Config(object):
82
 
 
83
 
    def __init__(self, path, configs=[]):
84
 
        self.path = path
85
 
 
86
 
        self._parser = ConfigParser()
87
 
        self._parser._defaults = IncludeDict(self._parser)
88
 
 
89
 
        if not posixpath.exists(path):
90
 
            raise Exception, "No such configuration file: %s" % path
91
 
        self._parser.read(path)
92
 
 
93
 
        for config in configs:
94
 
            match = re.match("(.*)/([^/]+)=(.*)", config)
95
 
            if not match:
96
 
                raise Exception, "Invalid config string: %s" % config
97
 
 
98
 
            (name, option, value) = match.groups()
99
 
            if not self._parser.has_section(name):
100
 
                self._parser.add_section(name)
101
 
 
102
 
            self._parser.set(name, option, value)
103
 
 
104
 
    def get_defaults(self):
105
 
        attributes = self._parser.defaults()
106
 
        return ConfigDefaults(self, 'DEFAULT', attributes)
107
 
 
108
 
    def get_section(self, name):
109
 
        if self._parser.has_section(name):
110
 
            attributes = dict(self._parser.items(name))
111
 
            return ConfigSection(self, name, attributes)
112
 
 
113
 
        return None
114
 
 
115
 
    def get_section_names(self):
116
 
        return self._parser.sections()