Update cktype resolution and crc32 interface.
This commit is contained in:
parent
82e1d2b3af
commit
f7151eba30
2 changed files with 33 additions and 21 deletions
|
@ -1,19 +1,30 @@
|
|||
from re import search
|
||||
from typing import Optional
|
||||
from typing import Dict, Optional, Type
|
||||
|
||||
from fck.cktype.cktypeinterface import CKTYPEINTERFACE as CKTYPEINTERFACE
|
||||
|
||||
from .crc32 import CRC32
|
||||
|
||||
CKTYPES = [
|
||||
(CRC32)
|
||||
]
|
||||
CKTYPES: Dict[str, Type[MD5 | CRC32]] = {
|
||||
"CRC32": (CRC32),
|
||||
}
|
||||
|
||||
|
||||
def resolve(fname: str, esum: Optional[str] = None, cstype: str = ""):
|
||||
"""
|
||||
Checks fname input for checksum pattern and returns first match.
|
||||
Can be overridden with cstype.
|
||||
If neither is applicable the first possible checksum type will be returned.
|
||||
Checks fname input for checksum patterns and returns first match.
|
||||
Can be overridden with cstype.
|
||||
If neither is applicable it defaults to CRC32.
|
||||
"""
|
||||
if esum is None and (match := CKTYPES[0].REGEX.search(fname)):
|
||||
esum = match.group(0)
|
||||
cstype = cstype.upper()
|
||||
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
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
from typing import Generator, List, Tuple, Optional
|
||||
from typing import List
|
||||
from re import compile, Pattern
|
||||
from zlib import crc32
|
||||
from .cktypeinterface import CKTYPEINTERFACE
|
||||
|
||||
|
||||
class CRC32(object):
|
||||
class CRC32(CKTYPEINTERFACE):
|
||||
NAME: str = "CRC32"
|
||||
EXT: List[str] = [".sfv"]
|
||||
SYNTAX: List[Pattern] = [
|
||||
compile(r'^;*$'),
|
||||
compile(r'^.* [0-9a-fA-F]{8}$')
|
||||
# comment lines
|
||||
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):
|
||||
self.cksum: int = 0
|
||||
|
@ -22,8 +26,5 @@ class CRC32(object):
|
|||
self.__init__()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
cstring = str(hex(self.cksum))[2:].upper()
|
||||
return ''.join([((8 - len(cstring)) * "0"), cstring])
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
return self.__repr__() == other.upper()
|
||||
cstring = format(self.cksum, "x").upper()
|
||||
return cstring.rjust(8, "0")
|
||||
|
|
Loading…
Reference in a new issue