From d3656da4f206400863438200353b925ce03b8351 Mon Sep 17 00:00:00 2001 From: Dmytro Yaroshenko <73843436+o-murphy@users.noreply.github.com> Date: Tue, 7 Nov 2023 08:59:37 +0200 Subject: [PATCH] Dependencies fix (#7) * dependencies fix * makes validation optional --- a7p/__init__.py | 3 ++- a7p/a7p.py | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/a7p/__init__.py b/a7p/__init__.py index c6bfbcb..7fe5fd3 100644 --- a/a7p/__init__.py +++ b/a7p/__init__.py @@ -1,10 +1,11 @@ -__version__ = '0.0.7' +__version__ = '0.0.8' __author__ = "o-murphy" __credits__ = ["Dmytro Yaroshenko"] __copyright__ = ("",) try: from a7p import profedit_pb2, factory + from a7p import protovalidate as validator from a7p.a7p import A7PFile, A7PDataError from a7p.profedit_pb2 import * from a7p.factory import A7PFactory diff --git a/a7p/a7p.py b/a7p/a7p.py index 9eb0730..02d3628 100644 --- a/a7p/a7p.py +++ b/a7p/a7p.py @@ -16,32 +16,34 @@ class A7PDataError(Exception): class A7PFile: @staticmethod - def loads(string: bytes): + def loads(string: bytes, validate: bool = True): data = string[32:] md5_hash = hashlib.md5(data).hexdigest() if md5_hash == string[:32].decode(): profile = profedit_pb2.Payload() profile.ParseFromString(data) - validator.validate(profile) + if validate: + validator.validate(profile) return profile else: raise A7PDataError("Input data is missing for MD5 hashing") @staticmethod - def load(file: BinaryIO) -> profedit_pb2.Payload: + def load(file: BinaryIO, validate: bool = True) -> profedit_pb2.Payload: string = file.read() - return A7PFile.loads(string) + return A7PFile.loads(string, validate) @staticmethod - def dumps(profile: profedit_pb2.Payload) -> bytes: - validator.validate(profile) + def dumps(profile: profedit_pb2.Payload, validate: bool = True) -> bytes: + if validate: + validator.validate(profile) data = profile.SerializeToString() md5_hash = hashlib.md5(data).hexdigest().encode() return md5_hash + data @staticmethod - def dump(profile: profedit_pb2.Payload, file: BinaryIO) -> None: - data = A7PFile.dumps(profile) + def dump(profile: profedit_pb2.Payload, file: BinaryIO, validate: bool = True) -> None: + data = A7PFile.dumps(profile, validate) file.write(data) @staticmethod