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