Skip to content

Commit

Permalink
fix lint and error in fb deserializer_gen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Krande committed Sep 27, 2024
1 parent d712980 commit d1d7dfa
Show file tree
Hide file tree
Showing 25 changed files with 352 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/scripts/animate_glb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def main():
]
keyframe_times = [0, 1, 3, 4, 6] # Example keyframe times

tri_anim = Animation("TestAnimation", keyframe_times, translation_keyframes, rotational_keyframes)
tri_anim = Animation("TestAnimation", keyframe_times, translation_keyframes, rotational_keyframes)
a.animation_store.add(tri_anim)

# Set your initial camera position
Expand Down
92 changes: 61 additions & 31 deletions src/ada/comms/fb_deserializer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from ada.comms.fb_model_gen import (
CameraParamsDC,
CommandTypeDC,
ErrorDC,
FileObjectDC,
FilePurposeDC,
FileTypeDC,
MeshInfoDC,
MessageDC,
ParameterDC,
ProcedureDC,
ProcedureStoreDC,
SceneOperationDC,
SceneOperationsDC,
TargetTypeDC,
WebClientDC,
)
from ada.comms.wsock import Message

from ada.comms.fb_model_gen import WebClientDC, FileObjectDC, MeshInfoDC, CameraParamsDC, SceneOperationDC, ProcedureStoreDC, ProcedureDC, ParameterDC, ErrorDC, MessageDC,CommandTypeDC, TargetTypeDC, SceneOperationsDC, FilePurposeDC, FileTypeDC

def deserialize_webclient(fb_obj) -> WebClientDC | None:
if fb_obj is None:
return None

return WebClientDC(
instance_id=fb_obj.InstanceId(),
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
address=fb_obj.Address().decode('utf-8') if fb_obj.Address() is not None else None,
port=fb_obj.Port()
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
address=fb_obj.Address().decode("utf-8") if fb_obj.Address() is not None else None,
port=fb_obj.Port(),
)


Expand All @@ -19,11 +35,11 @@ def deserialize_fileobject(fb_obj) -> FileObjectDC | None:
return None

return FileObjectDC(
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
file_type=FileTypeDC(fb_obj.FileType()),
purpose=FilePurposeDC(fb_obj.Purpose()),
filepath=fb_obj.Filepath().decode('utf-8') if fb_obj.Filepath() is not None else None,
filedata=bytes(fb_obj.FiledataAsNumpy()) if fb_obj.FiledataLength() > 0 else None
filepath=fb_obj.Filepath().decode("utf-8") if fb_obj.Filepath() is not None else None,
filedata=bytes(fb_obj.FiledataAsNumpy()) if fb_obj.FiledataLength() > 0 else None,
)


Expand All @@ -32,9 +48,9 @@ def deserialize_meshinfo(fb_obj) -> MeshInfoDC | None:
return None

return MeshInfoDC(
object_name=fb_obj.ObjectName().decode('utf-8') if fb_obj.ObjectName() is not None else None,
object_name=fb_obj.ObjectName().decode("utf-8") if fb_obj.ObjectName() is not None else None,
face_index=fb_obj.FaceIndex(),
json_data=fb_obj.JsonData().decode('utf-8') if fb_obj.JsonData() is not None else None
json_data=fb_obj.JsonData().decode("utf-8") if fb_obj.JsonData() is not None else None,
)


Expand All @@ -46,10 +62,10 @@ def deserialize_cameraparams(fb_obj) -> CameraParamsDC | None:
position=[fb_obj.Position(i) for i in range(fb_obj.PositionLength())] if fb_obj.PositionLength() > 0 else None,
look_at=[fb_obj.LookAt(i) for i in range(fb_obj.LookAtLength())] if fb_obj.LookAtLength() > 0 else None,
up=[fb_obj.Up(i) for i in range(fb_obj.UpLength())] if fb_obj.UpLength() > 0 else None,
fov=deserialize_float(fb_obj.Fov()),
near=deserialize_float(fb_obj.Near()),
far=deserialize_float(fb_obj.Far()),
force_camera=fb_obj.ForceCamera()
fov=fb_obj.Fov(),
near=fb_obj.Near(),
far=fb_obj.Far(),
force_camera=fb_obj.ForceCamera(),
)


