delete db file if exist on startup
[rainbowstream.git] / rainbowstream / config.py
1 import json
2 import re
3 import os
4 import os.path
5 from collections import OrderedDict
6
7 # Regular expression for comments in config file
8 comment_re = re.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re.DOTALL | re.MULTILINE
11 )
12
13 # Config dictionary
14 c = {}
15
16
17 def fixup(adict, k, v):
18 """
19 Fix up a key in json format
20 """
21 for key in adict.keys():
22 if key == k:
23 adict[key] = v
24 elif isinstance(adict[key], dict):
25 fixup(adict[key], k, v)
26
27
28 def load_config(filepath):
29 """
30 Load config from filepath
31 """
32 with open(filepath) as f:
33 content = ''.join(f.readlines())
34 match = comment_re.search(content)
35 while match:
36 content = content[:match.start()] + content[match.end():]
37 match = comment_re.search(content)
38 return json.loads(content, object_pairs_hook=OrderedDict)
39
40
41 def get_all_config():
42 """
43 Get all config
44 """
45 path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
46 data = load_config(path)
47 # Hard to set from prompt
48 data.pop('ONLY_LIST', None)
49 data.pop('IGNORE_LIST', None)
50 return data
51
52
53 def get_default_config(key):
54 """
55 Get default value of a config key
56 """
57 path = os.path.dirname(
58 __file__) + '/colorset/config'
59 try:
60 data = load_config(path)
61 except:
62 raise Exception('No such config key.')
63 return data[key]
64
65
66 def get_config(key):
67 """
68 Get current value of a config key
69 """
70 return c[key]
71
72
73 def set_config(key, value):
74 """
75 Set a config key with specific value
76 """
77 # Modify value
78 if value.isdigit():
79 value = int(value)
80 elif value.lower() == 'true':
81 value = True
82 elif value.lower() == 'false':
83 value = False
84 # Fix up
85 path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
86 data = {}
87 try:
88 data = load_config(path)
89 except:
90 pass
91 # Update global config
92 c[key] = value
93 # Update config file
94 if key in data:
95 fixup(data, key, value)
96 else:
97 data[key] = value
98 # Save
99 with open(path, 'w') as out:
100 json.dump(data, out, indent=4)
101 os.system('chmod 777 ' + path)
102
103
104 def delete_config(key):
105 """
106 Delete a config key
107 """
108 path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
109 data = load_config(path)
110 # Drop key
111 if key in data and key in c:
112 data.pop(key)
113 c.pop(key)
114 try:
115 data[key] = c[key] = get_default_config(key)
116 except:
117 pass
118 else:
119 raise Exception('No such config key.')
120 # Save
121 with open(path, 'w') as out:
122 json.dump(data, out, indent=4)
123 os.system('chmod 777 ' + path)
124
125
126 def reload_config():
127 """
128 Reload config
129 """
130 rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
131 try:
132 data = load_config(rainbow_config)
133 for d in data:
134 c[d] = data[d]
135 except:
136 print('It seems that ~/.rainbow_config.json has wrong format :(')
137
138
139 def init_config():
140 """
141 Init configuration
142 """
143 # Load the initial config
144 config = os.path.dirname(
145 __file__) + '/colorset/config'
146 try:
147 data = load_config(config)
148 for d in data:
149 c[d] = data[d]
150 except:
151 pass
152 # Load user's config
153 rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
154 try:
155 data = load_config(rainbow_config)
156 for d in data:
157 c[d] = data[d]
158 except:
159 pass
160 # Load default theme
161 theme_file = os.path.dirname(
162 __file__) + '/colorset/' + c['THEME'] + '.json'
163 try:
164 data = load_config(theme_file)
165 for d in data:
166 c[d] = data[d]
167 except:
168 pass
169
170
171 # Init config
172 init_config()