@@ -2412,6 +2412,55 @@ def __init__(self, infields=None, outfields=None, **kwargs):
2412
2412
and self .inputs .template [- 1 ] != '$' ):
2413
2413
self .inputs .template += '$'
2414
2414
2415
+ def _get_files_over_ssh (self , template ):
2416
+ """Get the files matching template over an SSH connection."""
2417
+ # Connect over SSH
2418
+ client = self ._get_ssh_client ()
2419
+ sftp = client .open_sftp ()
2420
+ sftp .chdir (self .inputs .base_directory )
2421
+
2422
+ # Get all files in the dir, and filter for desired files
2423
+ template_dir = os .path .dirname (template )
2424
+ template_base = os .path .basename (template )
2425
+ filelist = sftp .listdir (template_dir )
2426
+ if self .inputs .template_expression == 'fnmatch' :
2427
+ outfiles = fnmatch .filter (filelist , template_base )
2428
+ elif self .inputs .template_expression == 'regexp' :
2429
+ regexp = re .compile (template_base )
2430
+ outfiles = list (filter (regexp .match , filelist ))
2431
+ else :
2432
+ raise ValueError ('template_expression value invalid' )
2433
+
2434
+ if len (outfiles ) == 0 :
2435
+ # no files
2436
+ msg = 'Output key: %s Template: %s returned no files' % (
2437
+ key , template )
2438
+ if self .inputs .raise_on_empty :
2439
+ raise IOError (msg )
2440
+ else :
2441
+ warn (msg )
2442
+
2443
+ # return value
2444
+ outfiles = None
2445
+
2446
+ else :
2447
+ # found files, sort and save to outputs
2448
+ if self .inputs .sort_filelist :
2449
+ outfiles = human_order_sorted (outfiles )
2450
+
2451
+ # actually download the files, if desired
2452
+ if self .inputs .download_files :
2453
+ for f in outfiles :
2454
+ try :
2455
+ sftp .get (os .path .join (template_dir , f ), f )
2456
+ except IOError :
2457
+ iflogger .info ('remote file %s not found' % f )
2458
+
2459
+ # return value
2460
+ outfiles = list_to_filename (outfiles )
2461
+
2462
+ return outfiles
2463
+
2415
2464
def _list_outputs (self ):
2416
2465
try :
2417
2466
paramiko
@@ -2441,39 +2490,7 @@ def _list_outputs(self):
2441
2490
template = self .inputs .field_template [key ]
2442
2491
2443
2492
if not args :
2444
- # Connect over SSH
2445
- client = self ._get_ssh_client ()
2446
- sftp = client .open_sftp ()
2447
-
2448
- # Get the files in the base dir, and filter for desired files
2449
- sftp .chdir (self .inputs .base_directory )
2450
- filelist = sftp .listdir ()
2451
- if self .inputs .template_expression == 'fnmatch' :
2452
- filelist = fnmatch .filter (filelist , template )
2453
- elif self .inputs .template_expression == 'regexp' :
2454
- regexp = re .compile (template )
2455
- filelist = list (filter (regexp .match , filelist ))
2456
- else :
2457
- raise ValueError ('template_expression value invalid' )
2458
-
2459
- if len (filelist ) == 0 :
2460
- # no files
2461
- msg = 'Output key: %s Template: %s returned no files' % (
2462
- key , template )
2463
- if self .inputs .raise_on_empty :
2464
- raise IOError (msg )
2465
- else :
2466
- warn (msg )
2467
- else :
2468
- # found files, sort and save to outputs
2469
- if self .inputs .sort_filelist :
2470
- filelist = human_order_sorted (filelist )
2471
- outputs [key ] = list_to_filename (filelist )
2472
-
2473
- # actually download the files, if desired
2474
- if self .inputs .download_files :
2475
- for f in filelist :
2476
- sftp .get (f , f )
2493
+ outputs [key ] = self ._get_files_over_ssh (template )
2477
2494
2478
2495
for argnum , arglist in enumerate (args ):
2479
2496
maxlen = 1
@@ -2509,49 +2526,7 @@ def _list_outputs(self):
2509
2526
": Template %s failed to convert with args %s"
2510
2527
% (template , str (tuple (argtuple ))))
2511
2528
2512
- # Connect over SSH
2513
- client = self ._get_ssh_client ()
2514
- sftp = client .open_sftp ()
2515
- sftp .chdir (self .inputs .base_directory )
2516
-
2517
- # Get all files in the dir, and filter for desired files
2518
- filledtemplate_dir = os .path .dirname (filledtemplate )
2519
- filledtemplate_base = os .path .basename (filledtemplate )
2520
- filelist = sftp .listdir (filledtemplate_dir )
2521
- if self .inputs .template_expression == 'fnmatch' :
2522
- outfiles = fnmatch .filter (filelist ,
2523
- filledtemplate_base )
2524
- elif self .inputs .template_expression == 'regexp' :
2525
- regexp = re .compile (filledtemplate_base )
2526
- outfiles = list (filter (regexp .match , filelist ))
2527
- else :
2528
- raise ValueError ('template_expression value invalid' )
2529
-
2530
- if len (outfiles ) == 0 :
2531
- msg = 'Output key: %s Template: %s returned no files' % (
2532
- key , filledtemplate )
2533
-
2534
- # no files
2535
- if self .inputs .raise_on_empty :
2536
- raise IOError (msg )
2537
- else :
2538
- warn (msg )
2539
- outputs [key ].append (None )
2540
- else :
2541
- # found files, sort and save to outputs
2542
- if self .inputs .sort_filelist :
2543
- outfiles = human_order_sorted (outfiles )
2544
- outputs [key ].append (list_to_filename (outfiles ))
2545
-
2546
- # actually download the files, if desired
2547
- if self .inputs .download_files :
2548
- for f in outfiles :
2549
- try :
2550
- sftp .get (
2551
- os .path .join (filledtemplate_dir , f ), f )
2552
- except IOError :
2553
- iflogger .info ('remote file %s not found' ,
2554
- f )
2529
+ outputs [key ].append (self ._get_files_over_ssh (filledtemplate ))
2555
2530
2556
2531
# disclude where there was any invalid matches
2557
2532
if any ([val is None for val in outputs [key ]]):
0 commit comments