@@ -2309,6 +2309,54 @@ def __init__(self, infields=None, outfields=None, **kwargs):
2309
2309
):
2310
2310
self .inputs .template += '$'
2311
2311
2312
+ def _get_files_over_ssh (self , template ):
2313
+ """Get the files matching template over an SSH connection."""
2314
+ # Connect over SSH
2315
+ client = self ._get_ssh_client ()
2316
+ sftp = client .open_sftp ()
2317
+ sftp .chdir (self .inputs .base_directory )
2318
+
2319
+ # Get all files in the dir, and filter for desired files
2320
+ template_dir = os .path .dirname (template )
2321
+ template_base = os .path .basename (template )
2322
+ filelist = sftp .listdir (template_dir )
2323
+ if self .inputs .template_expression == 'fnmatch' :
2324
+ outfiles = fnmatch .filter (filelist , template_base )
2325
+ elif self .inputs .template_expression == 'regexp' :
2326
+ regexp = re .compile (template_base )
2327
+ outfiles = list (filter (regexp .match , filelist ))
2328
+ else :
2329
+ raise ValueError ('template_expression value invalid' )
2330
+
2331
+ if len (outfiles ) == 0 :
2332
+ # no files
2333
+ msg = 'Output key: %s Template: %s returned no files' % (key , template )
2334
+ if self .inputs .raise_on_empty :
2335
+ raise IOError (msg )
2336
+ else :
2337
+ warn (msg )
2338
+
2339
+ # return value
2340
+ outfiles = None
2341
+
2342
+ else :
2343
+ # found files, sort and save to outputs
2344
+ if self .inputs .sort_filelist :
2345
+ outfiles = human_order_sorted (outfiles )
2346
+
2347
+ # actually download the files, if desired
2348
+ if self .inputs .download_files :
2349
+ for f in outfiles :
2350
+ try :
2351
+ sftp .get (os .path .join (template_dir , f ), f )
2352
+ except IOError :
2353
+ iflogger .info ('remote file %s not found' % f )
2354
+
2355
+ # return value
2356
+ outfiles = list_to_filename (outfiles )
2357
+
2358
+ return outfiles
2359
+
2312
2360
def _list_outputs (self ):
2313
2361
try :
2314
2362
paramiko
@@ -2340,39 +2388,7 @@ def _list_outputs(self):
2340
2388
template = self .inputs .field_template [key ]
2341
2389
2342
2390
if not args :
2343
- # Connect over SSH
2344
- client = self ._get_ssh_client ()
2345
- sftp = client .open_sftp ()
2346
-
2347
- # Get the files in the base dir, and filter for desired files
2348
- sftp .chdir (self .inputs .base_directory )
2349
- filelist = sftp .listdir ()
2350
- if self .inputs .template_expression == 'fnmatch' :
2351
- filelist = fnmatch .filter (filelist , template )
2352
- elif self .inputs .template_expression == 'regexp' :
2353
- regexp = re .compile (template )
2354
- filelist = list (filter (regexp .match , filelist ))
2355
- else :
2356
- raise ValueError ('template_expression value invalid' )
2357
-
2358
- if len (filelist ) == 0 :
2359
- # no files
2360
- msg = 'Output key: %s Template: %s returned no files' % (
2361
- key , template )
2362
- if self .inputs .raise_on_empty :
2363
- raise IOError (msg )
2364
- else :
2365
- warn (msg )
2366
- else :
2367
- # found files, sort and save to outputs
2368
- if self .inputs .sort_filelist :
2369
- filelist = human_order_sorted (filelist )
2370
- outputs [key ] = list_to_filename (filelist )
2371
-
2372
- # actually download the files, if desired
2373
- if self .inputs .download_files :
2374
- for f in filelist :
2375
- sftp .get (f , f )
2391
+ outputs [key ] = self ._get_files_over_ssh (template )
2376
2392
2377
2393
for argnum , arglist in enumerate (args ):
2378
2394
maxlen = 1
@@ -2401,44 +2417,7 @@ def _list_outputs(self):
2401
2417
except TypeError as e :
2402
2418
raise TypeError (e .message + ": Template %s failed to convert with args %s" % (template , str (tuple (argtuple ))))
2403
2419
2404
- # Connect over SSH
2405
- client = self ._get_ssh_client ()
2406
- sftp = client .open_sftp ()
2407
- sftp .chdir (self .inputs .base_directory )
2408
-
2409
- # Get all files in the dir, and filter for desired files
2410
- filledtemplate_dir = os .path .dirname (filledtemplate )
2411
- filledtemplate_base = os .path .basename (filledtemplate )
2412
- filelist = sftp .listdir (filledtemplate_dir )
2413
- if self .inputs .template_expression == 'fnmatch' :
2414
- outfiles = fnmatch .filter (filelist , filledtemplate_base )
2415
- elif self .inputs .template_expression == 'regexp' :
2416
- regexp = re .compile (filledtemplate_base )
2417
- outfiles = list (filter (regexp .match , filelist ))
2418
- else :
2419
- raise ValueError ('template_expression value invalid' )
2420
-
2421
- if len (outfiles ) == 0 :
2422
- # no files
2423
- msg = 'Output key: %s Template: %s returned no files' % (key , filledtemplate )
2424
- if self .inputs .raise_on_empty :
2425
- raise IOError (msg )
2426
- else :
2427
- warn (msg )
2428
- outputs [key ].append (None )
2429
- else :
2430
- # found files, sort and save to outputs
2431
- if self .inputs .sort_filelist :
2432
- outfiles = human_order_sorted (outfiles )
2433
- outputs [key ].append (list_to_filename (outfiles ))
2434
-
2435
- # actually download the files, if desired
2436
- if self .inputs .download_files :
2437
- for f in outfiles :
2438
- try :
2439
- sftp .get (os .path .join (filledtemplate_dir , f ), f )
2440
- except IOError :
2441
- iflogger .info ('remote file %s not found' % f )
2420
+ outputs [key ].append (self ._get_files_over_ssh (filledtemplate ))
2442
2421
2443
2422
# disclude where there was any invalid matches
2444
2423
if any ([val is None for val in outputs [key ]]):
0 commit comments