54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from typing import Optional, Self
|
|
from os import path
|
|
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, cstype: str = ""):
|
|
self.fpath = fpath
|
|
self.fname = path.basename(fpath)
|
|
# TODO: Move out expected sum calculation
|
|
self.csum, self.esum = cktype.resolve(self.fname, esum, cstype)
|
|
|
|
def __repr__(self) -> str:
|
|
return self.fname
|
|
|
|
def __gt__(self, other: Self) -> bool:
|
|
return self.__repr__() > other.__repr__()
|
|
|
|
def mkFileStr(self) -> str:
|
|
return f"{self.csum}\t{self.fname}\n"
|
|
|
|
def verify(self) -> Optional[bool]:
|
|
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()
|