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