Expand All @@ -58,8 +74,7 @@ def deserialize_sceneoperation(fb_obj) -> SceneOperationDC | None:
return None

return SceneOperationDC(
operation=SceneOperationsDC(fb_obj.Operation()),
camera_params=deserialize_cameraparams(fb_obj.CameraParams())
operation=SceneOperationsDC(fb_obj.Operation()), camera_params=deserialize_cameraparams(fb_obj.CameraParams())
)


Expand All @@ -68,7 +83,11 @@ def deserialize_procedurestore(fb_obj) -> ProcedureStoreDC | None:
return None

return ProcedureStoreDC(
procedures=[deserialize_procedure(fb_obj.Procedures(i)) for i in range(fb_obj.ProceduresLength())] if fb_obj.ProceduresLength() > 0 else None
procedures=(
[deserialize_procedure(fb_obj.Procedures(i)) for i in range(fb_obj.ProceduresLength())]
if fb_obj.ProceduresLength() > 0
else None
)
)


Expand All @@ -77,14 +96,22 @@ def deserialize_procedure(fb_obj) -> ProcedureDC | None:
return None

return ProcedureDC(
id=fb_obj.Id().decode('utf-8') if fb_obj.Id() is not None else None,
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
description=fb_obj.Description().decode('utf-8') if fb_obj.Description() is not None else None,
script_file_location=fb_obj.ScriptFileLocation().decode('utf-8') if fb_obj.ScriptFileLocation() is not None else None,
parameters=[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())] if fb_obj.ParametersLength() > 0 else None,
input_ifc_filepath=fb_obj.InputIfcFilepath().decode('utf-8') if fb_obj.InputIfcFilepath() is not None else None,
output_ifc_filepath=fb_obj.OutputIfcFilepath().decode('utf-8') if fb_obj.OutputIfcFilepath() is not None else None,
error=fb_obj.Error().decode('utf-8') if fb_obj.Error() is not None else None
id=fb_obj.Id().decode("utf-8") if fb_obj.Id() is not None else None,
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
description=fb_obj.Description().decode("utf-8") if fb_obj.Description() is not None else None,
script_file_location=(
fb_obj.ScriptFileLocation().decode("utf-8") if fb_obj.ScriptFileLocation() is not None else None
),
parameters=(
[deserialize_parameter(fb_obj.Parameters(i)) for i in range(fb_obj.ParametersLength())]
if fb_obj.ParametersLength() > 0
else None
),
input_ifc_filepath=fb_obj.InputIfcFilepath().decode("utf-8") if fb_obj.InputIfcFilepath() is not None else None,
output_ifc_filepath=(
fb_obj.OutputIfcFilepath().decode("utf-8") if fb_obj.OutputIfcFilepath() is not None else None
),
error=fb_obj.Error().decode("utf-8") if fb_obj.Error() is not None else None,
)


Expand All @@ -93,9 +120,9 @@ def deserialize_parameter(fb_obj) -> ParameterDC | None:
return None

return ParameterDC(
name=fb_obj.Name().decode('utf-8') if fb_obj.Name() is not None else None,
type=fb_obj.Type().decode('utf-8') if fb_obj.Type() is not None else None,
value=fb_obj.Value().decode('utf-8') if fb_obj.Value() is not None else None
name=fb_obj.Name().decode("utf-8") if fb_obj.Name() is not None else None,
type=fb_obj.Type().decode("utf-8") if fb_obj.Type() is not None else None,
value=fb_obj.Value().decode("utf-8") if fb_obj.Value() is not None else None,
)


Expand All @@ -104,8 +131,7 @@ def deserialize_error(fb_obj) -> ErrorDC | None:
return None

return ErrorDC(
code=fb_obj.Code(),
message=fb_obj.Message().decode('utf-8') if fb_obj.Message() is not None else None
code=fb_obj.Code(), message=fb_obj.Message().decode("utf-8") if fb_obj.Message() is not None else None
)


