fix sqlalchemy bug
[rainbowstream.git] / rainbowstream / config.py
1 import json
2 import re
3 import os
4 import os.path
5
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
14 def 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
27 c = {}
28 default_config = 'rainbowstream/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 user's colorset
34 rainbow_config = os.environ.get(
35 'HOME',
36 os.environ.get(
37 'USERPROFILE',
38 '')) + os.sep + '.rainbow_config.json'
39 try:
40 data = load_config(rainbow_config)
41 for d in data:
42 c[d] = data[d]
43 c['theme'] = 'custom'
44 except:
45 pass