From c1d34575626f84b1cf34a4bf48f480f7b8f3edd7 Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Thu, 27 Jul 2017 13:27:12 -0400 Subject: [PATCH] Move writing to log from File to Groomer --- bin/filecheck.py | 30 ++++++++++++------------------ tests/test_filecheck.py | 12 +++--------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/bin/filecheck.py b/bin/filecheck.py index b445c5f..61dcbfe 100644 --- a/bin/filecheck.py +++ b/bin/filecheck.py @@ -31,10 +31,9 @@ class File(FileBase): filetype-specific processing methods. """ - def __init__(self, src_path, dst_path, logger=None): + def __init__(self, src_path, dst_path): super(File, self).__init__(src_path, dst_path) self.is_archive = False - self.logger = logger self.tempdir_path = self.dst_path + '_temp' subtypes_apps = ( @@ -157,19 +156,6 @@ class File(FileBase): if not self.is_dangerous: self.mime_processing_options.get(self.maintype, self.unknown)() - def write_log(self): - """Pass information about the file to self.logger.""" - if self.logger: - props = self.get_all_props() - if not self.is_archive: - if os.path.exists(self.tempdir_path): - # FIXME: in_tempdir is a hack to make image files appear at the correct tree depth in log - self.logger.add_file(self.src_path, props, in_tempdir=True) - return - self.logger.add_file(self.src_path, props) - else: - raise Warning("No logger associated with this File") - # ##### Helper functions ##### def _make_method_dict(self, list_of_tuples): """Returns a dictionary with mimetype: method pairs.""" @@ -604,7 +590,7 @@ class KittenGroomerFileCheck(KittenGroomerBase): self.logger.add_dir(srcpath) else: dstpath = os.path.join(dst_dir, os.path.basename(srcpath)) - cur_file = File(srcpath, dstpath, self.logger) + cur_file = File(srcpath, dstpath) self.process_file(cur_file) def process_file(self, file): @@ -621,7 +607,7 @@ class KittenGroomerFileCheck(KittenGroomerBase): if file.should_copy: file.safe_copy() file.set_property('copied', True) - file.write_log() + self.write_file_to_log(file) # TODO: Can probably handle cleaning up the tempdir better if hasattr(file, 'tempdir_path'): self.safe_rmtree(file.tempdir_path) @@ -642,7 +628,7 @@ class KittenGroomerFileCheck(KittenGroomerBase): unpack_command = command_str.format(SEVENZ_PATH, file.src_path, tempdir_path) self._run_process(unpack_command) - file.write_log() + self.write_file_to_log(file) self.process_dir(tempdir_path, file.dst_path) self.safe_rmtree(tempdir_path) self.recursive_archive_depth -= 1 @@ -657,6 +643,14 @@ class KittenGroomerFileCheck(KittenGroomerBase): return return True + def write_file_to_log(self, file): + """Pass information about `file` to self.logger.""" + props = file.get_all_props() + if not file.is_archive: + # FIXME: in_tempdir is a hack to make image files appear at the correct tree depth in log + in_tempdir = os.path.exists(file.tempdir_path) + self.logger.add_file(file.src_path, props, in_tempdir) + def list_files_dirs(self, root_dir_path): """ Returns a list of all files and directories diff --git a/tests/test_filecheck.py b/tests/test_filecheck.py index 066a5ae..b9f1b9a 100644 --- a/tests/test_filecheck.py +++ b/tests/test_filecheck.py @@ -2,13 +2,12 @@ # -*- coding: utf-8 -*- import os -import unittest.mock as mock import pytest import yaml try: - from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger + from bin.filecheck import KittenGroomerFileCheck, File NODEPS = False except ImportError: NODEPS = True @@ -107,20 +106,15 @@ def groomer(dest_dir_path): return KittenGroomerFileCheck(dummy_src_path, dest_dir_path, debug=True) -@fixture -def mock_logger(dest_dir_path): - return mock.MagicMock(spec=GroomerLogger) - - @parametrize( argnames="sample_file", argvalues=gather_sample_files(), ids=get_filename) -def test_sample_files(mock_logger, sample_file, groomer, dest_dir_path): +def test_sample_files(sample_file, groomer, dest_dir_path): if sample_file.xfail: pytest.xfail(reason='Marked xfail in file catalog') file_dest_path = os.path.join(dest_dir_path, sample_file.filename) - file = File(sample_file.path, file_dest_path, mock_logger) + file = File(sample_file.path, file_dest_path) groomer.process_file(file) print(file.description_string) print(file.mimetype)