42
42
)
43
43
from fileformats .generic import File
44
44
import nipype2pydra .package
45
+ from nipype2pydra .exceptions import UnmatchedParensException
45
46
46
47
logger = logging .getLogger ("nipype2pydra" )
47
48
@@ -498,7 +499,10 @@ def _referenced_funcs_and_methods(self):
498
499
method_returns [method_name ] = []
499
500
method_stacks [method_name ] = ()
500
501
for method_name in self .INCLUDED_METHODS :
501
- if method_name not in self .nipype_interface .__dict__ :
502
+ base = find_super_method (
503
+ self .nipype_interface , method_name , include_class = True
504
+ )[1 ]
505
+ if self .package .is_omitted (base ):
502
506
continue # Don't include base methods
503
507
method = getattr (self .nipype_interface , method_name )
504
508
referenced_methods .add (method )
@@ -1103,9 +1107,7 @@ def _get_referenced(
1103
1107
)
1104
1108
for match in re .findall (r"super\([^\)]*\)\.(\w+)\(" , method_body ):
1105
1109
super_method , base = find_super_method (super_base , match )
1106
- if any (
1107
- base .__module__ .startswith (m ) for m in UsedSymbols .ALWAYS_OMIT_MODULES
1108
- ):
1110
+ if self .package .is_omitted (super_method ):
1109
1111
continue
1110
1112
func_name = self ._common_parent_pkg_prefix (base ) + match
1111
1113
if func_name not in referenced_supers :
@@ -1296,28 +1298,28 @@ def replace_supers(self, method_body, super_base=None):
1296
1298
if super_base is None :
1297
1299
super_base = self .nipype_interface
1298
1300
name_map = self .method_supers [super_base ]
1299
-
1300
- def replace_super (match ):
1301
- super_method , base = find_super_method (super_base , match .group (1 ))
1301
+ splits = re .split (r"super\([^\)]*\)\.(\w+)(?=\()" , method_body )
1302
+ new_body = splits [0 ]
1303
+ for name , block in zip (splits [1 ::2 ], splits [2 ::2 ]):
1304
+ super_method , base = find_super_method (super_base , name )
1305
+ _ , args , post = extract_args (block )
1306
+ arg_str = ", " .join (args )
1302
1307
try :
1303
- return self .SPECIAL_SUPER_MAPPINGS [super_method ].format (
1304
- args = match . group ( 2 )
1308
+ new_body += self .SPECIAL_SUPER_MAPPINGS [super_method ].format (
1309
+ args = arg_str
1305
1310
)
1306
1311
except KeyError :
1307
1312
try :
1308
- return name_map [match . group ( 1 ) ] + "(" + match . group ( 2 ) + ")"
1313
+ new_body += name_map [name ] + "(" + arg_str + ")"
1309
1314
except KeyError :
1310
- if any (
1311
- base .__module__ .startswith (m )
1312
- for m in UsedSymbols .ALWAYS_OMIT_MODULES
1313
- ):
1315
+ if self .package .is_omitted (base ):
1314
1316
raise KeyError (
1315
- f"Require special mapping for '{ match . group ( 1 ) } ' in { base } class "
1317
+ f"Require special mapping for '{ name } ' in { base } class "
1316
1318
"as methods in that module are being omitted from the conversion"
1317
1319
) from None
1318
1320
raise
1319
-
1320
- return re . sub ( r"super\([^\)]*\)\.(\w+)\(([^\)]*)\)" , replace_super , method_body )
1321
+ new_body += post [ 1 :]
1322
+ return new_body
1321
1323
1322
1324
def unwrap_nested_methods (
1323
1325
self , method_body , additional_args = (), inputs_as_dict : bool = False
@@ -1375,13 +1377,22 @@ def unwrap_nested_methods(
1375
1377
)
1376
1378
# Insert additional arguments to the method call (which were previously
1377
1379
# accessed via member attributes)
1378
- new_body += name + insert_args_in_signature (
1379
- args ,
1380
- [
1381
- f"{ a } =inputs['{ a } ']" if inputs_as_dict else f"{ a } ={ a } "
1382
- for a in (list (self .method_args [name ]) + list (additional_args ))
1383
- ],
1384
- )
1380
+ args_to_be_inserted = list (self .method_args [name ]) + list (additional_args )
1381
+ try :
1382
+ new_body += name + insert_args_in_signature (
1383
+ args ,
1384
+ [
1385
+ f"{ a } =inputs['{ a } ']" if inputs_as_dict else f"{ a } ={ a } "
1386
+ for a in args_to_be_inserted
1387
+ ],
1388
+ )
1389
+ except UnmatchedParensException :
1390
+ logger .warning (
1391
+ f"Nested method call inside '{ name } ' in { self .full_address } , "
1392
+ "the following args will need to be manually inserted up after the "
1393
+ f"conversion: { args_to_be_inserted } "
1394
+ )
1395
+ new_body += name + args
1385
1396
method_body = new_body
1386
1397
# Convert assignment to self attributes into method-scoped variables (hopefully
1387
1398
# there aren't any name clashes)
@@ -1395,6 +1406,7 @@ def unwrap_nested_methods(
1395
1406
CommandLine ._format_arg : "argstr.format(**inputs)" ,
1396
1407
CommandLine ._filename_from_source : "{args} + '_generated'" ,
1397
1408
BaseInterface ._check_version_requirements : "[]" ,
1409
+ CommandLine ._parse_inputs : "{{}}" ,
1398
1410
}
1399
1411
1400
1412
INPUT_KEYS = [
0 commit comments