generate config file by default
[rainbowstream.git] / rainbowstream / config.py
... / ...
CommitLineData
1import json
2import re
3import os
4import os.path
5from collections import OrderedDict
6
7# Regular expression for comments
8comment_re = re.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re.DOTALL | re.MULTILINE
11)
12
13def load_config(filepath):
14 """
15 Load config from filepath
16 """
17 with open(filepath) as f:
18 content = ''.join(f.readlines())
19 match = comment_re.search(content)
20 while match:
21 content = content[:match.start()] + content[match.end():]
22 match = comment_re.search(content)
23 return json.loads(content, object_pairs_hook=OrderedDict)
24
25# Config dictionary
26c = {}
27
28# Load user's config
29rainbow_config = os.environ.get(
30 'HOME',
31 os.environ.get(
32 'USERPROFILE',
33 '')) + os.sep + '.rainbow_config.json'
34try:
35 data = load_config(rainbow_config)
36 for d in data:
37 c[d] = data[d]
38except:
39 pass
40
41# Load default theme
42theme_file = os.path.dirname(
43 __file__) + '/colorset/' + c['THEME'] + '.json'
44try:
45 data = load_config(theme_file)
46 for d in data:
47 c[d] = data[d]
48except:
49 pass