pycolib/configparser.py
author Radek Brich <radek.brich@devl.cz>
Wed, 07 May 2014 18:37:13 +0200
changeset 12 d5f749032fa1
parent 7 776ba4914dfc
permissions -rw-r--r--
Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
     1
import os.path
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
class ConfigParserError(Exception):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
    pass
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
class ConfigParser:
12
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
     9
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    10
    """
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    11
    Simple config file parser utilizing Python's exec() builtin.
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    12
    """
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    13
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
    def __init__(self):
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    15
        self.options = {}
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    16
        self.values = {}
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
7
776ba4914dfc Update ConfigParser: rename 'type_' back to 'type'. Rename project in setup.py to make RPM package names more standard.
Radek Brich <brich.radek@ifortuna.cz>
parents: 4
diff changeset
    18
    def add_option(self, name, type=str, default=None):
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    19
        """Register option name, type and default value."""
7
776ba4914dfc Update ConfigParser: rename 'type_' back to 'type'. Rename project in setup.py to make RPM package names more standard.
Radek Brich <brich.radek@ifortuna.cz>
parents: 4
diff changeset
    20
        self.options[name] = {'type':type, 'default':default}
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    21
        self.values[name] = default
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
12
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    23
    def load(self, fname, must_exist=True):
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    24
        """Read config file and check loaded values."""
12
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    25
        if not must_exist and not os.path.isfile(fname):
d5f749032fa1 Update ConfigParser: Add option to load file if exists and do nothing otherwise.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
    26
            return
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    27
        self.read(fname)
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
        self.check()
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    30
    def read(self, fname):
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    31
        """Read config file. Does not check values."""
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
        with open(fname) as f:
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    33
            exec(f.read(), self.values)
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
    def check(self):
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    36
        """Check option values against registered types."""
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    37
        for key in self.values.keys():
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
            if key == '__builtins__':
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
                continue
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    40
            if key in self.options:
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    41
                type_ = self.options[key]['type']
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    42
                if not isinstance(self.values[key], type_) and not self.values[key] is None:
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
                    raise ConfigParserError("Bad value of config parameter '%s': type is %s but should be %s"
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    44
                        % (key, type(self.values[key]), type_))
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
            else:
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
                raise ConfigParserError("Unknown config parameter '%s'." % key)
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
    def __getattr__(self, name):
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
        """Attribute access for config parameters.
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    51
        cfg.my_param <===> cfg.values['myparam']
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        """
4
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    54
        if name in self.values:
fdaa7dc9035e Refactor.
Radek Brich <radek.brich@devl.cz>
parents: 3
diff changeset
    55
            return self.values[name]
3
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
        else:
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
            raise AttributeError()
cc27136cdead Add ConfigParser and tests.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58