Commit | Line | Data |
---|---|---|
44d70096 | 1 | import json |
72c02928 | 2 | import re |
44d70096 VNM |
3 | import os |
4 | import os.path | |
6fa09c14 | 5 | |
0a0ee6db VNM |
6 | # Regular expression for comments |
7 | comment_re = re.compile( | |
8 | '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', | |
9 | re.DOTALL | re.MULTILINE | |
10 | ) | |
11 | ||
c075e6dc | 12 | |
0a0ee6db | 13 | def load_config(filepath): |
92be926e VNM |
14 | """ |
15 | Load config from filepath | |
16 | """ | |
ddb1e615 VNM |
17 | with open(filepath) as f: |
18 | content = ''.join(f.readlines()) | |
19 | match = comment_re.search(content) | |
20 | while match: | |
21 | content = content[:match.start()] + content[match.end():] | |
0a0ee6db | 22 | match = comment_re.search(content) |
ddb1e615 | 23 | return json.loads(content) |
6fa09c14 | 24 | |
1c8a5082 | 25 | # Config dictionary |
632c6fa5 | 26 | c = {} |
1c8a5082 | 27 | # Load default |
15f3e155 | 28 | default_config = os.path.dirname(__file__) + '/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' | |
1c8a5082 O |
33 | # Load init if exist |
34 | try: | |
35 | path = os.path.dirname(__file__) + '/colorset/init' | |
36 | f = open(path) | |
37 | lines = f.readlines() | |
38 | if len(lines) > 1: | |
39 | raise Exception('More than 1 default theme') | |
40 | theme_name = lines[0].strip() | |
422dd385 O |
41 | default_config = os.path.dirname( |
42 | __file__) + '/colorset/' + theme_name + '.json' | |
1c8a5082 O |
43 | data = load_config(default_config) |
44 | for d in data: | |
45 | c[d] = data[d] | |
46 | c['theme'] = theme_name | |
47 | f.close() | |
48 | except: | |
49 | pass | |
632c6fa5 | 50 | # Load user's colorset |
c075e6dc O |
51 | rainbow_config = os.environ.get( |
52 | 'HOME', | |
53 | os.environ.get( | |
54 | 'USERPROFILE', | |
55 | '')) + os.sep + '.rainbow_config.json' | |
ddb1e615 VNM |
56 | try: |
57 | data = load_config(rainbow_config) | |
a5301bc0 VNM |
58 | for d in data: |
59 | c[d] = data[d] | |
ddb1e615 VNM |
60 | c['theme'] = 'custom' |
61 | except: | |
62 | pass |