autopep8 and bumped version
[rainbowstream.git] / rainbowstream / config.py
... / ...
CommitLineData
1import json
2import re
3import os
4import os.path
5from collections import OrderedDict
6
7# Regular expression for comments
8comment_re = re.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re.DOTALL | re.MULTILINE
11)
12
13
14def 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
25def 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
38def 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
54def 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
64def get_config(key):
65 """
66 Get current value of a config key
67 """
68 return c[key]
69
70
71def 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
96def 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
114c = {}
115
116# Load the initial config
117config = os.path.dirname(
118 __file__) + '/colorset/config'
119try:
120 data = load_config(config)
121 for d in data:
122 c[d] = data[d]
123except:
124 pass
125
126# Load user's config
127rainbow_config = os.environ.get(
128 'HOME',
129 os.environ.get(
130 'USERPROFILE',
131 '')) + os.sep + '.rainbow_config.json'
132try:
133 data = load_config(rainbow_config)
134 for d in data:
135 c[d] = data[d]
136except:
137 print('It seems that ~/.rainbow_config.json has wrong format :(')
138
139# Load default theme
140theme_file = os.path.dirname(
141 __file__) + '/colorset/' + c['THEME'] + '.json'
142try:
143 data = load_config(theme_file)
144 for d in data:
145 c[d] = data[d]
146except:
147 pass