From 51760ebbb1056a002c18b1f9b5ad2984ef514831 Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Fri, 17 Mar 2017 22:19:56 -0400 Subject: [PATCH] Move default log setup back into filecheck * Realized that the API consumer might want to write their own logging tool. * FileBase and KittenGroomerBase will have no logging code. * If the API consumer likes, they can import GroomerLogger and use it in their implementation. --- bin/filecheck.py | 14 ++++++++++---- kittengroomer/helpers.py | 15 +++------------ tests/logging.py | 21 ++++++++++----------- tests/test_kittengroomer.py | 6 +----- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/bin/filecheck.py b/bin/filecheck.py index da7e00b..0a9febe 100644 --- a/bin/filecheck.py +++ b/bin/filecheck.py @@ -16,7 +16,7 @@ from PIL import Image # from PIL import PngImagePlugin from pdfid import PDFiD, cPDFiD -from kittengroomer import FileBase, KittenGroomerBase +from kittengroomer import FileBase, KittenGroomerBase, GroomerLogger SEVENZ_PATH = '/usr/bin/7z' @@ -86,8 +86,9 @@ class Config: class File(FileBase): def __init__(self, src_path, dst_path, logger): - super(File, self).__init__(src_path, dst_path, logger) + super(File, self).__init__(src_path, dst_path) self.is_recursive = False + self.logger = logger subtypes_apps = [ (Config.mimes_office, self._winoffice), @@ -170,7 +171,7 @@ class File(FileBase): def _check_filename(self): if self.filename[0] is '.': - # handle dotfiles + # TODO: handle dotfiles? pass right_to_left_override = u"\u202E" if right_to_left_override in self.filename: @@ -188,6 +189,9 @@ class File(FileBase): if not self.is_dangerous: self.mime_processing_options.get(self.main_type, self.unknown)() + def write_log(self): + pass + # ##### Helper functions ##### def _make_method_dict(self, list_of_tuples): """Returns a dictionary with mimetype: method pairs.""" @@ -500,9 +504,11 @@ class File(FileBase): class KittenGroomerFileCheck(KittenGroomerBase): def __init__(self, root_src, root_dst, max_recursive_depth=2, debug=False): - super(KittenGroomerFileCheck, self).__init__(root_src, root_dst, debug) + super(KittenGroomerFileCheck, self).__init__(root_src, root_dst) self.recursive_archive_depth = 0 self.max_recursive_depth = max_recursive_depth + self.cur_file = None + self.logger = GroomerLogger(self.dst_root_dir, debug) def process_dir(self, src_dir, dst_dir): """Process a directory on the source key.""" diff --git a/kittengroomer/helpers.py b/kittengroomer/helpers.py index f193cd3..5fc5d7a 100644 --- a/kittengroomer/helpers.py +++ b/kittengroomer/helpers.py @@ -37,7 +37,7 @@ class FileBase(object): Contains file attributes and various helper methods. """ - def __init__(self, src_path, dst_path, logger=None): + def __init__(self, src_path, dst_path): """ Initialized with the source path and expected destination path. @@ -47,7 +47,6 @@ class FileBase(object): self.src_path = src_path self.dst_path = dst_path self.filename = os.path.basename(self.src_path) - self.logger = logger self._file_props = { 'filepath': self.src_path, 'filename': self.filename, @@ -261,11 +260,6 @@ class FileBase(object): self.add_error(e, '') return False - def write_log(self): - """Write logs from file to self.logger.""" - file_log = self.logger.add_file(self) - file_log.fields(**self._file_props) - class GroomerLogger(object): """Groomer logging interface.""" @@ -320,19 +314,16 @@ class GroomerLogger(object): def add_file(self, file): """Add a file to the log.""" - return self.log.name('file.src_path') + pass class KittenGroomerBase(object): """Base object responsible for copy/sanitization process.""" - def __init__(self, src_root_dir, dst_root_dir, debug=False): + def __init__(self, src_root_dir, dst_root_dir): """Initialized with path to source and dest directories.""" self.src_root_dir = src_root_dir self.dst_root_dir = dst_root_dir - self.debug = debug - self.cur_file = None - self.logger = GroomerLogger(self.dst_root_dir, debug) def safe_rmtree(self, directory): """Remove a directory tree if it exists.""" diff --git a/tests/logging.py b/tests/logging.py index c937a71..0b5237d 100644 --- a/tests/logging.py +++ b/tests/logging.py @@ -9,14 +9,13 @@ def save_logs(groomer, test_description): with open(groomer.logger.log_processing, 'r') as logfile: log = logfile.read() test_log.write(log) - if groomer.debug: - if os.path.exists(groomer.logger.log_debug_err): - test_log.write(divider.format('ERR LOG')) - with open(groomer.logger.log_debug_err, 'r') as debug_err: - err = debug_err.read() - test_log.write(err) - if os.path.exists(groomer.logger.log_debug_out): - test_log.write(divider.format('OUT LOG')) - with open(groomer.logger.log_debug_out, 'r') as debug_out: - out = debug_out.read() - test_log.write(out) + if os.path.exists(groomer.logger.log_debug_err): + test_log.write(divider.format('ERR LOG')) + with open(groomer.logger.log_debug_err, 'r') as debug_err: + err = debug_err.read() + test_log.write(err) + if os.path.exists(groomer.logger.log_debug_out): + test_log.write(divider.format('OUT LOG')) + with open(groomer.logger.log_debug_out, 'r') as debug_out: + out = debug_out.read() + test_log.write(out) diff --git a/tests/test_kittengroomer.py b/tests/test_kittengroomer.py index 940136d..88500b7 100644 --- a/tests/test_kittengroomer.py +++ b/tests/test_kittengroomer.py @@ -6,7 +6,6 @@ import os import pytest from kittengroomer import FileBase, KittenGroomerBase, GroomerLogger -from kittengroomer.helpers import ImplementationRequired skip = pytest.mark.skip xfail = pytest.mark.xfail @@ -245,10 +244,7 @@ class TestKittenGroomerBase: assert generic_groomer def test_instantiation(self, source_directory, dest_directory): - groomer = KittenGroomerBase(source_directory, dest_directory) - debug_groomer = KittenGroomerBase(source_directory, - dest_directory, - debug=True) + KittenGroomerBase(source_directory, dest_directory) def test_list_all_files(self, tmpdir): file = tmpdir.join('test.txt')