183bfe35e84c2b9a604c5847fc2ca81b22b24505
[rainbowstream.git] / rainbowstream / config.py
1 import json
2 import re
3 import os
4 import os.path
5
6 from .db import *
7
8 db = RainbowDB()
9
10 # Regular expression for comments
11 comment_re = re.compile(
12 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
13 re.DOTALL | re.MULTILINE
14 )
15
16
17 def load_config(filepath):
18 """
19 Load config from filepath
20 """
21 with open(filepath) as f:
22 content = ''.join(f.readlines())
23 match = comment_re.search(content)
24 while match:
25 content = content[:match.start()] + content[match.end():]
26 match = comment_re.search(content)
27 return json.loads(content)
28
29 # Load default colorset
30 c = {}
31 default_config = 'rainbowstream/colorset/default.json'
32 data = load_config(default_config)
33 for d in data:
34 c[d] = data[d]
35 c['theme'] = 'default'
36 # Load user's colorset
37 rainbow_config = os.environ.get(
38 'HOME',
39 os.environ.get(
40 'USERPROFILE',
41 '')) + os.sep + '.rainbow_config.json'
42 try:
43 data = load_config(rainbow_config)
44 for d in data:
45 c[d] = data[d]
46 c['theme'] = 'custom'
47 except:
48 pass