Quality of life improvements
Add helper functions to directly read/write bytes
This commit is contained in:
parent
98bcd88cb4
commit
5801539bb0
@ -16,6 +16,7 @@
|
||||
|
||||
import io
|
||||
import struct
|
||||
from typing import List
|
||||
|
||||
from .known_types import CslaKnownTypes
|
||||
from .serialization_info import ChildData, FieldData, SerializationInfo
|
||||
@ -23,11 +24,20 @@ from .serialization_info import ChildData, FieldData, SerializationInfo
|
||||
class CslaBinaryReader:
|
||||
"""Reads binary-serialised CSLA data into SerializationInfo objects"""
|
||||
|
||||
@classmethod
|
||||
def read_from_bytes(cls, data: bytes) -> List[SerializationInfo]:
|
||||
stream = io.BytesIO(data)
|
||||
reader = cls(stream)
|
||||
return reader.read()
|
||||
|
||||
# --------------
|
||||
# Implementation
|
||||
|
||||
def __init__(self, stream: io.BufferedIOBase):
|
||||
self.stream = stream
|
||||
self.keywords_dictionary = {}
|
||||
|
||||
def read(self):
|
||||
def read(self) -> List[SerializationInfo]:
|
||||
# CslaBinaryReader.Read
|
||||
total_count = self.read_int32()
|
||||
|
||||
|
@ -24,6 +24,16 @@ from .serialization_info import SerializationInfo
|
||||
class CslaBinaryWriter:
|
||||
"""Writes SerializationInfo objects into binary-serialised CSLA data"""
|
||||
|
||||
@classmethod
|
||||
def write_to_bytes(cls, serialisation_infos: List[SerializationInfo]) -> bytes:
|
||||
stream = io.BytesIO()
|
||||
writer = cls(stream)
|
||||
writer.write(serialisation_infos)
|
||||
return stream.getvalue()
|
||||
|
||||
# --------------
|
||||
# Implementation
|
||||
|
||||
def __init__(self, stream: io.BufferedIOBase):
|
||||
self.stream = stream
|
||||
self.keywords_dictionary = []
|
||||
|
@ -20,23 +20,23 @@ from typing import Any, List
|
||||
@dataclass
|
||||
class ChildData:
|
||||
# SerializationInfo.ChildData
|
||||
name: str
|
||||
is_dirty: bool
|
||||
reference_id: int
|
||||
name: str = ''
|
||||
is_dirty: bool = False
|
||||
reference_id: int = -1
|
||||
|
||||
@dataclass
|
||||
class FieldData:
|
||||
# SerializationInfo.FieldData
|
||||
name: str
|
||||
enum_type_name: str
|
||||
is_dirty: bool
|
||||
value: Any
|
||||
name: str = ''
|
||||
enum_type_name: str = None
|
||||
is_dirty: bool = False
|
||||
value: Any = None
|
||||
|
||||
@dataclass
|
||||
class SerializationInfo:
|
||||
"""Format-agnostic representation of serialised CSLA object"""
|
||||
|
||||
reference_id: int = 0
|
||||
reference_id: int = -1
|
||||
type_name: str = ''
|
||||
children: List[ChildData] = field(default_factory=list)
|
||||
values: List[FieldData] = field(default_factory=list)
|
||||
|
Loading…
x
Reference in New Issue
Block a user