core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
This commit is contained in:
parent
13d24075c1
commit
04a42fa9a3
1 changed files with 9 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue