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.

import os.path


class ConfigParserError(Exception):
    pass


class ConfigParser:

    """
    Simple config file parser utilizing Python's exec() builtin.
    """

    def __init__(self):
        self.options = {}
        self.values = {}

    def add_option(self, name, type=str, default=None):
        """Register option name, type and default value."""
        self.options[name] = {'type':type, 'default':default}
        self.values[name] = default

    def load(self, fname, must_exist=True):
        """Read config file and check loaded values."""
        if not must_exist and not os.path.isfile(fname):
            return
        self.read(fname)
        self.check()

    def read(self, fname):
        """Read config file. Does not check values."""
        with open(fname) as f:
            exec(f.read(), self.values)

    def check(self):
        """Check option values against registered types."""
        for key in self.values.keys():
            if key == '__builtins__':
                continue
            if key in self.options:
                type_ = self.options[key]['type']
                if not isinstance(self.values[key], type_) and not self.values[key] is None:
                    raise ConfigParserError("Bad value of config parameter '%s': type is %s but should be %s"
                        % (key, type(self.values[key]), type_))
            else:
                raise ConfigParserError("Unknown config parameter '%s'." % key)

    def __getattr__(self, name):
        """Attribute access for config parameters.

        cfg.my_param <===> cfg.values['myparam']

        """
        if name in self.values:
            return self.values[name]
        else:
            raise AttributeError()