From c01e29026d03cde69ffcc9c52fadefc865ee453c Mon Sep 17 00:00:00 2001 From: Zafer Balkan Date: Sun, 17 Sep 2023 15:20:07 +0300 Subject: [PATCH] Simplified PANFile init --- src/PANFile.py | 8 ++++++++ src/hunter.py | 17 +++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/PANFile.py b/src/PANFile.py index 01dc71f..c458d24 100644 --- a/src/PANFile.py +++ b/src/PANFile.py @@ -7,6 +7,8 @@ from dispatcher import Dispatcher from enums import ScanStatusEnum +TEXT_FILE_SIZE_LIMIT: int = 1_073_741_824 # 1Gb + class PANFile: """ PANFile: class for a file that can check itself for PANs""" @@ -49,6 +51,12 @@ def __init__(self, filename: str, file_dir: str) -> None: self.set_error( f'Failed to detect mimetype and encoding. Inner exception: {ex}') + self.set_file_stats() + + if self.size > TEXT_FILE_SIZE_LIMIT: + self.set_error( + error_msg=f'File size {panutils.size_friendly(size=self.size)} over limit of {panutils.size_friendly(size=TEXT_FILE_SIZE_LIMIT)} for checking for file \"{self.filename}\"') + def __cmp__(self, other: 'PANFile') -> bool: return self.path.lower() == other.path.lower() diff --git a/src/hunter.py b/src/hunter.py index a99ae6b..a97327b 100644 --- a/src/hunter.py +++ b/src/hunter.py @@ -7,8 +7,6 @@ from PANFile import PANFile from patterns import CardPatterns -TEXT_FILE_SIZE_LIMIT: int = 1_073_741_824 # 1Gb - class Hunter: @@ -25,7 +23,7 @@ def get_files(self) -> tuple: # tuple[PANFile, ...]: return tuple(self.__all_files) def add_single_file(self, filename: str, dir: str) -> None: - file: PANFile = self.__try_init_PANfile(filename=filename, dir=dir) + file: PANFile = PANFile(filename=filename, file_dir=dir) self.__all_files.append(file) # def get_scannable_files(self) -> Generator[tuple[int, int, int], None, None]: @@ -57,8 +55,8 @@ def get_scannable_files(self) -> Generator[tuple, None, None]: for filename in files: if root == self.__conf.search_dir: root_items_completed += 1 - pan_file: PANFile = self.__try_init_PANfile( - filename=filename, dir=root) + pan_file: PANFile = PANFile( + filename=filename, file_dir=root) doc_files.append(pan_file) if not pan_file.errors: docs_found += 1 @@ -83,12 +81,3 @@ def scan_files(self) -> Generator[tuple, None, None]: matches_found += len(matches) files_completed += 1 yield matches_found, files_completed - - def __try_init_PANfile(self, filename: str, dir: str) -> PANFile: - pan_file = PANFile(filename=filename, file_dir=dir) - pan_file.set_file_stats() - if pan_file.size > TEXT_FILE_SIZE_LIMIT: - pan_file.set_error( - error_msg=f'File size {panutils.size_friendly(size=pan_file.size)} over limit of {panutils.size_friendly(size=TEXT_FILE_SIZE_LIMIT)} for checking for file \"{filename}\"') - - return pan_file