860646d78ca29f62375fac264246733a2b699b15
6 from collections
import OrderedDict
8 # Regular expression for comments in config file
9 comment_re
= re
.compile(
10 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
11 re
.DOTALL | re
.MULTILINE
18 def fixup(adict
, k
, v
):
20 Fix up a key in json format
22 for key
in adict
.keys():
25 elif isinstance(adict
[key
], dict):
26 fixup(adict
[key
], k
, v
)
29 def load_config(filepath
):
31 Load config from filepath
33 with
open(filepath
, encoding
='utf-8') as f
:
34 content
= ''.join(f
.readlines())
35 match
= comment_re
.search(content
)
37 content
= content
[:match
.start()] + content
[match
.end():]
38 match
= comment_re
.search(content
)
39 return json
.loads(content
, object_pairs_hook
=OrderedDict
)
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)
55 def get_default_config(key
):
57 Get default value of a config key
59 path
= os
.path
.dirname(
60 __file__
) + '/colorset/config'
62 data
= load_config(path
)
64 raise Exception('No such config key.')
70 Get current value of a config key
75 def set_config(key
, value
):
77 Set a config key with specific value
82 elif value
.lower() == 'true':
84 elif value
.lower() == 'false':
87 path
= os
.path
.expanduser("~") + os
.sep
+ '.rainbow_config.json'
90 data
= load_config(path
)
93 # Update global config
97 fixup(data
, key
, value
)
101 with
open(path
, 'w', encoding
='utf-8') as out
:
102 json
.dump(data
, out
, indent
=4)
103 os
.system('chmod 777 ' + path
)
106 def delete_config(key
):
110 path
= os
.path
.expanduser("~") + os
.sep
+ '.rainbow_config.json'
111 data
= load_config(path
)
113 if key
in data
and key
in c
:
117 data
[key
] = c
[key
] = get_default_config(key
)
121 raise Exception('No such config key.')
123 with
open(path
, 'w', encoding
='utf-8') as out
:
124 json
.dump(data
, out
, indent
=4)
125 os
.system('chmod 777 ' + path
)
132 rainbow_config
= os
.path
.expanduser("~") + os
.sep
+ '.rainbow_config.json'
134 data
= load_config(rainbow_config
)
138 print('It seems that ~/.rainbow_config.json has wrong format :(')
145 # Load the initial config
146 config
= os
.path
.dirname(
147 __file__
) + '/colorset/config'
149 data
= load_config(config
)
155 rainbow_config
= os
.path
.expanduser("~") + os
.sep
+ '.rainbow_config.json'
157 data
= load_config(rainbow_config
)
163 theme_file
= os
.path
.dirname(
164 __file__
) + '/colorset/' + c
['THEME'] + '.json'
166 data
= load_config(theme_file
)