From 360d36fa94bed043a91a4b8a7a7d519fb81f6913 Mon Sep 17 00:00:00 2001 From: derped Date: Sat, 23 Nov 2024 22:48:02 +0100 Subject: [PATCH] Move checker.py into File class. --- fck/checker.py | 32 -------------------------------- fck/file.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 fck/checker.py diff --git a/fck/checker.py b/fck/checker.py deleted file mode 100644 index 74a5590..0000000 --- a/fck/checker.py +++ /dev/null @@ -1,32 +0,0 @@ -from typing import Generator, List, Tuple, Optional -from .file import FILE -import re - - -def checkmark(value: Optional[bool] = None) -> str: - """ - Takes optional bool and returns colored string. - """ - return { - True: '\033[92m✓\033[0m', - False: '\033[91m❌\033[0m', - None: '\033[33m?\033[0m' - }[value] - - -def check(f: FILE, largefile: bool = False) -> bool: - """ - Check given file and return checked file. - """ - f.csum.reset() - try: - with open(f.fpath, 'rb') as file: - if not largefile: - f.csum.gensum(file.read()) - else: - for line in file: - f.csum.gensum(line) - except FileNotFoundError: - print(f"[WARN]: No such file or directory: {f.fpath}") - print(f"{checkmark(f.verify())} \t {f.csum} \t {f.esum} \t {f.fname}") - return f.verify() diff --git a/fck/file.py b/fck/file.py index 812252c..c53f669 100644 --- a/fck/file.py +++ b/fck/file.py @@ -4,6 +4,15 @@ from . import cktype class FILE: + """ + Colored checkmark representation. + """ + CHECKMARK = { + True: "\033[92m✓\033[0m", + False: "\033[91m❌\033[0m", + None: "\033[33m?\033[0m", + } + def __init__(self, fpath: str, esum: Optional[str] = None): self.fpath = fpath self.fname = path.basename(fpath) @@ -16,3 +25,22 @@ class FILE: if self.esum is None: return None return self.csum.__repr__() == self.esum + + def check(self, largefile: bool = False) -> Optional[bool]: + """ + Check given file and return checked file. + """ + self.csum.reset() + try: + with open(self.fpath, "rb") as file: + if not largefile: + self.csum.gensum(file.read()) + else: + for line in file: + self.csum.gensum(line) + except FileNotFoundError: + print(f"[WARN]: No such file or directory: {self.fpath}") + print( + f"{self.CHECKMARK[self.verify()]} \t {self.csum} \t {self.esum} \t {self.fname}" + ) + return self.verify()