diff options
Diffstat (limited to 'olpcfr/config.py')
-rw-r--r-- | olpcfr/config.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/olpcfr/config.py b/olpcfr/config.py new file mode 100644 index 0000000..9180e54 --- /dev/null +++ b/olpcfr/config.py @@ -0,0 +1,111 @@ + +# python import +import logging +from ConfigParser import SafeConfigParser + +# get application logger +logger = logging.getLogger('atoideweb') + + +class Config(object): + + class __Singleton: + """Our singleton object. + """ + + def __init__(self, config=None): + """Create the new singleton with the application config. + + :param config: SafeConfigParser object for all the application + :see: `ConfigParser.SafeConfigParser` + """ + # ensure config + if config is None: + # ... + self.__config = SafeConfigParser() + # ... + self.__config.read('config.ini') + # ... + else: + self.__config = config + + def set(self, path, value, type_=str): + # set value + self.set_value(*path.split(">"), value=value, type_=type_) + + def get(self, path, type_=str): + """A little jQuery like shortcut for the `get_value` method. + + :param path: something like "section>option" + :param type_: type of the expected value. Default: str + :return: expected value in the expected type or None + :rtype: `object` + """ + # get value + _value = self.get_value(*path.split(">"), type_=type_) + # check and return + return None if _value is None or _value == "" else _value + + def set_value(self, section, option, value=None, type_=str): + # check has config + if self.__config is None: + return + # ensure section + if self.__config.has_section(section): + pass + else: + self.__config.add_section(section) + # do set + self.__config.set(section, option, value) + + def get_value(self, section, option, type_=str): + """Simple config value getter to avoid exception risk when getting + a config value. If the section and option exist, returns the + corresponding value, None otherwise. + + The `type_` parameter specify the expected option type and + return. + + :param section: section name of the expected value + :param option: option name name of the expected value + :param type_: type of the expected value. Default: str + :return: expected value in the expected type or None + :rtype: `object` + """ + # check has config + if self.__config is None: + return None + # check value exist + elif self.__config.has_section(section) \ + and self.__config.has_option(section, option): + # check expected value type + if type_ is int: + return self.__config.getint(section, option) + elif type_ is bool: + return self.__config.getboolean(section, option) + elif type_ is str: + return self.__config.get(section, option) + elif type_ is list: + _v = self.__config.get(section, option) + return _v.split(' ') + # unexpected type ?? + else: + return None + # value does not exist + else: + # do nothing + return None + + # singleton instance + instance = None + + def __new__(c, config=None, force=False): + """Singleton new init. + """ + # if doesn't already initialized + if not Config.instance \ + or force is True: + # create a new instance + Config.instance = Config.__Singleton(config=config) + # return the manager object + return Config.instance |