@@ -526,38 +526,35 @@ def read_header(file):
526526 return header_data [1 ].values (),header_data [3 ]
527527
528528
529- def allow (file , include_regex = None ):
530- name , ext = os .path .splitext (file )
531- if ext .lower () != ".sc2replay" :
532- return False
533- elif include_regex and not re .match (include_regex ,name ):
534- return False
535- else :
536- return True
529+ def _allow (filename , regex ):
530+ name , ext = os .path .splitext (filename )
531+ if ext .lower () != ".sc2replay" : return False
532+ return re .match (regex , name ) if regex else True
537533
538- def get_files ( location , include_regex = None ,
539- exclude_dirs = [], depth = - 1 , follow_symlinks = True , ** extras ):
534+ def get_files ( path , regex = None , exclude = [] ,
535+ depth = - 1 , followlinks = False , ** extras ):
540536
541- if not os .path .exists (location ):
542- raise ValueError ("Location `{0}` does not exit" .format (location ))
537+ #os.walk and os.path.isfile fail silently. We want to be loud!
538+ if not os .path .exists (path ):
539+ raise ValueError ("Location `{0}` does not exist" .format (path ))
543540
544- if not os .path .isdir (location ):
545- if allow (location , include_regex ):
546- return [location ]
541+ # Curry the function to prime it for use in the filter function
542+ allow = lambda filename : _allow (filename , regex )
543+
544+ # You can't get more than one file from a file name!
545+ if os .path .isfile (path ):
546+ return [path ] if allow (os .path .basename (path )) else []
547547
548548 files = list ()
549- for file in os .listdir (location ):
550- path = os .path .join (location , file )
551- if os .path .isdir (path ):
552- if file in exclude_dirs or depth == 0 :
553- continue
554- elif not follow_symlinks and os .path .islink (path ):
555- continue
556- else :
557- files .extend (get_files (path , include_regex ,
558- exclude_dirs , depth - 1 , follow_symlinks ))
559- elif os .path .isfile (path ):
560- if allow (file , include_regex ):
561- files .append (path )
549+ for root , directories , filenames in os .walk (path , followlinks = followlinks ):
550+ # Exclude the indicated directories by removing them from `directories`
551+ for index ,directory in enumerate (directories ):
552+ if directory in exclude or depth == 0 :
553+ del directories [index ]
554+
555+ # Extend our return value only with the allowed file type and regex
556+ allowed_files = filter (allow , filenames )
557+ files .extend (os .path .join (root ,file ) for file in allowed_files )
558+ depth -= 1
562559
563560 return files
0 commit comments