55import sys
66
77from wolframclient .cli .utils import SimpleCommand
8- from wolframclient .utils .importutils import module_path
8+ from wolframclient .utils .importutils import module_path , safe_import_string_and_call
99
1010
1111class Command (SimpleCommand ):
@@ -24,42 +24,40 @@ def _module_args(self, *args):
2424 for arg in args :
2525 yield arg
2626
27- def handle (self , ** opts ):
27+ def run (self , path , * args ):
28+ originals = sys .argv
29+ sys .argv = list (self ._module_args (* args ))
30+ safe_import_string_and_call (path )
31+ sys .argv = originals
2832
29- argv = sys . argv
33+ def handle ( self , ** opts ):
3034
31- from autoflake import main
35+ # autoflake is not removing imports if they are in a list
36+ # to fix this we first refactor the code using imports in single line
3237
33- sys .argv = list (
34- self ._module_args (
35- "--in-place" ,
36- "--remove-duplicate-keys" ,
37- "--expand-star-import" ,
38- "--remove-all-unused-imports" ,
39- "--recursive" ,
40- )
38+ self .run (
39+ "isort.main.main" ,
40+ "-rc" ,
41+ "-sl" ,
42+ "-a" ,
43+ "from __future__ import absolute_import, print_function, unicode_literals" ,
4144 )
4245
43- main ()
46+ # then we remove all missing imports and we expand star imports
4447
45- from isort .main import main
46-
47- sys .argv = list (
48- self ._module_args (
49- "-rc" ,
50- "--multi-line" ,
51- "5" ,
52- "-a" ,
53- "from __future__ import absolute_import, print_function, unicode_literals" ,
54- )
48+ self .run (
49+ "autoflake.main" ,
50+ "--in-place" ,
51+ "--remove-duplicate-keys" ,
52+ "--expand-star-import" ,
53+ "--remove-all-unused-imports" ,
54+ "--recursive" ,
5555 )
5656
57- main ()
58-
59- from black import main
57+ # then we use refactor imports again using pretty newline style
6058
61- sys . argv = list ( self ._module_args ( "--line-length " , "95 " , "--target-version " , "py34" ) )
59+ self .run ( "isort.main.main " , "-rc " , "--multi-line " , "5" )
6260
63- main ()
61+ # after that we finally run black to refactor all code
6462
65- sys . argv = argv
63+ self . run ( "black.main" , "--line-length" , "95" , "--target-version" , "py34" )
0 commit comments