diff options
Diffstat (limited to 'atoidejouerbeta/tools/config.py')
-rw-r--r-- | atoidejouerbeta/tools/config.py | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/atoidejouerbeta/tools/config.py b/atoidejouerbeta/tools/config.py new file mode 100644 index 0000000..939d6f7 --- /dev/null +++ b/atoidejouerbeta/tools/config.py @@ -0,0 +1,166 @@ + +# python import +import logging +from ConfigParser import SafeConfigParser + +# atoidejouerbeta import +from atoidejouerbeta.tools import storage + +# get application logger +logger = logging.getLogger('atoidejouerbeta') + +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(storage.get_config_path()) + else: + self.__config = config + + def get_mode(self): + # get mode value + _mode = self.get('activity>mode') + # ensure mode + if _mode in ['easy', 'advanced']: + pass + else: + # is easy by default + _mode = 'easy' + # ensure config for further use + self.set('activity>mode', _mode) + # return it + return _mode + + def get_rate(self): + # get rate value + _rate = self.get('activity>rate') + # ensure rate + if _rate in ['normal', 'smooth', 'faster']: + pass + else: + # is easy by default + _rate = 'normal' + # ensure config for further use + self.set('activity>rate', _rate) + # return it + return _rate + + def get_rate_value(self): + # .. + _rate = self.get_rate() + # simple rate factory + if _rate == 'faster': + return 0.2 + elif _rate == 'smooth': + return 0.5 + else: + return 1.0 + + def get_dnd(self): + # get dnd value + _dnd = self.get('activity>dnd') + # ensure dnd + if _dnd in ['yes', 'no']: + pass + else: + # is no by default + _dnd = 'no' + # ensure config for further use + self.set('activity>dnd', _dnd) + # return it + return _dnd + + def use_dnd(self): + return self.get_dnd() == 'yes' + + 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) + # return the manager object + return Config.instance |