-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
80 lines (58 loc) · 1.84 KB
/
utils.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
import os
import json
import yaml
import configparser
class Preferences:
def __init__(self, string_ini):
self.config = configparser.ConfigParser()
self.config.read_string(string_ini)
self.d = self.to_dict(self.config._sections)
def as_dict(self):
return self.d
def to_dict(self, config):
"""
Nested OrderedDict to normal dict.
Also, remove the annoying quotes (apostrophes) from around string values.
"""
d = json.loads(json.dumps(config))
d = remove_quotes(d)
d = {k: v for k, v in d.items() if v}
return d
def handle_sigterm(*args):
raise KeyboardInterrupt()
def normalize_str(string):
string = string.lower()
string = string.replace(' ', '_')
string = ''.join([c for c in string if c.isalpha() or c.isdigit() or c == '_']).rstrip()
return string
def remove_quotes(config):
for key, value in list(config.items()):
# Remove quotes from section
key_strip = key.strip('"')
config[key_strip] = config.pop(key)
if isinstance(value, str):
s = config[key_strip]
# Remove quotes from value
s = s.strip('"')
# Convert strings to numbers
try:
s = int(s)
except ValueError:
pass
config[key_strip] = s
if isinstance(value, dict):
config[key_strip] = remove_quotes(value)
return config
def load_file(path_to_file):
if not os.path.isfile(path_to_file):
return {}
filename, extension = os.path.splitext(path_to_file)
with open(path_to_file) as f:
try:
if 'json' in extension:
data = json.load(f)
else:
data = yaml.safe_load(f)
except Exception:
return {}
return data