44import json
55import pkg_resources
66
7+ class SetEncoder (json .JSONEncoder ):
8+ def default (self , obj ):
9+ if isinstance (obj , set ):
10+ return list (obj )
11+ return json .JSONEncoder .default (self , obj )
712
813class Config :
914
1015 def __init__ (self ):
1116 conf = self .__load ()
1217
13- self .suffix = conf ['suffix' ]
14- self .comment = conf ['comment' ]
15- self .ignore = conf ['ignore' ]
18+ self .suffix = set ( conf ['suffix' ])
19+ self .comment = set ( conf ['comment' ])
20+ self .ignore = set ( conf ['ignore' ])
1621
1722 def invoke (self , args ):
1823 if args .restore :
@@ -26,7 +31,7 @@ def invoke(self, args):
2631 self .show ()
2732
2833 def show (self ):
29- print (json .dumps (self .__dict__ , indent = 4 ))
34+ print (json .dumps (self .__dict__ , indent = 4 , cls = SetEncoder ))
3035
3136 def __confirm (self , tips ):
3237 check = input (tips )
@@ -35,26 +40,26 @@ def __confirm(self, tips):
3540 def __append_config (self , suffix_add , comment_add , ignore_add ):
3641 if suffix_add :
3742 if self .__confirm ("'suffix' will be appended with {} (y/n)" .format (suffix_add )):
38- self .suffix .extend (suffix_add )
43+ self .suffix .update (suffix_add )
3944 if comment_add :
4045 if self .__confirm ("'comment' will be appended with {} (y/n)" .format (comment_add )):
41- self .comment .extend (comment_add )
46+ self .comment .update (comment_add )
4247 if ignore_add :
4348 if self .__confirm ("'ignore' will be appended with {} (y/n)" .format (ignore_add )):
44- self .ignore .extend (ignore_add )
49+ self .ignore .update (ignore_add )
4550
4651 self .__update ()
4752
4853 def __reset_config (self , suffix_reset , comment_reset , ignore_reset ):
4954 if suffix_reset :
5055 if self .__confirm ("'suffix' will be replaced with {} (y/n)" .format (suffix_reset )):
51- self .suffix = suffix_reset
56+ self .suffix = set ( suffix_reset )
5257 if comment_reset :
5358 if self .__confirm ("'comment' will be replaced with {} (y/n)" .format (comment_reset )):
54- self .comment = comment_reset
59+ self .comment = set ( comment_reset )
5560 if ignore_reset :
5661 if self .__confirm ("'ignore' will be replaced with {} (y/n)" .format (ignore_reset )):
57- self .ignore = ignore_reset
62+ self .ignore = set ( ignore_reset )
5863
5964 self .__update ()
6065
@@ -67,14 +72,14 @@ def __load(self):
6772 def __update (self ):
6873 filename = pkg_resources .resource_filename (__name__ , 'config.json' )
6974 with open (filename , 'w' ) as config :
70- json .dump (self .__dict__ , config , indent = 4 )
75+ json .dump (self .__dict__ , config , indent = 4 , cls = SetEncoder )
7176
7277 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" ]
78+ self .suffix = { "c" , "cc" , "clj" , "cpp" , "cs" , "cu" , "cuh" , "dart" , "go" , "h" , "hpp" , "java" , "jl" , "js" , "kt " ,
79+ "lisp " , "lua " , "pde " , "m " , "php " , "py " , "R " , "rb " , "rs " , "rust" , "sh" , "scala" , "swift" , "ts " ,
80+ "vb" }
81+ self .comment = { "#" , "//" , "/*" , "*" , "*/" , ":" , ";" , '""""' }
82+ self .ignore = { "out" , "venv" , ".git" , ".idea" , "build" , "target" , "node_modules" , ".vscode" , "dist" }
7883
79- if self .__confirm ('Default configuration will be restored (y/n)?' ):
84+ if self .__confirm ('The default configuration will be restored (y/n)?' ):
8085 self .__update ()
0 commit comments