autopep8
[rainbowstream.git] / rainbowstream / config.py
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 try:
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 except:
26 pass
27
28 # Load default colorset
29 c = {}
30 default_config = 'rainbowstream/colorset/default.json'
31 data = load_config(default_config)
32 for d in data:
33 c[d] = data[d]
34 # Load user's colorset
35 rainbow_config = os.environ.get(
36 'HOME',
37 os.environ.get(
38 'USERPROFILE',
39 '')) + os.sep + '.rainbow_config.json'
40 data = load_config(rainbow_config)
41 for d in data:
42 c[d] = data[d]