1245ca456371d6a9de4d8f0944d7368c6be6da5d
5 from collections
import OrderedDict
7 # Regular expression for comments in config file
8 comment_re
= re
.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re
.DOTALL | re
.MULTILINE
17 def fixup(adict
, k
, v
):
19 Fix up a key in json format
21 for key
in adict
.keys():
24 elif isinstance(adict
[key
], dict):
25 fixup(adict
[key
], k
, v
)
28 def load_config(filepath
):
30 Load config from filepath
32 with
open(filepath
) as f
:
33 content
= ''.join(f
.readlines())
34 match
= comment_re
.search(content
)
36 content
= content
[:match
.start()] + content
[match
.end():]
37 match
= comment_re
.search(content
)
38 return json
.loads(content
, object_pairs_hook
=OrderedDict
)
45 path
= os
.environ
.get(
49 '')) + os
.sep
+ '.rainbow_config.json'
50 data
= load_config(path
)
51 # Hard to set from prompt
52 data
.pop('ONLY_LIST', None)
53 data
.pop('IGNORE_LIST', None)
57 def get_default_config(key
):
59 Get default value of a config key
61 path
= os
.path
.dirname(
62 __file__
) + '/colorset/config'
64 data
= load_config(path
)
66 raise Exception('No such config key.')
72 Get current value of a config key
77 def set_config(key
, value
):
79 Set a config key with specific value
84 elif value
.lower() == 'true':
86 elif value
.lower() == 'false':
89 path
= os
.environ
.get(
93 '')) + os
.sep
+ '.rainbow_config.json'
94 data
= load_config(path
)
95 # Update global config
99 fixup(data
, key
, value
)
103 with
open(path
, 'w') as out
:
104 json
.dump(data
, out
, indent
=4)
105 os
.system('chmod 777 ' + path
)
108 def delete_config(key
):
112 path
= os
.environ
.get(
116 '')) + os
.sep
+ '.rainbow_config.json'
117 data
= load_config(path
)
119 if key
in data
and key
in c
:
123 data
[key
] = c
[key
] = get_default_config(key
)
127 raise Exception('No such config key.')
129 with
open(path
, 'w') as out
:
130 json
.dump(data
, out
, indent
=4)
131 os
.system('chmod 777 ' + path
)
138 rainbow_config
= os
.environ
.get(
142 '')) + os
.sep
+ '.rainbow_config.json'
144 data
= load_config(rainbow_config
)
148 print('It seems that ~/.rainbow_config.json has wrong format :(')
155 # Load the initial config
156 config
= os
.path
.dirname(
157 __file__
) + '/colorset/config'
159 data
= load_config(config
)
165 rainbow_config
= os
.environ
.get(
169 '')) + os
.sep
+ '.rainbow_config.json'
171 data
= load_config(rainbow_config
)
175 print('It seems that ~/.rainbow_config.json has wrong format :(')
177 theme_file
= os
.path
.dirname(
178 __file__
) + '/colorset/' + c
['THEME'] + '.json'
180 data
= load_config(theme_file
)