Expand All @@ -122,8 +148,12 @@ def deserialize_message(fb_obj) -> MessageDC | None:
client_type=TargetTypeDC(fb_obj.ClientType()),
scene_operation=deserialize_sceneoperation(fb_obj.SceneOperation()),
target_id=fb_obj.TargetId(),
web_clients=[deserialize_webclient(fb_obj.WebClients(i)) for i in range(fb_obj.WebClientsLength())] if fb_obj.WebClientsLength() > 0 else None,
procedure_store=deserialize_procedurestore(fb_obj.ProcedureStore())
web_clients=(
[deserialize_webclient(fb_obj.WebClients(i)) for i in range(fb_obj.WebClientsLength())]
if fb_obj.WebClientsLength() > 0
else None
),
procedure_store=deserialize_procedurestore(fb_obj.ProcedureStore()),
)


Expand Down
22 changes: 18 additions & 4 deletions src/ada/comms/fb_model_gen.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations
from enum import Enum
from dataclasses import dataclass
from typing import Optional, List
import pathlib

import pathlib
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional


class CommandTypeDC(Enum):
Expand All @@ -19,33 +19,39 @@ class CommandTypeDC(Enum):
ERROR = 9
SERVER_REPLY = 10


class TargetTypeDC(Enum):
WEB = 0
LOCAL = 1
SERVER = 2


class SceneOperationsDC(Enum):
ADD = 0
REMOVE = 1
REPLACE = 2


class FilePurposeDC(Enum):
DESIGN = 0
ANALYSIS = 1
FABRICATE = 2


class FileTypeDC(Enum):
IFC = 0
GLB = 1
SQLITE = 2


@dataclass
class WebClientDC:
instance_id: int = None
name: str = ""
address: str = ""
port: int = None


@dataclass
class FileObjectDC:
name: str = ""
Expand All @@ -54,12 +60,14 @@ class FileObjectDC:
filepath: pathlib.Path | str = ""
filedata: bytes = None


@dataclass
class MeshInfoDC:
object_name: str = ""
face_index: int = None
json_data: str = ""


@dataclass
class CameraParamsDC:
position: List[float] = None
Expand All @@ -70,15 +78,18 @@ class CameraParamsDC:
far: float = None
force_camera: bool = None


@dataclass
class SceneOperationDC:
operation: Optional[SceneOperationsDC] = None
camera_params: Optional[CameraParamsDC] = None


@dataclass
class ProcedureStoreDC:
procedures: Optional[List[ProcedureDC]] = None


@dataclass
class ProcedureDC:
id: str = ""
Expand All @@ -90,17 +101,20 @@ class ProcedureDC:
output_ifc_filepath: pathlib.Path | str = ""
error: str = ""


@dataclass
class ParameterDC:
name: str = ""
type: str = ""
value: str = ""


@dataclass
class ErrorDC:
code: int = None
message: str = ""


@dataclass
class MessageDC:
instance_id: int = None
Expand Down
30 changes: 26 additions & 4 deletions src/ada/comms/fb_serializer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import flatbuffers
from typing import Optional

from ada.comms.wsock import WebClient, FileObject, MeshInfo, CameraParams, SceneOperation, ProcedureStore, Procedure, Parameter, Error, Message
import flatbuffers
from ada.comms.fb_model_gen import (
CameraParamsDC,
ErrorDC,
FileObjectDC,
MeshInfoDC,
MessageDC,
ParameterDC,
ProcedureDC,
ProcedureStoreDC,
SceneOperationDC,
WebClientDC,
)
from ada.comms.wsock import (
CameraParams,
Error,
FileObject,
MeshInfo,
Message,
Parameter,
Procedure,
ProcedureStore,
SceneOperation,
WebClient,
)

from ada.comms.fb_model_gen import WebClientDC, FileObjectDC, MeshInfoDC, CameraParamsDC, SceneOperationDC, ProcedureStoreDC, ProcedureDC, ParameterDC, ErrorDC, MessageDC

def serialize_webclient(builder: flatbuffers.Builder, obj: Optional[WebClientDC]) -> Optional[int]:
if obj is None:
Expand Down Expand Up @@ -203,7 +225,7 @@ def serialize_error(builder: flatbuffers.Builder, obj: Optional[ErrorDC]) -> Opt
return Error.End(builder)


def serialize_message(message: MessageDC, builder: flatbuffers.Builder=None) -> bytes:
def serialize_message(message: MessageDC, builder: flatbuffers.Builder = None) -> bytes:
if builder is None:
builder = flatbuffers.Builder(1024)
file_object_obj = None
Expand Down
Loading

0 comments on commit d1d7dfa

Please sign in to comment.