2e79c9aea768e210c76c3fedcae1b7b5246b3ae1
5 from collections
import OrderedDict
7 # Regular expression for comments
8 comment_re
= re
.compile(
9 '(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
10 re
.DOTALL | re
.MULTILINE
13 def fixup(adict
, k
, v
):
15 Fix up a key in json format
17 for key
in adict
.keys():
20 elif type(adict
[key
]) is dict:
21 fixup(adict
[key
], k
, v
)
24 def load_config(filepath
):
26 Load config from filepath
28 with
open(filepath
) as f
:
29 content
= ''.join(f
.readlines())
30 match
= comment_re
.search(content
)
32 content
= content
[:match
.start()] + content
[match
.end():]
33 match
= comment_re
.search(content
)
34 return json
.loads(content
, object_pairs_hook
=OrderedDict
)
41 path
= os
.environ
.get(
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)
53 def get_default_config(key
):
55 Get default value of a config key
57 path
= os
.path
.dirname(
58 __file__
) + '/colorset/config'
59 data
= load_config(path
)
65 Get current value of a config key
70 def set_config(key
,value
):
72 Set a config key with specific value
77 elif value
.lower() == 'True':
79 elif value
.lower() == 'False':
82 path
= os
.environ
.get(
86 '')) + os
.sep
+ '.rainbow_config.json'
87 data
= load_config(path
)
88 fixup(data
, key
, value
)
90 with
open(path
, 'w') as out
:
91 json
.dump(data
, out
, indent
= 4)
92 os
.system('chmod 777 ' + path
)
99 rainbow_config
= os
.environ
.get(
103 '')) + os
.sep
+ '.rainbow_config.json'
105 data
= load_config(rainbow_config
)
109 print('It seems that ~/.rainbow_config.json has wrong format :(')
115 # Load the initial config
116 config
= os
.path
.dirname(
117 __file__
) + '/colorset/config'
119 data
= load_config(config
)
126 rainbow_config
= os
.environ
.get(
130 '')) + os
.sep
+ '.rainbow_config.json'
132 data
= load_config(rainbow_config
)
136 print('It seems that ~/.rainbow_config.json has wrong format :(')
139 theme_file
= os
.path
.dirname(
140 __file__
) + '/colorset/' + c
['THEME'] + '.json'
142 data
= load_config(theme_file
)