| 1 | import json |
| 2 | import re |
| 3 | import os |
| 4 | import os.path |
| 5 | |
| 6 | # Regular expression for comments |
| 7 | comment_re = re.compile( |
| 8 | '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', |
| 9 | re.DOTALL | re.MULTILINE |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | def load_config(filepath): |
| 14 | """ |
| 15 | Load config from filepath |
| 16 | """ |
| 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():] |
| 22 | match = comment_re.search(content) |
| 23 | return json.loads(content) |
| 24 | |
| 25 | # Config dictionary |
| 26 | c = {} |
| 27 | # Load default |
| 28 | default_config = os.path.dirname(__file__) + '/colorset/default.json' |
| 29 | data = load_config(default_config) |
| 30 | for d in data: |
| 31 | c[d] = data[d] |
| 32 | c['theme'] = 'default' |
| 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() |
| 41 | default_config = os.path.dirname(__file__)+'/colorset/'+theme_name+'.json' |
| 42 | data = load_config(default_config) |
| 43 | for d in data: |
| 44 | c[d] = data[d] |
| 45 | c['theme'] = theme_name |
| 46 | f.close() |
| 47 | except: |
| 48 | pass |
| 49 | # Load user's colorset |
| 50 | rainbow_config = os.environ.get( |
| 51 | 'HOME', |
| 52 | os.environ.get( |
| 53 | 'USERPROFILE', |
| 54 | '')) + os.sep + '.rainbow_config.json' |
| 55 | try: |
| 56 | data = load_config(rainbow_config) |
| 57 | for d in data: |
| 58 | c[d] = data[d] |
| 59 | c['theme'] = 'custom' |
| 60 | except: |
| 61 | pass |