d481dcd31e358a850e585996b0b8850b6201d964
[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 def 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
24 def load_config(filepath):
25 """
26 Load config from filepath
27 """
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():]
33 match = comment_re.search(content)
34 return json.loads(content, object_pairs_hook=OrderedDict)
35
36
37 def 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'
46 return load_config(path)
47
48
49 def get_default_config(key):
50 """
51 Get default value of a config key
52 """
53 path = os.path.dirname(
54 __file__) + '/colorset/config'
55 data = load_config(path)
56 return data[key]
57
58
59 def get_config(key):
60 """
61 Get current value of a config key
62 """
63 return c[key]
64
65
66 def set_config(key,value):
67 """
68 Set a config key with specific value
69 """
70 # Modify value
71 if value.isdigit():
72 value = int(value)
73 if value.lower() == 'True':
74 value = True
75 elif value.lower() == 'False':
76 value = False
77 # Fix up
78 path = os.environ.get(
79 'HOME',
80 os.environ.get(
81 'USERPROFILE',
82 '')) + os.sep + '.rainbow_config.json'
83 data = load_config(path)
84 fixup(data, key, value)
85 # Save
86 with open(path, 'w') as out:
87 json.dump(data, out, indent = 4)
88 os.system('chmod 777 ' + path)
89
90
91 def reload_config():
92 """
93 Reload config
94 """
95 rainbow_config = os.environ.get(
96 'HOME',
97 os.environ.get(
98 'USERPROFILE',
99 '')) + os.sep + '.rainbow_config.json'
100 try:
101 data = load_config(rainbow_config)
102 for d in data:
103 c[d] = data[d]
104 except:
105 print('It seems that ~/.rainbow_config.json has wrong format :(')
106
107
108 # Config dictionary
109 c = {}
110
111 # Load the initial config
112 config = os.path.dirname(
113 __file__) + '/colorset/config'
114 try:
115 data = load_config(config)
116 for d in data:
117 c[d] = data[d]
118 except:
119 pass
120
121 # Load user's config
122 rainbow_config = os.environ.get(
123 'HOME',
124 os.environ.get(
125 'USERPROFILE',
126 '')) + os.sep + '.rainbow_config.json'
127 try:
128 data = load_config(rainbow_config)
129 for d in data:
130 c[d] = data[d]
131 except:
132 print('It seems that ~/.rainbow_config.json has wrong format :(')
133
134 # Load default theme
135 theme_file = os.path.dirname(
136 __file__) + '/colorset/' + c['THEME'] + '.json'
137 try:
138 data = load_config(theme_file)
139 for d in data:
140 c[d] = data[d]
141 except:
142 pass