# HG changeset patch # User Goffi # Date 1453656488 -3600 # Node ID 5b8a859d5bb4c5cd605b08c705fdd213999be4e1 # Parent 6209de5e3e25d13599713a296ca1ed9f856be5a6 core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem diff -r 6209de5e3e25 -r 5b8a859d5bb4 src/tools/config.py --- a/src/tools/config.py Sun Jan 24 18:24:01 2016 +0100 +++ b/src/tools/config.py Sun Jan 24 18:28:08 2016 +0100 @@ -25,6 +25,7 @@ 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 @@ @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 @@ 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