55import pkg_resources
66
77
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+
815class Config :
916
1017 def __init__ (self ):
1118 conf = self .__load ()
1219
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' ])
1623
1724 def invoke (self , args ):
1825 if args .restore :
1926 self .restore ()
2027 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 )
2328 if any ([args .suffix_reset , args .comment_reset , args .ignore_reset ]):
2429 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 )
2534 if args .show_list :
2635 self .show ()
2736
2837 def show (self ):
29- print (json .dumps (self .__dict__ , indent = 4 ))
38+ print (json .dumps (self .__dict__ , indent = 4 , cls = SetEncoder ))
3039
3140 def __confirm (self , tips ):
3241 check = input (tips )
3342 return check .strip ().lower () == 'y'
3443
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+
3557 def __append_config (self , suffix_add , comment_add , ignore_add ):
3658 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 )
3961 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 )
4264 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 )
4567
4668 self .__update ()
4769
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 )
5880
5981 self .__update ()
6082
@@ -67,14 +89,14 @@ def __load(self):
6789 def __update (self ):
6890 filename = pkg_resources .resource_filename (__name__ , 'config.json' )
6991 with open (filename , 'w' ) as config :
70- json .dump (self .__dict__ , config , indent = 4 )
92+ json .dump (self .__dict__ , config , indent = 4 , cls = SetEncoder )
7193
7294 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" }
78100
79- if self .__confirm ('Default configuration will be restored (y/n)? ' ):
101+ if self .__confirm ('The default configuration will be restored. (y/n) ' ):
80102 self .__update ()
0 commit comments