5
5
import pkg_resources
6
6
7
7
8
+ class SetEncoder (json .JSONEncoder ):
9
+ def default (self , obj ):
10
+ if isinstance (obj , set ):
11
+ return list (obj )
12
+ return json .JSONEncoder .default (self , obj )
13
+
14
+
8
15
class Config :
9
16
10
17
def __init__ (self ):
11
18
conf = self .__load ()
12
19
13
- self .suffix = conf ['suffix' ]
14
- self .comment = conf ['comment' ]
15
- self .ignore = conf ['ignore' ]
20
+ self .suffix = set ( conf ['suffix' ])
21
+ self .comment = set ( conf ['comment' ])
22
+ self .ignore = set ( conf ['ignore' ])
16
23
17
24
def invoke (self , args ):
18
25
if args .restore :
19
26
self .restore ()
20
27
else :
21
- if any ([args .suffix_add , args .comment_add , args .ignore_add ]):
22
- self .__append_config (args .suffix_add , args .comment_add , args .ignore_add )
23
28
if any ([args .suffix_reset , args .comment_reset , args .ignore_reset ]):
24
29
self .__reset_config (args .suffix_reset , args .comment_reset , args .ignore_reset )
30
+ if any ([args .suffix_add , args .comment_add , args .ignore_add ]):
31
+ self .__append_config (args .suffix_add , args .comment_add , args .ignore_add )
32
+ if any ([args .suffix_del , args .comment_del , args .ignore_del ]):
33
+ self .__remove_config (args .suffix_del , args .comment_del , args .ignore_del )
25
34
if args .show_list :
26
35
self .show ()
27
36
28
37
def show (self ):
29
- print (json .dumps (self .__dict__ , indent = 4 ))
38
+ print (json .dumps (self .__dict__ , indent = 4 , cls = SetEncoder ))
30
39
31
40
def __confirm (self , tips ):
32
41
check = input (tips )
33
42
return check .strip ().lower () == 'y'
34
43
44
+ def __reset_config (self , suffix_reset , comment_reset , ignore_reset ):
45
+ if suffix_reset :
46
+ if self .__confirm ("'suffix' will be replaced with {} . (y/n) " .format (suffix_reset )):
47
+ self .suffix = set (suffix_reset )
48
+ if comment_reset :
49
+ if self .__confirm ("'comment' will be replaced with {} . (y/n) " .format (comment_reset )):
50
+ self .comment = set (comment_reset )
51
+ if ignore_reset :
52
+ if self .__confirm ("'ignore' will be replaced with {} . (y/n) " .format (ignore_reset )):
53
+ self .ignore = set (ignore_reset )
54
+
55
+ self .__update ()
56
+
35
57
def __append_config (self , suffix_add , comment_add , ignore_add ):
36
58
if suffix_add :
37
- if self .__confirm ("'suffix' will be appended with {} (y/n)" .format (suffix_add )):
38
- self .suffix .extend (suffix_add )
59
+ if self .__confirm ("'suffix' will be appended with {} . (y/n) " .format (suffix_add )):
60
+ self .suffix .update (suffix_add )
39
61
if comment_add :
40
- if self .__confirm ("'comment' will be appended with {} (y/n)" .format (comment_add )):
41
- self .comment .extend (comment_add )
62
+ if self .__confirm ("'comment' will be appended with {} . (y/n) " .format (comment_add )):
63
+ self .comment .update (comment_add )
42
64
if ignore_add :
43
- if self .__confirm ("'ignore' will be appended with {} (y/n)" .format (ignore_add )):
44
- self .ignore .extend (ignore_add )
65
+ if self .__confirm ("'ignore' will be appended with {} . (y/n) " .format (ignore_add )):
66
+ self .ignore .update (ignore_add )
45
67
46
68
self .__update ()
47
69
48
- def __reset_config (self , suffix_reset , comment_reset , ignore_reset ):
49
- if suffix_reset :
50
- if self .__confirm ("'suffix' will be replaced with {} (y/n)" .format (suffix_reset )):
51
- self .suffix = suffix_reset
52
- if comment_reset :
53
- if self .__confirm ("'comment' will be replaced with {} (y/n)" .format (comment_reset )):
54
- self .comment = comment_reset
55
- if ignore_reset :
56
- if self .__confirm ("'ignore' will be replaced with {} (y/n)" .format (ignore_reset )):
57
- self .ignore = ignore_reset
70
+ def __remove_config (self , suffix_del , comment_del , ignore_del ):
71
+ if suffix_del :
72
+ if self .__confirm ("'suffix' will remove {} . (y/n) " .format (suffix_del )):
73
+ self .suffix . difference_update ( suffix_del )
74
+ if comment_del :
75
+ if self .__confirm ("'comment' will remove {} . (y/n) " .format (comment_del )):
76
+ self .comment . difference_update ( comment_del )
77
+ if ignore_del :
78
+ if self .__confirm ("'ignore' will remove {} . (y/n) " .format (ignore_del )):
79
+ self .ignore . difference_update ( ignore_del )
58
80
59
81
self .__update ()
60
82
@@ -67,14 +89,14 @@ def __load(self):
67
89
def __update (self ):
68
90
filename = pkg_resources .resource_filename (__name__ , 'config.json' )
69
91
with open (filename , 'w' ) as config :
70
- json .dump (self .__dict__ , config , indent = 4 )
92
+ json .dump (self .__dict__ , config , indent = 4 , cls = SetEncoder )
71
93
72
94
def restore (self ):
73
- self .suffix = [ "c" , "cc" , "clj" , "cpp" , "cs" , "cu" , "cuh" , "dart" , "go" , "h" ,
74
- "hpp " , "java " , "jl " , "js " , "kt " , "lisp " , "lua " , "pde " , "m " , "php " ,
75
- "py" , "R" , "rb" , "rs" , "rust" , "sh" , "scala" , "swift" , "ts" , " vb"]
76
- self .comment = [ "#" , "//" , "/*" , "*" , "*/" , ":" , ";" , '""""' ]
77
- self .ignore = [ "out" , "venv" , ".git" , ".idea" , "build" , "target" , "node_modules" , ".vscode" , "dist" ]
95
+ self .suffix = { "c" , "cc" , "clj" , "cpp" , "cs" , "cu" , "cuh" , "dart" , "go" , "h" , "hpp" , "java" , "jl" , "js" , "kt " ,
96
+ "lisp " , "lua " , "pde " , "m " , "php " , "py " , "R " , "rb " , "rs " , "rust" , "sh" , "scala" , "swift" , "ts " ,
97
+ "vb" }
98
+ self .comment = { "#" , "//" , "/*" , "*" , "*/" , ":" , ";" , '""""' }
99
+ self .ignore = { "out" , "venv" , ".git" , ".idea" , "build" , "target" , "node_modules" , ".vscode" , "dist" }
78
100
79
- if self .__confirm ('Default configuration will be restored (y/n)? ' ):
101
+ if self .__confirm ('The default configuration will be restored. (y/n) ' ):
80
102
self .__update ()
0 commit comments