Commit | Line | Data |
---|---|---|
44d70096 | 1 | import json |
72c02928 | 2 | import re |
44d70096 VNM |
3 | import os |
4 | import os.path | |
6fa09c14 | 5 | |
4cf86720 | 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 | ||
c075e6dc | 13 | |
0a0ee6db | 14 | def load_config(filepath): |
92be926e VNM |
15 | """ |
16 | Load config from filepath | |
17 | """ | |
ddb1e615 VNM |
18 | with open(filepath) as f: |
19 | content = ''.join(f.readlines()) | |
20 | match = comment_re.search(content) | |
21 | while match: | |
22 | content = content[:match.start()] + content[match.end():] | |
0a0ee6db | 23 | match = comment_re.search(content) |
ddb1e615 | 24 | return json.loads(content) |
6fa09c14 | 25 | |
632c6fa5 O |
26 | # Load default colorset |
27 | c = {} | |
0a0ee6db | 28 | default_config = 'rainbowstream/colorset/default.json' |
632c6fa5 | 29 | data = load_config(default_config) |
ddb1e615 VNM |
30 | for d in data: |
31 | c[d] = data[d] | |
32 | c['theme'] = 'default' | |
632c6fa5 | 33 | # Load user's colorset |
c075e6dc O |
34 | rainbow_config = os.environ.get( |
35 | 'HOME', | |
36 | os.environ.get( | |
37 | 'USERPROFILE', | |
38 | '')) + os.sep + '.rainbow_config.json' | |
ddb1e615 VNM |
39 | try: |
40 | data = load_config(rainbow_config) | |
a5301bc0 VNM |
41 | for d in data: |
42 | c[d] = data[d] | |
ddb1e615 VNM |
43 | c['theme'] = 'custom' |
44 | except: | |
45 | pass |