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