fix docs
[rainbowstream.git] / rainbowstream / config.py
CommitLineData
44d70096 1import json
72c02928 2import re
44d70096
VNM
3import os
4import os.path
6fa09c14 5
4cf86720 6
0a0ee6db
VNM
7# Regular expression for comments
8comment_re = re.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re.DOTALL | re.MULTILINE
11)
12
c075e6dc 13
0a0ee6db 14def load_config(filepath):
92be926e
VNM
15 """
16 Load config from filepath
17 """
ddb1e615
VNM
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():]
0a0ee6db 23 match = comment_re.search(content)
ddb1e615 24 return json.loads(content)
6fa09c14 25
632c6fa5
O
26# Load default colorset
27c = {}
15f3e155 28default_config = os.path.dirname(__file__) + '/colorset/default.json'
632c6fa5 29data = load_config(default_config)
ddb1e615
VNM
30for d in data:
31 c[d] = data[d]
32c['theme'] = 'default'
632c6fa5 33# Load user's colorset
c075e6dc
O
34rainbow_config = os.environ.get(
35 'HOME',
36 os.environ.get(
37 'USERPROFILE',
38 '')) + os.sep + '.rainbow_config.json'
ddb1e615
VNM
39try:
40 data = load_config(rainbow_config)
a5301bc0
VNM
41 for d in data:
42 c[d] = data[d]
ddb1e615
VNM
43 c['theme'] = 'custom'
44except:
45 pass