Merge pull request #20 from bnrubin/master
[rainbowstream.git] / rainbowstream / config.py
CommitLineData
44d70096 1import json
72c02928 2import re
44d70096
VNM
3import os
4import os.path
1f2f6159 5from collections import OrderedDict
6fa09c14 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
29fd0be6
O
13def fixup(adict, k, v):
14 """
15 Fix up a key in json format
16 """
17 for key in adict.keys():
18 if key == k:
19 adict[key] = v
20 elif type(adict[key]) is dict:
21 fixup(adict[key], k, v)
22
23
0a0ee6db 24def load_config(filepath):
92be926e
VNM
25 """
26 Load config from filepath
27 """
ddb1e615
VNM
28 with open(filepath) as f:
29 content = ''.join(f.readlines())
30 match = comment_re.search(content)
31 while match:
32 content = content[:match.start()] + content[match.end():]
0a0ee6db 33 match = comment_re.search(content)
1f2f6159 34 return json.loads(content, object_pairs_hook=OrderedDict)
6fa09c14 35
29fd0be6
O
36
37def get_all_config():
38 """
39 Get all config
40 """
41 path = os.environ.get(
42 'HOME',
43 os.environ.get(
44 'USERPROFILE',
45 '')) + os.sep + '.rainbow_config.json'
3c01ba57
O
46 data = load_config(path)
47 # Hard to set from prompt
48 data.pop('ONLY_LIST',None)
49 data.pop('IGNORE_LIST',None)
50 return data
29fd0be6
O
51
52
53def get_default_config(key):
54 """
55 Get default value of a config key
56 """
57 path = os.path.dirname(
58 __file__) + '/colorset/config'
59 data = load_config(path)
60 return data[key]
61
62
63def get_config(key):
64 """
65 Get current value of a config key
66 """
67 return c[key]
68
69
70def set_config(key,value):
71 """
72 Set a config key with specific value
73 """
74 # Modify value
75 if value.isdigit():
76 value = int(value)
720123f8 77 elif value.lower() == 'True':
29fd0be6
O
78 value = True
79 elif value.lower() == 'False':
80 value = False
81 # Fix up
82 path = os.environ.get(
83 'HOME',
84 os.environ.get(
85 'USERPROFILE',
86 '')) + os.sep + '.rainbow_config.json'
87 data = load_config(path)
88 fixup(data, key, value)
89 # Save
90 with open(path, 'w') as out:
91 json.dump(data, out, indent = 4)
92 os.system('chmod 777 ' + path)
93
94
95def reload_config():
96 """
97 Reload config
98 """
99 rainbow_config = os.environ.get(
100 'HOME',
101 os.environ.get(
102 'USERPROFILE',
103 '')) + os.sep + '.rainbow_config.json'
104 try:
105 data = load_config(rainbow_config)
106 for d in data:
107 c[d] = data[d]
108 except:
109 print('It seems that ~/.rainbow_config.json has wrong format :(')
110
111
1c8a5082 112# Config dictionary
632c6fa5 113c = {}
1f2f6159 114
687567eb 115# Load the initial config
116config = os.path.dirname(
117 __file__) + '/colorset/config'
118try:
119 data = load_config(config)
120 for d in data:
121 c[d] = data[d]
122except:
123 pass
124
1f2f6159 125# Load user's config
c075e6dc
O
126rainbow_config = os.environ.get(
127 'HOME',
128 os.environ.get(
129 'USERPROFILE',
130 '')) + os.sep + '.rainbow_config.json'
ddb1e615
VNM
131try:
132 data = load_config(rainbow_config)
a5301bc0
VNM
133 for d in data:
134 c[d] = data[d]
1f2f6159 135except:
8b8566d1 136 print('It seems that ~/.rainbow_config.json has wrong format :(')
1f2f6159
O
137
138# Load default theme
139theme_file = os.path.dirname(
140 __file__) + '/colorset/' + c['THEME'] + '.json'
141try:
142 data = load_config(theme_file)
143 for d in data:
144 c[d] = data[d]
ddb1e615
VNM
145except:
146 pass