Mercurial > libervia-backend
comparison src/tools/config.py @ 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 | a123e881f9e5 |
children | ac2ac7fe8a9b |
comparison
equal
deleted
inserted
replaced
1834:6209de5e3e25 | 1835:5b8a859d5bb4 |
---|---|
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 log = getLogger(__name__) | 24 log = getLogger(__name__) |
25 | 25 |
26 from sat.core.constants import Const as C | 26 from sat.core.constants import Const as C |
27 from sat.core.i18n import _ | 27 from sat.core.i18n import _ |
28 from sat.core import exceptions | |
28 | 29 |
29 from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError | 30 from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError |
30 from xdg import BaseDirectory | 31 from xdg import BaseDirectory |
31 import os | 32 import os |
32 import csv | 33 import csv |
73 | 74 |
74 @param config (SafeConfigParser): the configuration instance | 75 @param config (SafeConfigParser): the configuration instance |
75 @param section (str): section of the config file (None or '' for DEFAULT) | 76 @param section (str): section of the config file (None or '' for DEFAULT) |
76 @param name (str): name of the option | 77 @param name (str): name of the option |
77 @param default: value to use if not found, or Exception to raise an exception | 78 @param default: value to use if not found, or Exception to raise an exception |
78 @return: str, list or dict | 79 @return (unicode, list, dict): parsed value |
79 @raise: NoOptionError if option is not present and default is Exception | 80 @raise: NoOptionError if option is not present and default is Exception |
80 NoSectionError if section doesn't exists and default is Exception | 81 NoSectionError if section doesn't exists and default is Exception |
82 exceptions.ParsingError error while parsing value | |
81 """ | 83 """ |
82 if not section: | 84 if not section: |
83 section = DEFAULTSECT | 85 section = DEFAULTSECT |
84 | 86 |
85 try: | 87 try: |
86 value = config.get(section, name) | 88 value = config.get(section, name).decode('utf-8') |
87 except (NoOptionError, NoSectionError) as e: | 89 except (NoOptionError, NoSectionError) as e: |
88 if default is Exception: | 90 if default is Exception: |
89 raise e | 91 raise e |
90 return default | 92 return default |
91 | 93 |
93 value = os.path.expanduser(value) | 95 value = os.path.expanduser(value) |
94 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) | 96 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) |
95 elif name.endswith('_list'): | 97 elif name.endswith('_list'): |
96 value = csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next() | 98 value = csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next() |
97 elif name.endswith('_dict'): | 99 elif name.endswith('_dict'): |
98 value = json.loads(value) | 100 try: |
101 value = json.loads(value) | |
102 except ValueError as e: | |
103 raise exceptions.ParsingError(u"Error while parsing data: {}".format(e)) | |
99 if not isinstance(value, dict): | 104 if not isinstance(value, dict): |
100 raise ValueError(u"{name} value is not a dict: {value}".format(name=name, value=value)) | 105 raise exceptions.ParsingError(u"{name} value is not a dict: {value}".format(name=name, value=value)) |
101 return value | 106 return value |