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 return load_config(path
)
49 def get_default_config(key
):
51 Get default value of a config key
53 path
= os
.path
.dirname(
54 __file__
) + '/colorset/config'
55 data
= load_config(path
)
61 Get current value of a config key
66 def set_config(key
,value
):
68 Set a config key with specific value
73 if value
.lower() == 'True':
75 elif value
.lower() == 'False':
78 path
= os
.environ
.get(
82 '')) + os
.sep
+ '.rainbow_config.json'
83 data
= load_config(path
)
84 fixup(data
, key
, value
)
86 with
open(path
, 'w') as out
:
87 json
.dump(data
, out
, indent
= 4)
88 os
.system('chmod 777 ' + path
)
95 rainbow_config
= os
.environ
.get(
99 '')) + os
.sep
+ '.rainbow_config.json'
101 data
= load_config(rainbow_config
)
105 print('It seems that ~/.rainbow_config.json has wrong format :(')
111 # Load the initial config
112 config
= os
.path
.dirname(
113 __file__
) + '/colorset/config'
115 data
= load_config(config
)
122 rainbow_config
= os
.environ
.get(
126 '')) + os
.sep
+ '.rainbow_config.json'
128 data
= load_config(rainbow_config
)
132 print('It seems that ~/.rainbow_config.json has wrong format :(')
135 theme_file
= os
.path
.dirname(
136 __file__
) + '/colorset/' + c
['THEME'] + '.json'
138 data
= load_config(theme_file
)