changeset 1835:5b8a859d5bb4

core (config): getConfig now returns unicode and raise exceptions.ParsingError in case of parsing problem
author Goffi <goffi@goffi.org>
date Sun, 24 Jan 2016 18:28:08 +0100
parents 6209de5e3e25
children fb94f92dc740
files src/tools/config.py
diffstat 1 files changed, 9 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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