Move checker.py into File class.

This commit is contained in:
Kevin Baensch 2024-11-23 22:48:02 +01:00
parent f76286da82
commit 360d36fa94
Signed by: derped
GPG key ID: C0F1D326C7626543
2 changed files with 28 additions and 32 deletions

View file

@ -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()

View file

@ -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()