Change Groomer private methods to public

* Changed safe_rmtree, safe_copy, safe_remove, and safe_mkdir to public methods
* If something is being used in a subclass it probably shouldn't be a private
method
pull/12/head
Dan Puttick 2017-03-07 15:31:32 -05:00
parent 12d5624b4d
commit fc8923fddd
3 changed files with 9 additions and 13 deletions

View File

@ -523,10 +523,10 @@ class KittenGroomerFileCheck(KittenGroomerBase):
self.process_archive(file)
else:
# TODO: Make a File attribute for should be copied True/False and check it
self._safe_copy()
self.safe_copy()
file.write_log()
if hasattr(file, "tempdir_path"):
self._safe_rmtree(file.tempdir_path)
self.safe_rmtree(file.tempdir_path)
def process_archive(self, file):
"""Unpacks an archive using 7zip and processes contents.
@ -546,7 +546,7 @@ class KittenGroomerFileCheck(KittenGroomerBase):
# LOG: check that tree is working correctly here
self.logger.tree(tempdir_path)
self.process_dir(tempdir_path, file.dst_path)
self._safe_rmtree(tempdir_path)
self.safe_rmtree(tempdir_path)
self.recursive_archive_depth -= 1
def _handle_archivebomb(self, file):

View File

@ -256,34 +256,30 @@ class KittenGroomerBase(object):
self.cur_file = None
self.logger = GroomerLogger(self.dst_root_dir, debug)
def _safe_rmtree(self, directory):
def safe_rmtree(self, directory):
"""Remove a directory tree if it exists."""
# TODO: should be a public method
if os.path.exists(directory):
shutil.rmtree(directory)
def _safe_remove(self, filepath):
def safe_remove(self, filepath):
"""Remove a file if it exists."""
# TODO: should be a public method
if os.path.exists(filepath):
os.remove(filepath)
def _safe_mkdir(self, directory):
def safe_mkdir(self, directory):
"""Make a directory if it does not exist."""
# TODO: should be a public method
if not os.path.exists(directory):
os.makedirs(directory)
def _safe_copy(self, src=None, dst=None):
def safe_copy(self, src=None, dst=None):
"""Copy a file and create directory if needed."""
# TODO: should be a public method
if src is None:
src = self.cur_file.src_path
if dst is None:
dst = self.cur_file.dst_path
try:
dst_path, filename = os.path.split(dst)
self._safe_mkdir(dst_path)
self.safe_mkdir(dst_path)
shutil.copy(src, dst)
return True
except Exception as e:

View File

@ -264,7 +264,7 @@ class TestKittenGroomerBase:
filedest = testdir.join('test.txt')
simple_groomer = KittenGroomerBase(tmpdir.strpath, testdir.strpath)
simple_groomer.cur_file = FileBase(file.strpath, filedest.strpath)
assert simple_groomer._safe_copy() is True
assert simple_groomer.safe_copy() is True
#check that it handles weird file path inputs
def test_list_all_files(self, tmpdir):