term
[rainbowstream.git] / rainbowstream / config.py
... / ...
CommitLineData
1import json
2import re
3import os
4import os.path
5
6
7# Regular expression for comments
8comment_re = re.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re.DOTALL | re.MULTILINE
11)
12
13
14def load_config(filepath):
15 """
16 Load config from filepath
17 """
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():]
23 match = comment_re.search(content)
24 return json.loads(content)
25
26# Load default colorset
27c = {}
28default_config = 'rainbowstream/colorset/default.json'
29data = load_config(default_config)
30for d in data:
31 c[d] = data[d]
32c['theme'] = 'default'
33# Load user's colorset
34rainbow_config = os.environ.get(
35 'HOME',
36 os.environ.get(
37 'USERPROFILE',
38 '')) + os.sep + '.rainbow_config.json'
39try:
40 data = load_config(rainbow_config)
41 for d in data:
42 c[d] = data[d]
43 c['theme'] = 'custom'
44except:
45 pass