Update index.rst
[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.environ.get(
46 'HOME',
47 os.environ.get(
48 'USERPROFILE',
49 '')) + os.sep + '.rainbow_config.json'
50 data = load_config(path)
51 # Hard to set from prompt
52 data.pop('ONLY_LIST', None)
53 data.pop('IGNORE_LIST', None)
54 return data
55
56
57 def get_default_config(key):
58 """
59 Get default value of a config key
60 """
61 path = os.path.dirname(
62 __file__) + '/colorset/config'
63 try:
64 data = load_config(path)
65 except:
66 raise Exception('No such config key.')
67 return data[key]
68
69 def get_config(key):
70 """
71 Get current value of a config key
72 """
73 return c[key]
74
75
76 def set_config(key, value):
77 """
78 Set a config key with specific value
79 """
80 # Modify value
81 if value.isdigit():
82 value = int(value)
83 elif value.lower() == 'true':
84 value = True
85 elif value.lower() == 'false':
86 value = False
87 # Fix up
88 path = os.environ.get(
89 'HOME',
90 os.environ.get(
91 'USERPROFILE',
92 '')) + os.sep + '.rainbow_config.json'
93 data = load_config(path)
94 if key in data:
95 fixup(data, key, value)
96 else:
97 data[key] = value
98 c[key] = value
99 # Save
100 with open(path, 'w') as out:
101 json.dump(data, out, indent=4)
102 os.system('chmod 777 ' + path)
103
104
105 def delete_config(key):
106 """
107 Delete a config key
108 """
109 path = os.environ.get(
110 'HOME',
111 os.environ.get(
112 'USERPROFILE',
113 '')) + os.sep + '.rainbow_config.json'
114 data = load_config(path)
115 # Drop key
116 if key in data and key in c:
117 data.pop(key)
118 c.pop(key)
119 else:
120 raise Exception('No such config key.')
121 # Save
122 with open(path, 'w') as out:
123 json.dump(data, out, indent=4)
124 os.system('chmod 777 ' + path)
125
126
127 def reload_config():
128 """
129 Reload config
130 """
131 rainbow_config = os.environ.get(
132 'HOME',
133 os.environ.get(
134 'USERPROFILE',
135 '')) + os.sep + '.rainbow_config.json'
136 try:
137 data = load_config(rainbow_config)
138 for d in data:
139 c[d] = data[d]
140 except:
141 print('It seems that ~/.rainbow_config.json has wrong format :(')
142
143
144 def init_config():
145 """
146 Init configuration
147 """
148 # Load the initial config
149 config = os.path.dirname(
150 __file__) + '/colorset/config'
151 try:
152 data = load_config(config)
153 for d in data:
154 c[d] = data[d]
155 except:
156 pass
157 # Load user's config
158 rainbow_config = os.environ.get(
159 'HOME',
160 os.environ.get(
161 'USERPROFILE',
162 '')) + os.sep + '.rainbow_config.json'
163 try:
164 data = load_config(rainbow_config)
165 for d in data:
166 c[d] = data[d]
167 except:
168 print('It seems that ~/.rainbow_config.json has wrong format :(')
169 # Load default theme
170 theme_file = os.path.dirname(
171 __file__) + '/colorset/' + c['THEME'] + '.json'
172 try:
173 data = load_config(theme_file)
174 for d in data:
175 c[d] = data[d]
176 except:
177 pass
178
179
180 # Init config
181 init_config()