~pieq/checkbox/add-30suspend-1reboot-cycles-support

« back to all changes in this revision

Viewing changes to hwtest/config.py

  • Committer: Marc Tardif
  • Date: 2007-10-04 23:12:10 UTC
  • Revision ID: marc.tardif@canonical.com-20071004231210-unxckndkgndxfdp6
Refactored questions to use templates and scripts which fixes bug #149195.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from ConfigParser import ConfigParser
 
3
 
 
4
 
 
5
class ConfigSection(object):
 
6
 
 
7
    def __init__(self, **kwargs):
 
8
        self._kwargs = kwargs
 
9
 
 
10
    def _get_value(self, attr):
 
11
        return os.environ.get(attr.upper()) \
 
12
            or os.environ.get(attr.lower()) \
 
13
            or self._kwargs.get(attr.upper()) \
 
14
            or self._kwargs.get(attr.lower())
 
15
 
 
16
    def __getattr__(self, attr):
 
17
        if attr in self._kwargs:
 
18
            return self._get_value(attr)
 
19
 
 
20
        raise AttributeError, attr
 
21
 
 
22
 
 
23
class Config(object):
 
24
 
 
25
    def __init__(self, config_parser=None):
 
26
        self._parser = config_parser or ConfigParser()
 
27
        self.sections = {}
 
28
 
 
29
    def load_directory(self, directory):
 
30
        for name in [name for name in os.listdir(directory)
 
31
                     if name.endswith(".conf")]:
 
32
            self.load_path(os.path.join(directory, name))
 
33
 
 
34
    def load_path(self, path):
 
35
        self._parser.read(path)
 
36
 
 
37
    def get_section(self, section):
 
38
        if section in self._parser.sections():
 
39
            kwargs = dict(self._parser.items(section))
 
40
            return ConfigSection(**kwargs)
 
41
 
 
42
        return None
 
43
 
 
44
    def get_section_names(self):
 
45
        return self._parser.sections()