Removing superfluous str() functions
[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 """
9d5a2a1c 32 with open(filepath) as f:
ddb1e615
VNM
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 """
a8e71259 45 try:
46 path = os.path.expanduser("~") + 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 data.pop('FORMAT', None)
eb9b6273 52 data.pop('QUOTE_FORMAT', None)
53 data.pop('NOTIFY_FORMAT', None)
a8e71259 54 return data
55 except:
56 return []
29fd0be6
O
57
58
59def get_default_config(key):
60 """
61 Get default value of a config key
62 """
fe9bb33b 63 try:
a8e71259 64 path = os.path.dirname(__file__) + '/colorset/config'
fe9bb33b 65 data = load_config(path)
a8e71259 66 return data[key]
fe9bb33b 67 except:
a8e71259 68 raise Exception('This config key does not exist in default.')
29fd0be6 69
ceec8593 70
29fd0be6
O
71def get_config(key):
72 """
73 Get current value of a config key
74 """
75 return c[key]
76
77
a8c5fce4 78def set_config(key, value):
29fd0be6
O
79 """
80 Set a config key with specific value
81 """
82 # Modify value
83 if value.isdigit():
84 value = int(value)
fe9bb33b 85 elif value.lower() == 'true':
29fd0be6 86 value = True
fe9bb33b 87 elif value.lower() == 'false':
29fd0be6 88 value = False
a8e71259 89 # Update global config
90 c[key] = value
91 # Load current user config
8619b00b 92 path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
63388de7
O
93 data = {}
94 try:
95 data = load_config(path)
96 except:
a8e71259 97 return
ceec8593 98 # Update config file
fe9bb33b 99 if key in data:
100 fixup(data, key, value)
101 else:
102 data[key] = value
29fd0be6 103 # Save
9d5a2a1c 104 with open(path, 'w') as out:
a8c5fce4 105 json.dump(data, out, indent=4)
29fd0be6
O
106 os.system('chmod 777 ' + path)
107
108
fe9bb33b 109def delete_config(key):
ceec8593 110 """
111 Delete a config key
112 """
8619b00b 113 path = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
62686013 114 try:
a8e71259 115 data = load_config(path)
116 except:
117 raise Exception('Config file is messed up.')
ceec8593 118 # Drop key
119 if key in data and key in c:
120 data.pop(key)
121 c.pop(key)
122 try:
123 data[key] = c[key] = get_default_config(key)
124 except:
125 pass
126 else:
127 raise Exception('No such config key.')
128 # Save
9d5a2a1c 129 with open(path, 'w') as out:
ceec8593 130 json.dump(data, out, indent=4)
131 os.system('chmod 777 ' + path)
fe9bb33b 132
133
29fd0be6
O
134def reload_config():
135 """
136 Reload config
137 """
62686013 138 try:
139 rainbow_config = os.path.expanduser("~") + \
140 os.sep + '.rainbow_config.json'
29fd0be6
O
141 data = load_config(rainbow_config)
142 for d in data:
143 c[d] = data[d]
144 except:
a8e71259 145 raise Exception('Can not reload config file with wrong format.')
29fd0be6
O
146
147
531f5682
O
148def init_config():
149 """
150 Init configuration
151 """
152 # Load the initial config
e3927852
O
153 config = os.path.dirname(__file__) + \
154 '/colorset/config'
531f5682
O
155 try:
156 data = load_config(config)
157 for d in data:
158 c[d] = data[d]
159 except:
160 pass
161 # Load user's config
8619b00b 162 rainbow_config = os.path.expanduser("~") + os.sep + '.rainbow_config.json'
531f5682
O
163 try:
164 data = load_config(rainbow_config)
165 for d in data:
166 c[d] = data[d]
248cb620 167 except (IOError, ValueError) as e:
a8e71259 168 c['USER_JSON_ERROR'] = str(e)
531f5682 169 # Load default theme
e3927852
O
170 theme_file = os.path.dirname(__file__) + \
171 '/colorset/' + c['THEME'] + '.json'
531f5682
O
172 try:
173 data = load_config(theme_file)
174 for d in data:
175 c[d] = data[d]
176 except:
177 pass
178
1f2f6159 179
531f5682 180# Init config
fe9bb33b 181init_config()