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 VNM |
8 | comment_re = re.compile( |
9 | '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', | |
10 | re.DOTALL | re.MULTILINE | |
11 | ) | |
12 | ||
531f5682 O |
13 | # Config dictionary |
14 | c = {} | |
15 | ||
a8c5fce4 | 16 | |
29fd0be6 O |
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 | |
a8c5fce4 | 24 | elif isinstance(adict[key], dict): |
29fd0be6 O |
25 | fixup(adict[key], k, v) |
26 | ||
27 | ||
0a0ee6db | 28 | def 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 | |
41 | def 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) | |
3c01ba57 | 50 | return data |
29fd0be6 O |
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' | |
fe9bb33b | 59 | try: |
60 | data = load_config(path) | |
61 | except: | |
ceec8593 | 62 | raise Exception('No such config key.') |
29fd0be6 O |
63 | return data[key] |
64 | ||
ceec8593 | 65 | |
29fd0be6 O |
66 | def get_config(key): |
67 | """ | |
68 | Get current value of a config key | |
69 | """ | |
70 | return c[key] | |
71 | ||
72 | ||
a8c5fce4 | 73 | def set_config(key, value): |
29fd0be6 O |
74 | """ |
75 | Set a config key with specific value | |
76 | """ | |
77 | # Modify value | |
78 | if value.isdigit(): | |
79 | value = int(value) | |
fe9bb33b | 80 | elif value.lower() == 'true': |
29fd0be6 | 81 | value = True |
fe9bb33b | 82 | elif value.lower() == 'false': |
29fd0be6 O |
83 | value = False |
84 | # Fix up | |
8619b00b | 85 | path = os.path.expanduser("~") + os.sep + '.rainbow_config.json' |
63388de7 O |
86 | data = {} |
87 | try: | |
88 | data = load_config(path) | |
89 | except: | |
90 | pass | |
ceec8593 | 91 | # Update global config |
92 | c[key] = value | |
93 | # Update config file | |
fe9bb33b | 94 | if key in data: |
95 | fixup(data, key, value) | |
96 | else: | |
97 | data[key] = value | |
29fd0be6 O |
98 | # Save |
99 | with open(path, 'w') as out: | |
a8c5fce4 | 100 | json.dump(data, out, indent=4) |
29fd0be6 O |
101 | os.system('chmod 777 ' + path) |
102 | ||
103 | ||
fe9bb33b | 104 | def delete_config(key): |
ceec8593 | 105 | """ |
106 | Delete a config key | |
107 | """ | |
8619b00b | 108 | path = os.path.expanduser("~") + os.sep + '.rainbow_config.json' |
ceec8593 | 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) | |
fe9bb33b | 124 | |
125 | ||
29fd0be6 O |
126 | def reload_config(): |
127 | """ | |
128 | Reload config | |
129 | """ | |
8619b00b | 130 | rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json' |
29fd0be6 O |
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 | ||
531f5682 O |
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 | |
8619b00b | 153 | rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json' |
531f5682 O |
154 | try: |
155 | data = load_config(rainbow_config) | |
156 | for d in data: | |
157 | c[d] = data[d] | |
158 | except: | |
63388de7 | 159 | pass |
531f5682 O |
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 | ||
1f2f6159 | 170 | |
531f5682 | 171 | # Init config |
fe9bb33b | 172 | init_config() |