fix: refactor libtw2 load that passes pyright & pylint

This commit is contained in:
ChillerDragon 2024-04-23 12:37:03 +08:00
parent 7b860510fd
commit d395cbfd1d

View file

@ -1,24 +1,23 @@
from twnet_parser.huffman_twnet_parser import huffman
HAS_LIBTW2 = False
LIBTW2_HUFFMAN = None
try:
import libtw2_huffman # type: ignore
HAS_LIBTW2 = True
import libtw2_huffman as LIBTW2_HUFFMAN # type: ignore
except ImportError:
HAS_LIBTW2 = False
LIBTW2_HUFFMAN = None
def backend_name() -> str:
if HAS_LIBTW2:
if LIBTW2_HUFFMAN:
return 'rust-libtw2'
return 'python-twnet_parser'
def compress(data: bytes) -> bytes:
if HAS_LIBTW2:
return libtw2_huffman.compress(data)
if LIBTW2_HUFFMAN:
return LIBTW2_HUFFMAN.compress(data)
return huffman.decompress(bytearray(data))
def decompress(data: bytes) -> bytes:
if HAS_LIBTW2:
return libtw2_huffman.decompress(data)
if LIBTW2_HUFFMAN:
return LIBTW2_HUFFMAN.decompress(data)
return huffman.decompress(bytearray(data))