From fc8923fddd10430c9706edc897ed7585c2b4db2c Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Tue, 7 Mar 2017 15:31:32 -0500 Subject: [PATCH] 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 --- bin/filecheck.py | 6 +++--- kittengroomer/helpers.py | 14 +++++--------- tests/test_kittengroomer.py | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/bin/filecheck.py b/bin/filecheck.py index a49297d..a0c4f6f 100644 --- a/bin/filecheck.py +++ b/bin/filecheck.py @@ -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): diff --git a/kittengroomer/helpers.py b/kittengroomer/helpers.py index caaedff..cf9e098 100644 --- a/kittengroomer/helpers.py +++ b/kittengroomer/helpers.py @@ -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: diff --git a/tests/test_kittengroomer.py b/tests/test_kittengroomer.py index 4497728..5866aaf 100644 --- a/tests/test_kittengroomer.py +++ b/tests/test_kittengroomer.py @@ -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):