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