Mercurial > libervia-backend
comparison src/tools/config.py @ 1833:a123e881f9e5
core (config): _dict values are now handled with json syntax
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 23 Jan 2016 20:06:01 +0100 |
parents | d17772b0fe22 |
children | 5b8a859d5bb4 |
comparison
equal
deleted
inserted
replaced
1832:39545dc527a1 | 1833:a123e881f9e5 |
---|---|
28 | 28 |
29 from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError | 29 from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError |
30 from xdg import BaseDirectory | 30 from xdg import BaseDirectory |
31 import os | 31 import os |
32 import csv | 32 import csv |
33 import json | |
33 | 34 |
34 | 35 |
35 def fixConfigOption(section, option, value, silent=True): | 36 def fixConfigOption(section, option, value, silent=True): |
36 """Force a configuration option value, writing it in the first found user | 37 """Force a configuration option value, writing it in the first found user |
37 config file, eventually creating a new user config file if none is found. | 38 config file, eventually creating a new user config file if none is found. |
92 value = os.path.expanduser(value) | 93 value = os.path.expanduser(value) |
93 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) | 94 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) |
94 elif name.endswith('_list'): | 95 elif name.endswith('_list'): |
95 value = csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next() | 96 value = csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next() |
96 elif name.endswith('_dict'): | 97 elif name.endswith('_dict'): |
97 value = dict(csv.reader([item], delimiter=':', quotechar='"', skipinitialspace=True).next() | 98 value = json.loads(value) |
98 for item in csv.reader([value], delimiter=',', quotechar='"', skipinitialspace=True).next()) | 99 if not isinstance(value, dict): |
100 raise ValueError(u"{name} value is not a dict: {value}".format(name=name, value=value)) | |
99 return value | 101 return value |