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