697361d5369adfe51bba161fa88599a9d8f23914
[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 try:
22 with open(filepath) as f:
23 content = ''.join(f.readlines())
24 match = comment_re.search(content)
25 while match:
26 content = content[:match.start()] + content[match.end():]
27 match = comment_re.search(content)
28 return json.loads(content)
29 except IOError:
30 return None
31
32 # Load default colorset
33 c = {}
34 default_config = 'rainbowstream/colorset/default.json'
35 data = load_config(default_config)
36 if data:
37 for d in data:
38 c[d] = data[d]
39 db.theme_store('default')
40 # Load user's colorset
41 rainbow_config = os.environ.get(
42 'HOME',
43 os.environ.get(
44 'USERPROFILE',
45 '')) + os.sep + '.rainbow_config.json'
46 data = load_config(rainbow_config)
47 if data:
48 for d in data:
49 c[d] = data[d]
50 db.theme_update('user')