check if None
[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)
4cf86720 28 db.theme_store('user')
632c6fa5 29 return json.loads(content)
4cf86720
VNM
30 except IOError:
31 db.theme_store('default')
a5301bc0 32 return None
6fa09c14 33
632c6fa5
O
34# Load default colorset
35c = {}
0a0ee6db 36default_config = 'rainbowstream/colorset/default.json'
632c6fa5 37data = load_config(default_config)
a5301bc0
VNM
38if data:
39 for d in data:
40 c[d] = data[d]
632c6fa5 41# Load user's colorset
c075e6dc
O
42rainbow_config = os.environ.get(
43 'HOME',
44 os.environ.get(
45 'USERPROFILE',
46 '')) + os.sep + '.rainbow_config.json'
632c6fa5 47data = load_config(rainbow_config)
a5301bc0
VNM
48if data:
49 for d in data:
50 c[d] = data[d]