1515import shutil
1616from collections import defaultdict
1717from pathlib import Path
18+ import warnings
1819
1920try :
2021 import ConfigParser as ConfigParser
5354
5455logger = logging .getLogger ("reusables" )
5556
57+ scandir_warning_given = False
58+
59+
60+ def scandir_warning ():
61+ global scandir_warning_given
62+ scandir_warning_given = True
63+ warnings .warn (
64+ '"enable_scandir" option will be removed in the next release of Reusables.'
65+ ' Please remove all references to "enable_scandir" or version lock to Reusables < 1.0.0'
66+ )
67+
5668
5769def extract (archive_file , path = "." , delete_on_success = False , enable_rar = False ):
5870 """
@@ -124,7 +136,7 @@ def archive(
124136 allow_zip_64 = True ,
125137 ** tarfile_kwargs ,
126138):
127- """ Archive a list of files (or files inside a folder), can chose between
139+ """Archive a list of files (or files inside a folder), can chose between
128140
129141 - zip
130142 - tar
@@ -166,8 +178,7 @@ def archive(
166178 raise ValueError (err_msg )
167179 logger .debug ("{0} file detected for {1}" .format (archive_type , name ))
168180 elif archive_type not in ("tar" , "gz" , "bz2" , "zip" , "lzma" ):
169- err_msg = ("archive_type must be zip, gz, bz2,"
170- " or gz, was {0}" .format (archive_type ))
181+ err_msg = "archive_type must be zip, gz, bz2, lzma, or gz, was {0}" .format (archive_type )
171182 logger .error (err_msg )
172183 raise ValueError (err_msg )
173184
@@ -183,16 +194,14 @@ def archive(
183194 elif store :
184195 compression = zipfile .ZIP_STORED
185196
186- arch = zipfile .ZipFile (name , 'w' ,
187- compression ,
188- allowZip64 = allow_zip_64 )
197+ arch = zipfile .ZipFile (name , "w" , compression , allowZip64 = allow_zip_64 )
189198 write = arch .write
190199 elif archive_type in ("tar" , "gz" , "bz2" ):
191200 mode = archive_type if archive_type != "tar" else ""
192201 arch = tarfile .open (name , "w:{0}" .format (mode ), ** tarfile_kwargs )
193202 write = arch .add
194203 else :
195- raise ValueError ("archive_type must be zip, gz, bz2, or gz" )
204+ raise ValueError ("archive_type must be zip, gz, bz2, lzma, or gz" )
196205
197206 try :
198207 for file_path in files_to_archive :
@@ -396,7 +405,7 @@ def config_namespace(config_file=None, auto_find=False, verify=True, **cfg_optio
396405 return ConfigNamespace (** config_dict (config_file , auto_find , verify , ** cfg_options ))
397406
398407
399- def os_tree (directory ):
408+ def os_tree (directory , enable_scandir = False ):
400409 """
401410 Return a directories contents as a dictionary hierarchy.
402411
@@ -413,6 +422,9 @@ def os_tree(directory):
413422 :param directory: path to directory to created the tree of.
414423 :return: dictionary of the directory
415424 """
425+ if enable_scandir and not scandir_warning_given :
426+ scandir_warning ()
427+
416428 if not os .path .exists (directory ):
417429 raise OSError ("Directory does not exist" )
418430 if not os .path .isdir (directory ):
@@ -484,6 +496,7 @@ def find_files(
484496 disable_glob = False ,
485497 depth = None ,
486498 abspath = False ,
499+ enable_scandir = False ,
487500 disable_pathlib = False ,
488501):
489502 """
@@ -525,6 +538,8 @@ def find_files(
525538 :param disable_pathlib: only return string, not path objects
526539 :return: generator of all files in the specified directory
527540 """
541+ if enable_scandir and not scandir_warning_given :
542+ scandir_warning ()
528543
529544 def pathed (path ):
530545 if disable_pathlib :
@@ -571,7 +586,7 @@ def pathed(path):
571586 yield pathed (os .path .join (root , file_name ))
572587
573588
574- def remove_empty_directories (root_directory , dry_run = False , ignore_errors = True ):
589+ def remove_empty_directories (root_directory , dry_run = False , ignore_errors = True , enable_scandir = False ):
575590 """
576591 Remove all empty folders from a path. Returns list of empty directories.
577592
@@ -580,6 +595,8 @@ def remove_empty_directories(root_directory, dry_run=False, ignore_errors=True):
580595 :param ignore_errors: Permissions are a pain, just ignore if you blocked
581596 :return: list of removed directories
582597 """
598+ if enable_scandir and not scandir_warning_given :
599+ scandir_warning ()
583600
584601 directory_list = []
585602 for root , directories , files in os .walk (root_directory , topdown = False ):
@@ -609,7 +626,7 @@ def remove_empty_directories(root_directory, dry_run=False, ignore_errors=True):
609626 return directory_list
610627
611628
612- def remove_empty_files (root_directory , dry_run = False , ignore_errors = True ):
629+ def remove_empty_files (root_directory , dry_run = False , ignore_errors = True , enable_scandir = False ):
613630 """
614631 Remove all empty files from a path. Returns list of the empty files removed.
615632
@@ -618,6 +635,9 @@ def remove_empty_files(root_directory, dry_run=False, ignore_errors=True):
618635 :param ignore_errors: Permissions are a pain, just ignore if you blocked
619636 :return: list of removed files
620637 """
638+ if enable_scandir and not scandir_warning_given :
639+ scandir_warning ()
640+
621641 file_list = []
622642 for root , directories , files in os .walk (root_directory ):
623643 for file_name in files :
@@ -641,7 +661,7 @@ def remove_empty_files(root_directory, dry_run=False, ignore_errors=True):
641661 return file_list
642662
643663
644- def dup_finder (file_path , directory = "." ):
664+ def dup_finder (file_path , directory = "." , enable_scandir = False ):
645665 """
646666 Check a directory for duplicates of the specified file. This is meant
647667 for a single file only, for checking a directory for dups, use
@@ -668,6 +688,9 @@ def dup_finder(file_path, directory="."):
668688 :param directory: Directory to dig recursively into to look for duplicates
669689 :return: generators
670690 """
691+ if enable_scandir and not scandir_warning_given :
692+ scandir_warning ()
693+
671694 size = os .path .getsize (file_path )
672695 if size == 0 :
673696 for empty_file in remove_empty_files (directory , dry_run = True ):
0 commit comments