Move writing to log from File to Groomer

pull/21/head
Dan Puttick 2017-07-27 13:27:12 -04:00
parent 05179a7272
commit c1d3457562
2 changed files with 15 additions and 27 deletions

View File

@ -31,10 +31,9 @@ class File(FileBase):
filetype-specific processing methods. 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) super(File, self).__init__(src_path, dst_path)
self.is_archive = False self.is_archive = False
self.logger = logger
self.tempdir_path = self.dst_path + '_temp' self.tempdir_path = self.dst_path + '_temp'
subtypes_apps = ( subtypes_apps = (
@ -157,19 +156,6 @@ class File(FileBase):
if not self.is_dangerous: if not self.is_dangerous:
self.mime_processing_options.get(self.maintype, self.unknown)() 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 ##### # ##### Helper functions #####
def _make_method_dict(self, list_of_tuples): def _make_method_dict(self, list_of_tuples):
"""Returns a dictionary with mimetype: method pairs.""" """Returns a dictionary with mimetype: method pairs."""
@ -604,7 +590,7 @@ class KittenGroomerFileCheck(KittenGroomerBase):
self.logger.add_dir(srcpath) self.logger.add_dir(srcpath)
else: else:
dstpath = os.path.join(dst_dir, os.path.basename(srcpath)) 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) self.process_file(cur_file)
def process_file(self, file): def process_file(self, file):
@ -621,7 +607,7 @@ class KittenGroomerFileCheck(KittenGroomerBase):
if file.should_copy: if file.should_copy:
file.safe_copy() file.safe_copy()
file.set_property('copied', True) file.set_property('copied', True)
file.write_log() self.write_file_to_log(file)
# TODO: Can probably handle cleaning up the tempdir better # TODO: Can probably handle cleaning up the tempdir better
if hasattr(file, 'tempdir_path'): if hasattr(file, 'tempdir_path'):
self.safe_rmtree(file.tempdir_path) self.safe_rmtree(file.tempdir_path)
@ -642,7 +628,7 @@ class KittenGroomerFileCheck(KittenGroomerBase):
unpack_command = command_str.format(SEVENZ_PATH, unpack_command = command_str.format(SEVENZ_PATH,
file.src_path, tempdir_path) file.src_path, tempdir_path)
self._run_process(unpack_command) self._run_process(unpack_command)
file.write_log() self.write_file_to_log(file)
self.process_dir(tempdir_path, file.dst_path) self.process_dir(tempdir_path, file.dst_path)
self.safe_rmtree(tempdir_path) self.safe_rmtree(tempdir_path)
self.recursive_archive_depth -= 1 self.recursive_archive_depth -= 1
@ -657,6 +643,14 @@ class KittenGroomerFileCheck(KittenGroomerBase):
return return
return True 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): def list_files_dirs(self, root_dir_path):
""" """
Returns a list of all files and directories Returns a list of all files and directories

View File

@ -2,13 +2,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import unittest.mock as mock
import pytest import pytest
import yaml import yaml
try: try:
from bin.filecheck import KittenGroomerFileCheck, File, GroomerLogger from bin.filecheck import KittenGroomerFileCheck, File
NODEPS = False NODEPS = False
except ImportError: except ImportError:
NODEPS = True NODEPS = True
@ -107,20 +106,15 @@ def groomer(dest_dir_path):
return KittenGroomerFileCheck(dummy_src_path, dest_dir_path, debug=True) return KittenGroomerFileCheck(dummy_src_path, dest_dir_path, debug=True)
@fixture
def mock_logger(dest_dir_path):
return mock.MagicMock(spec=GroomerLogger)
@parametrize( @parametrize(
argnames="sample_file", argnames="sample_file",
argvalues=gather_sample_files(), argvalues=gather_sample_files(),
ids=get_filename) 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: if sample_file.xfail:
pytest.xfail(reason='Marked xfail in file catalog') pytest.xfail(reason='Marked xfail in file catalog')
file_dest_path = os.path.join(dest_dir_path, sample_file.filename) 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) groomer.process_file(file)
print(file.description_string) print(file.description_string)
print(file.mimetype) print(file.mimetype)