| 1 | import json |
| 2 | import re |
| 3 | import os |
| 4 | import os.path |
| 5 | from collections import OrderedDict |
| 6 | |
| 7 | # Regular expression for comments |
| 8 | comment_re = re.compile( |
| 9 | '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?', |
| 10 | re.DOTALL | re.MULTILINE |
| 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, object_pairs_hook=OrderedDict) |
| 24 | |
| 25 | # Config dictionary |
| 26 | c = {} |
| 27 | |
| 28 | # Load the initial config |
| 29 | config = os.path.dirname( |
| 30 | __file__) + '/colorset/config' |
| 31 | try: |
| 32 | data = load_config(config) |
| 33 | for d in data: |
| 34 | c[d] = data[d] |
| 35 | except: |
| 36 | pass |
| 37 | |
| 38 | # Load user's config |
| 39 | rainbow_config = os.environ.get( |
| 40 | 'HOME', |
| 41 | os.environ.get( |
| 42 | 'USERPROFILE', |
| 43 | '')) + os.sep + '.rainbow_config.json' |
| 44 | try: |
| 45 | data = load_config(rainbow_config) |
| 46 | for d in data: |
| 47 | c[d] = data[d] |
| 48 | except: |
| 49 | print('It seems that ~/.rainbow_config.json has wrong format :(') |
| 50 | |
| 51 | # Load default theme |
| 52 | theme_file = os.path.dirname( |
| 53 | __file__) + '/colorset/' + c['THEME'] + '.json' |
| 54 | try: |
| 55 | data = load_config(theme_file) |
| 56 | for d in data: |
| 57 | c[d] = data[d] |
| 58 | except: |
| 59 | pass |