forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigparser.py
87 lines (72 loc) · 3.09 KB
/
configparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Written by Egbert Bouman
# see LICENSE.txt for license information
import ast
import codecs
from ConfigParser import DEFAULTSECT, RawConfigParser
from threading import RLock
from Tribler.Core.exceptions import OperationNotPossibleAtRuntimeException
class CallbackConfigParser(RawConfigParser):
def __init__(self, *args, **kwargs):
RawConfigParser.__init__(self, *args, **kwargs)
self.filename = None
self.callback = None
self.lock = RLock()
def set_callback(self, callback):
with self.lock:
self.callback = callback
def read_file(self, filename, encoding='utf-8'):
self.filename = filename
with codecs.open(filename, 'rb', encoding) as fp:
self.readfp(fp)
def set(self, section, option, new_value):
with self.lock:
if self.callback and self.has_section(section) and self.has_option(section, option):
old_value = self.get(section, option)
if not self.callback(section, option, new_value, old_value):
raise OperationNotPossibleAtRuntimeException
RawConfigParser.set(self, section, option, new_value)
def get(self, section, option, literal_eval=True):
value = RawConfigParser.get(self, section, option) if RawConfigParser.has_option(
self, section, option) else None
if literal_eval:
return CallbackConfigParser.get_literal_value(value)
return value
def copy(self):
with self.lock:
copied_config = CallbackConfigParser()
for section in self.sections():
copied_config.add_section(section)
for option, value in self.items(section):
copied_config.set(section, option, value)
return copied_config
def write_file(self, filename=None, encoding='utf-8'):
if not filename:
filename = self.filename
with codecs.open(filename, 'wb', encoding) as fp:
self.write(fp)
def write(self, fp):
with self.lock:
if self._defaults:
fp.write(u"[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write(u"%s = %s\n" % (key, unicode(value).replace(u'\n', u'\n\t')))
fp.write(u"\n")
for section in self._sections:
fp.write(u"[%s]\n" % section)
for (key, value) in self._sections[section].items():
if key != u"__name__":
fp.write(u"%s = %s\n" % (key, unicode(value).replace(u'\n', u'\n\t')))
fp.write(u"\n")
@staticmethod
def get_literal_value(value):
try:
return ast.literal_eval(value)
except (ValueError, SyntaxError):
return value
def get_config_as_json(self):
json_dict = {}
for section in self.sections():
json_dict[section] = {}
for option, value in self.items(section):
json_dict[section][option] = CallbackConfigParser.get_literal_value(value)
return json_dict