fix bug
[rainbowstream.git] / rainbowstream / config.py
CommitLineData
44d70096 1import json
72c02928 2import re
44d70096
VNM
3import os
4import os.path
6fa09c14 5
4cf86720
VNM
6from .db import *
7
8db = RainbowDB()
9
0a0ee6db
VNM
10# Regular expression for comments
11comment_re = re.compile(
12 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
13 re.DOTALL | re.MULTILINE
14)
15
c075e6dc 16
0a0ee6db 17def load_config(filepath):
92be926e
VNM
18 """
19 Load config from filepath
20 """
0a0ee6db
VNM
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)
632c6fa5 28 return json.loads(content)
4cf86720 29 except IOError:
a5301bc0 30 return None
6fa09c14 31
632c6fa5
O
32# Load default colorset
33c = {}
0a0ee6db 34default_config = 'rainbowstream/colorset/default.json'
632c6fa5 35data = load_config(default_config)
a5301bc0
VNM
36if data:
37 for d in data:
38 c[d] = data[d]
8394e34b 39db.theme_store('default')
632c6fa5 40# Load user's colorset
c075e6dc
O
41rainbow_config = os.environ.get(
42 'HOME',
43 os.environ.get(
44 'USERPROFILE',
45 '')) + os.sep + '.rainbow_config.json'
632c6fa5 46data = load_config(rainbow_config)
a5301bc0
VNM
47if data:
48 for d in data:
49 c[d] = data[d]
8394e34b 50 db.theme_update('user')