diff --git a/src/tools/config.py b/src/tools/config.py index c598c2f9..9f25a82c 100644 --- a/src/tools/config.py +++ b/src/tools/config.py @@ -25,6 +25,7 @@ log = getLogger(__name__) from sat.core.constants import Const as C from sat.core.i18n import _ +from sat.core import exceptions from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError from xdg import BaseDirectory @@ -75,15 +76,16 @@ def getConfig(config, section, name, default=None): @param section (str): section of the config file (None or '' for DEFAULT) @param name (str): name of the option @param default: value to use if not found, or Exception to raise an exception - @return: str, list or dict + @return (unicode, list, dict): parsed value @raise: NoOptionError if option is not present and default is Exception NoSectionError if section doesn't exists and default is Exception + exceptions.ParsingError error while parsing value """ if not section: section = DEFAULTSECT try: - value = config.get(section, name) + value = config.get(section, name).decode('utf-8') except (NoOptionError, NoSectionError) as e: if default is Exception: raise e @@ -95,7 +97,10 @@ def getConfig(config, section, name, default=None): elif name.endswith('_list'): value = csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next() elif name.endswith('_dict'): - value = json.loads(value) + try: + value = json.loads(value) + except ValueError as e: + raise exceptions.ParsingError(u"Error while parsing data: {}".format(e)) if not isinstance(value, dict): - raise ValueError(u"{name} value is not a dict: {value}".format(name=name, value=value)) + raise exceptions.ParsingError(u"{name} value is not a dict: {value}".format(name=name, value=value)) return value