Skip to content

Commit

Permalink
Dependencies fix (#7)
Browse files Browse the repository at this point in the history
* dependencies fix
* makes validation optional
  • Loading branch information
o-murphy authored Nov 7, 2023
1 parent 932ad51 commit d3656da
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion a7p/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 10 additions & 8 deletions a7p/a7p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3656da

Please sign in to comment.