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 | |
0a0ee6db VNM |
7 | # Regular expression for comments |
8 | comment_re = re.compile( | |
9 | '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', | |
10 | re.DOTALL | re.MULTILINE | |
11 | ) | |
12 | ||
29fd0be6 O |
13 | def fixup(adict, k, v): |
14 | """ | |
15 | Fix up a key in json format | |
16 | """ | |
17 | for key in adict.keys(): | |
18 | if key == k: | |
19 | adict[key] = v | |
20 | elif type(adict[key]) is dict: | |
21 | fixup(adict[key], k, v) | |
22 | ||
23 | ||
0a0ee6db | 24 | def load_config(filepath): |
92be926e VNM |
25 | """ |
26 | Load config from filepath | |
27 | """ | |
ddb1e615 VNM |
28 | with open(filepath) as f: |
29 | content = ''.join(f.readlines()) | |
30 | match = comment_re.search(content) | |
31 | while match: | |
32 | content = content[:match.start()] + content[match.end():] | |
0a0ee6db | 33 | match = comment_re.search(content) |
1f2f6159 | 34 | return json.loads(content, object_pairs_hook=OrderedDict) |
6fa09c14 | 35 | |
29fd0be6 O |
36 | |
37 | def get_all_config(): | |
38 | """ | |
39 | Get all config | |
40 | """ | |
41 | path = os.environ.get( | |
42 | 'HOME', | |
43 | os.environ.get( | |
44 | 'USERPROFILE', | |
45 | '')) + os.sep + '.rainbow_config.json' | |
46 | return load_config(path) | |
47 | ||
48 | ||
49 | def get_default_config(key): | |
50 | """ | |
51 | Get default value of a config key | |
52 | """ | |
53 | path = os.path.dirname( | |
54 | __file__) + '/colorset/config' | |
55 | data = load_config(path) | |
56 | return data[key] | |
57 | ||
58 | ||
59 | def get_config(key): | |
60 | """ | |
61 | Get current value of a config key | |
62 | """ | |
63 | return c[key] | |
64 | ||
65 | ||
66 | def set_config(key,value): | |
67 | """ | |
68 | Set a config key with specific value | |
69 | """ | |
70 | # Modify value | |
71 | if value.isdigit(): | |
72 | value = int(value) | |
73 | if value.lower() == 'True': | |
74 | value = True | |
75 | elif value.lower() == 'False': | |
76 | value = False | |
77 | # Fix up | |
78 | path = os.environ.get( | |
79 | 'HOME', | |
80 | os.environ.get( | |
81 | 'USERPROFILE', | |
82 | '')) + os.sep + '.rainbow_config.json' | |
83 | data = load_config(path) | |
84 | fixup(data, key, value) | |
85 | # Save | |
86 | with open(path, 'w') as out: | |
87 | json.dump(data, out, indent = 4) | |
88 | os.system('chmod 777 ' + path) | |
89 | ||
90 | ||
91 | def reload_config(): | |
92 | """ | |
93 | Reload config | |
94 | """ | |
95 | rainbow_config = os.environ.get( | |
96 | 'HOME', | |
97 | os.environ.get( | |
98 | 'USERPROFILE', | |
99 | '')) + os.sep + '.rainbow_config.json' | |
100 | try: | |
101 | data = load_config(rainbow_config) | |
102 | for d in data: | |
103 | c[d] = data[d] | |
104 | except: | |
105 | print('It seems that ~/.rainbow_config.json has wrong format :(') | |
106 | ||
107 | ||
1c8a5082 | 108 | # Config dictionary |
632c6fa5 | 109 | c = {} |
1f2f6159 | 110 | |
687567eb | 111 | # Load the initial config |
112 | config = os.path.dirname( | |
113 | __file__) + '/colorset/config' | |
114 | try: | |
115 | data = load_config(config) | |
116 | for d in data: | |
117 | c[d] = data[d] | |
118 | except: | |
119 | pass | |
120 | ||
1f2f6159 | 121 | # Load user's config |
c075e6dc O |
122 | rainbow_config = os.environ.get( |
123 | 'HOME', | |
124 | os.environ.get( | |
125 | 'USERPROFILE', | |
126 | '')) + os.sep + '.rainbow_config.json' | |
ddb1e615 VNM |
127 | try: |
128 | data = load_config(rainbow_config) | |
a5301bc0 VNM |
129 | for d in data: |
130 | c[d] = data[d] | |
1f2f6159 | 131 | except: |
8b8566d1 | 132 | print('It seems that ~/.rainbow_config.json has wrong format :(') |
1f2f6159 O |
133 | |
134 | # Load default theme | |
135 | theme_file = os.path.dirname( | |
136 | __file__) + '/colorset/' + c['THEME'] + '.json' | |
137 | try: | |
138 | data = load_config(theme_file) | |
139 | for d in data: | |
140 | c[d] = data[d] | |
ddb1e615 VNM |
141 | except: |
142 | pass |