-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsnipsTools.py
45 lines (36 loc) · 1.28 KB
/
snipsTools.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
import io
import configparser
CONFIGURATION_ENCODING_FORMAT = "utf-8"
class SnipsConfigParser(configparser.SafeConfigParser):
def to_dict(self):
return {
section: {
optname: option for optname, option in self.items(section)
} for section in self.sections()
}
@staticmethod
def read_configuration_file(configuration_file):
try:
with io.open(
configuration_file,
encoding=CONFIGURATION_ENCODING_FORMAT) as f:
conf_parser = SnipsConfigParser()
conf_parser.readfp(f)
return conf_parser.to_dict()
except (IOError, configparser.Error) as e:
print(e)
return dict()
@staticmethod
def write_configuration_file(configuration_file, data):
conf_parser = SnipsConfigParser()
for key in data.keys():
conf_parser.add_section(key)
for inner_key in data[key].keys():
conf_parser.set(key, inner_key, data[key][inner_key])
try:
with open(configuration_file, 'w') as f:
conf_parser.write(f)
return True
except (IOError, configparser.Error) as e:
print(e)
return False