Skip to content

Commit

Permalink
Simplified PANFile init
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Sep 17, 2023
1 parent 37f7006 commit c01e290
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/PANFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 3 additions & 14 deletions src/hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from PANFile import PANFile
from patterns import CardPatterns

TEXT_FILE_SIZE_LIMIT: int = 1_073_741_824 # 1Gb


class Hunter:

Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit c01e290

Please sign in to comment.