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