theme on DB
[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 db.theme_store('user')
29 return json.loads(content)
30 except IOError:
31 db.theme_store('default')
32 pass
33
34 # Load default colorset
35 c = {}
36 default_config = 'rainbowstream/colorset/default.json'
37 data = load_config(default_config)
38 for d in data:
39 c[d] = data[d]
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 for d in data:
48 c[d] = data[d]