Update cktype resolution and crc32 interface.

This commit is contained in:
Kevin Baensch 2024-11-23 23:29:14 +01:00
parent 82e1d2b3af
commit f7151eba30
Signed by: derped
GPG key ID: C0F1D326C7626543
2 changed files with 33 additions and 21 deletions

View file

@ -1,19 +1,30 @@
from re import search from typing import Dict, Optional, Type
from typing import Optional
from fck.cktype.cktypeinterface import CKTYPEINTERFACE as CKTYPEINTERFACE
from .crc32 import CRC32 from .crc32 import CRC32
CKTYPES = [ CKTYPES: Dict[str, Type[MD5 | CRC32]] = {
(CRC32) "CRC32": (CRC32),
] }
def resolve(fname: str, esum: Optional[str] = None, cstype: str = ""): def resolve(fname: str, esum: Optional[str] = None, cstype: str = ""):
""" """
Checks fname input for checksum pattern and returns first match. Checks fname input for checksum patterns and returns first match.
Can be overridden with cstype. Can be overridden with cstype.
If neither is applicable the first possible checksum type will be returned. If neither is applicable it defaults to CRC32.
""" """
if esum is None and (match := CKTYPES[0].REGEX.search(fname)): cstype = cstype.upper()
esum = match.group(0) if cstype in CKTYPES:
if esum is None and (match := CKTYPES[cstype].REGEX.search(fname)):
esum = match.group(0)
return CKTYPES[cstype](), esum
return CKTYPES[0](), esum for cstname, csclass in CKTYPES.items():
if match := csclass.REGEX.search(fname):
if esum is None:
esum = match.group(0)
return csclass(), esum
return CRC32(), esum

View file

@ -1,16 +1,20 @@
from typing import Generator, List, Tuple, Optional from typing import List
from re import compile, Pattern from re import compile, Pattern
from zlib import crc32 from zlib import crc32
from .cktypeinterface import CKTYPEINTERFACE
class CRC32(object): class CRC32(CKTYPEINTERFACE):
NAME: str = "CRC32" NAME: str = "CRC32"
EXT: List[str] = [".sfv"] EXT: List[str] = [".sfv"]
SYNTAX: List[Pattern] = [ SYNTAX: List[Pattern] = [
compile(r'^;*$'), # comment lines
compile(r'^.* [0-9a-fA-F]{8}$') compile(r"^;*$"),
# filepath checksum pair
compile(r"^.* [0-9a-fA-F]{8}$"),
] ]
REGEX: Pattern = compile(r'[0-9a-fA-F]{8}') REGEX: Pattern = compile(r"[0-9a-fA-F]{8}")
NULL: str = "00000000"
def __init__(self): def __init__(self):
self.cksum: int = 0 self.cksum: int = 0
@ -22,8 +26,5 @@ class CRC32(object):
self.__init__() self.__init__()
def __repr__(self) -> str: def __repr__(self) -> str:
cstring = str(hex(self.cksum))[2:].upper() cstring = format(self.cksum, "x").upper()
return ''.join([((8 - len(cstring)) * "0"), cstring]) return cstring.rjust(8, "0")
def __eq__(self, other) -> bool:
return self.__repr__() == other.upper()