Skip to content

Commit 6f81216

Browse files
committed
refacto types in utils_functions.py
1 parent 7305599 commit 6f81216

File tree

5 files changed

+55
-70
lines changed

5 files changed

+55
-70
lines changed

src/opengeodeweb_viewer/rpc/mesh/mesh_protocols.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
from opengeodeweb_microservice.database.data import Data
99

1010
# Local application imports
11-
from opengeodeweb_viewer.utils_functions import get_schemas_dict, validate_schema
11+
from opengeodeweb_viewer.utils_functions import (
12+
get_schemas_dict,
13+
validate_schema,
14+
RpcParams,
15+
RpcParamsWithColor,
16+
)
1217
from opengeodeweb_viewer.object.object_methods import VtkObjectView
1318

1419

@@ -22,7 +27,7 @@ def __init__(self) -> None:
2227
super().__init__()
2328

2429
@exportRpc(mesh_prefix + mesh_schemas_dict["register"]["rpc"])
25-
def registerMesh(self, params: dict[str, str | int | float | bool]) -> None:
30+
def registerMesh(self, params: RpcParams) -> None:
2631
validate_schema(params, self.mesh_schemas_dict["register"], self.mesh_prefix)
2732
data_id = str(params["id"])
2833
try:
@@ -62,30 +67,27 @@ def registerMesh(self, params: dict[str, str | int | float | bool]) -> None:
6267
raise
6368

6469
@exportRpc(mesh_prefix + mesh_schemas_dict["deregister"]["rpc"])
65-
def deregisterMesh(self, params: dict[str, str | int | float | bool]) -> None:
70+
def deregisterMesh(self, params: RpcParams) -> None:
6671
validate_schema(params, self.mesh_schemas_dict["deregister"], self.mesh_prefix)
6772
data_id = str(params["id"])
6873
self.deregisterObject(data_id)
6974

7075
@exportRpc(mesh_prefix + mesh_schemas_dict["visibility"]["rpc"])
71-
def SetMeshVisibility(self, params: dict[str, str | int | float | bool]) -> None:
76+
def SetMeshVisibility(self, params: RpcParams) -> None:
7277
validate_schema(params, self.mesh_schemas_dict["visibility"], self.mesh_prefix)
7378
data_id, visibility = str(params["id"]), bool(params["visibility"])
7479
self.SetVisibility(data_id, visibility)
7580

7681
@exportRpc(mesh_prefix + mesh_schemas_dict["opacity"]["rpc"])
77-
def setMeshOpacity(self, params: dict[str, str | int | float | bool]) -> None:
82+
def setMeshOpacity(self, params: RpcParams) -> None:
7883
validate_schema(params, self.mesh_schemas_dict["opacity"], self.mesh_prefix)
7984
data_id, opacity = str(params["id"]), float(params["opacity"])
8085
self.SetOpacity(data_id, opacity)
8186

8287
@exportRpc(mesh_prefix + mesh_schemas_dict["color"]["rpc"])
83-
def setMeshColor(
84-
self,
85-
params: dict[str, str | int | float | bool | dict[str, str | int | float]],
86-
) -> None:
88+
def setMeshColor(self, params: RpcParamsWithColor) -> None:
8789
validate_schema(params, self.mesh_schemas_dict["color"], self.mesh_prefix)
88-
color_dict = cast(dict[str, str | int | float], params["color"])
90+
color_dict = cast(dict[str, int], params["color"])
8991
data_id, red, green, blue = (
9092
str(params["id"]),
9193
int(color_dict["r"]),
@@ -95,7 +97,7 @@ def setMeshColor(
9597
self.SetColor(data_id, red, green, blue)
9698

9799
@exportRpc(mesh_prefix + mesh_schemas_dict["apply_textures"]["rpc"])
98-
def meshApplyTextures(self, params: dict[str, str | list[dict[str, str]]]) -> None:
100+
def meshApplyTextures(self, params: RpcParams) -> None:
99101
validate_schema(
100102
params, self.mesh_schemas_dict["apply_textures"], self.mesh_prefix
101103
)

src/opengeodeweb_viewer/rpc/mesh/points/mesh_points_protocols.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Standard library imports
22
import os
3-
from typing import Union
43

54
# Third party imports
65
from wslink import register as exportRpc
76

87
# Local application imports
9-
from opengeodeweb_viewer.utils_functions import get_schemas_dict, validate_schema
8+
from opengeodeweb_viewer.utils_functions import (
9+
get_schemas_dict,
10+
validate_schema,
11+
RpcParams,
12+
RpcParamsWithColor,
13+
)
1014
from opengeodeweb_viewer.rpc.mesh.mesh_protocols import VtkMeshView
1115

1216

@@ -20,15 +24,15 @@ def __init__(self) -> None:
2024
super().__init__()
2125

2226
@exportRpc(mesh_points_prefix + mesh_points_schemas_dict["visibility"]["rpc"])
23-
def setMeshPointsVisibility(self, params: dict[str, Union[str, bool]]) -> None:
27+
def setMeshPointsVisibility(self, params: RpcParams) -> None:
2428
validate_schema(
2529
params, self.mesh_points_schemas_dict["visibility"], self.mesh_points_prefix
2630
)
2731
id, visibility = params["id"], params["visibility"]
2832
self.SetPointsVisibility(id, visibility)
2933

3034
@exportRpc(mesh_points_prefix + mesh_points_schemas_dict["color"]["rpc"])
31-
def setMeshPointsColor(self, params: dict[str, Union[str, dict[str, int]]]) -> None:
35+
def setMeshPointsColor(self, params: RpcParamsWithColor) -> None:
3236
validate_schema(
3337
params, self.mesh_points_schemas_dict["color"], self.mesh_points_prefix
3438
)
@@ -41,15 +45,15 @@ def setMeshPointsColor(self, params: dict[str, Union[str, dict[str, int]]]) -> N
4145
self.SetPointsColor(id, red, green, blue)
4246

4347
@exportRpc(mesh_points_prefix + mesh_points_schemas_dict["size"]["rpc"])
44-
def setMeshPointsSize(self, params: dict[str, Union[str, int]]) -> None:
48+
def setMeshPointsSize(self, params: RpcParams) -> None:
4549
validate_schema(
4650
params, self.mesh_points_schemas_dict["size"], self.mesh_points_prefix
4751
)
4852
id, size = params["id"], params["size"]
4953
self.SetPointsSize(id, size)
5054

5155
@exportRpc(mesh_points_prefix + mesh_points_schemas_dict["vertex_attribute"]["rpc"])
52-
def setMeshPointsVertexAttribute(self, params: dict[str, str]) -> None:
56+
def setMeshPointsVertexAttribute(self, params: RpcParams) -> None:
5357
validate_schema(
5458
params,
5559
self.mesh_points_schemas_dict["vertex_attribute"],

src/opengeodeweb_viewer/rpc/viewer/viewer_protocols.py

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Standard library imports
22
import math
33
import os
4-
from typing import Union
54

65
# Third party imports
76
import vtk
@@ -12,7 +11,13 @@
1211
from wslink import register as exportRpc
1312

1413
# Local application imports
15-
from opengeodeweb_viewer.utils_functions import get_schemas_dict, validate_schema
14+
from opengeodeweb_viewer.utils_functions import (
15+
get_schemas_dict,
16+
validate_schema,
17+
RpcParams,
18+
RpcParamsWithColor,
19+
RpcParamsWithList,
20+
)
1621
from opengeodeweb_viewer.vtk_protocol import VtkView
1722

1823

@@ -26,9 +31,7 @@ def __init__(self) -> None:
2631
super().__init__()
2732

2833
@exportRpc(viewer_prefix + viewer_schemas_dict["reset_visualization"]["rpc"])
29-
def resetVisualization(
30-
self, params: dict[str, Union[str, int, float, bool]]
31-
) -> None:
34+
def resetVisualization(self, params: RpcParams) -> None:
3235
validate_schema(
3336
params, self.viewer_schemas_dict["reset_visualization"], self.viewer_prefix
3437
)
@@ -83,12 +86,7 @@ def resetVisualization(
8386
self.render()
8487

8588
@exportRpc(viewer_prefix + viewer_schemas_dict["set_background_color"]["rpc"])
86-
def setBackgroundColor(
87-
self,
88-
params: dict[
89-
str, Union[str, int, float, bool, dict[str, Union[str, int, float]]]
90-
],
91-
) -> None:
89+
def setBackgroundColor(self, params: RpcParamsWithColor) -> None:
9290
validate_schema(
9391
params, self.viewer_schemas_dict["set_background_color"], self.viewer_prefix
9492
)
@@ -106,7 +104,7 @@ def setBackgroundColor(
106104
self.render()
107105

108106
@exportRpc(viewer_prefix + viewer_schemas_dict["reset_camera"]["rpc"])
109-
def resetCamera(self, params: dict[str, Union[str, int, float, bool]]) -> None:
107+
def resetCamera(self, params: RpcParams) -> None:
110108
validate_schema(
111109
params, self.viewer_schemas_dict["reset_camera"], self.viewer_prefix
112110
)
@@ -116,9 +114,7 @@ def resetCamera(self, params: dict[str, Union[str, int, float, bool]]) -> None:
116114
self.render()
117115

118116
@exportRpc(viewer_prefix + viewer_schemas_dict["take_screenshot"]["rpc"])
119-
def takeScreenshot(
120-
self, params: dict[str, Union[str, int, float, bool]]
121-
) -> dict[str, Union[str, bytes]]:
117+
def takeScreenshot(self, params: RpcParams) -> dict[str, str | bytes]:
122118
validate_schema(
123119
params, self.viewer_schemas_dict["take_screenshot"], self.viewer_prefix
124120
)
@@ -167,7 +163,7 @@ def takeScreenshot(
167163
return {"blob": self.addAttachment(file_content)}
168164

169165
@exportRpc(viewer_prefix + viewer_schemas_dict["update_data"]["rpc"])
170-
def updateData(self, params: dict[str, Union[str, int, float, bool]]) -> None:
166+
def updateData(self, params: RpcParams) -> None:
171167
validate_schema(
172168
params, self.viewer_schemas_dict["update_data"], self.viewer_prefix
173169
)
@@ -190,9 +186,7 @@ def updateData(self, params: dict[str, Union[str, int, float, bool]]) -> None:
190186
self.render()
191187

192188
@exportRpc(viewer_prefix + viewer_schemas_dict["get_point_position"]["rpc"])
193-
def getPointPosition(
194-
self, params: dict[str, Union[str, int, float, bool]]
195-
) -> dict[str, float]:
189+
def getPointPosition(self, params: RpcParams) -> dict[str, float]:
196190
validate_schema(
197191
params, self.viewer_schemas_dict["get_point_position"], self.viewer_prefix
198192
)
@@ -220,9 +214,7 @@ def computeEpsilon(self, renderer: vtk.vtkRenderer, z: float) -> float:
220214
return math.sqrt(epsilon) * 0.0125
221215

222216
@exportRpc(viewer_prefix + viewer_schemas_dict["picked_ids"]["rpc"])
223-
def pickedIds(
224-
self, params: dict[str, Union[str, int, float, bool, list[str]]]
225-
) -> dict[str, list[str]]:
217+
def pickedIds(self, params: RpcParamsWithList) -> dict[str, list[str]]:
226218
validate_schema(
227219
params, self.viewer_schemas_dict["picked_ids"], self.viewer_prefix
228220
)
@@ -247,7 +239,7 @@ def pickedIds(
247239
return {"array_ids": array_ids}
248240

249241
@exportRpc(viewer_prefix + viewer_schemas_dict["grid_scale"]["rpc"])
250-
def toggleGridScale(self, params: dict[str, Union[str, int, float, bool]]) -> None:
242+
def toggleGridScale(self, params: RpcParams) -> None:
251243
validate_schema(
252244
params, self.viewer_schemas_dict["grid_scale"], self.viewer_prefix
253245
)
@@ -258,28 +250,15 @@ def toggleGridScale(self, params: dict[str, Union[str, int, float, bool]]) -> No
258250
self.render()
259251

260252
@exportRpc(viewer_prefix + viewer_schemas_dict["axes"]["rpc"])
261-
def toggleAxes(self, params: dict[str, Union[str, int, float, bool]]) -> None:
253+
def toggleAxes(self, params: RpcParams) -> None:
262254
validate_schema(params, self.viewer_schemas_dict["axes"], self.viewer_prefix)
263255
id, visibility = "axes", params["visibility"]
264256
actor = self.get_object(id)["actor"]
265257
actor.SetVisibility(visibility)
266258
self.render()
267259

268260
@exportRpc(viewer_prefix + viewer_schemas_dict["update_camera"]["rpc"])
269-
def updateCamera(
270-
self,
271-
params: dict[
272-
str,
273-
Union[
274-
str,
275-
int,
276-
float,
277-
bool,
278-
dict[str, Union[str, int, float, list[float]]],
279-
list[float],
280-
],
281-
],
282-
) -> None:
261+
def updateCamera(self, params: RpcParams) -> None:
283262
validate_schema(
284263
params, self.viewer_schemas_dict["update_camera"], self.viewer_prefix
285264
)
@@ -301,14 +280,14 @@ def updateCamera(
301280
self.render()
302281

303282
@exportRpc(viewer_prefix + viewer_schemas_dict["render_now"]["rpc"])
304-
def renderNow(self, params: dict[str, Union[str, int, float, bool]]) -> None:
283+
def renderNow(self, params: RpcParams) -> None:
305284
validate_schema(
306285
params, self.viewer_schemas_dict["render_now"], self.viewer_prefix
307286
)
308287
self.render()
309288

310289
@exportRpc(viewer_prefix + viewer_schemas_dict["set_z_scaling"]["rpc"])
311-
def setZScaling(self, params: dict[str, Union[str, int, float, bool]]) -> None:
290+
def setZScaling(self, params: RpcParams) -> None:
312291
validate_schema(
313292
params, self.viewer_schemas_dict["set_z_scaling"], self.viewer_prefix
314293
)

src/opengeodeweb_viewer/utils_functions.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
import fastjsonschema
77
from fastjsonschema import JsonSchemaException
88

9-
# Local application imports
9+
type JsonPrimitive = str | int | float | bool
10+
type JsonValue = JsonPrimitive | dict[str, JsonValue] | list[JsonValue]
11+
type RpcParams = dict[str, JsonValue]
12+
13+
type ColorDict = dict[str, int]
14+
type RpcParamsWithColor = dict[str, JsonPrimitive | ColorDict]
15+
type RpcParamsWithList = dict[str, JsonPrimitive | list[str]]
16+
type RpcTestParams = list[
17+
dict[str, str | int | float | bool | dict[str, int] | list[str]] | int
18+
] | None
1019

1120

1221
def get_schemas_dict(path):
@@ -21,7 +30,7 @@ def get_schemas_dict(path):
2130
return schemas_dict
2231

2332

24-
def validate_schema(params, schema, prefix=""):
33+
def validate_schema(params: RpcParams, schema: dict, prefix: str = "") -> None:
2534
print(f"{prefix}{schema['rpc']}", f"{params=}", flush=True)
2635
try:
2736
validate = fastjsonschema.compile(schema)

src/tests/conftest.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import vtk
77
import os
88
import shutil
9-
import time
10-
import os
11-
from pathlib import Path
129
import xml.etree.ElementTree as ET
1310
from typing import Callable, Generator
1411
from opengeodeweb_viewer import config
1512
from opengeodeweb_microservice.database.connection import get_session, init_database
1613
from opengeodeweb_microservice.database.data import Data
14+
from opengeodeweb_viewer.utils_functions import RpcTestParams
1715

1816

1917
class ServerMonitor:
@@ -31,14 +29,7 @@ def __init__(self, log: str) -> None:
3129
self._init_ws()
3230
self._drain_initial_messages()
3331

34-
def call(
35-
self,
36-
rpc: str,
37-
params: (
38-
list[dict[str, str | int | float | bool | dict[str, int] | list[str]] | int]
39-
| None
40-
) = None,
41-
) -> None:
32+
def call(self, rpc: str, params: RpcTestParams = None) -> None:
4233
if params is None:
4334
params = [{}]
4435
self.ws.send(

0 commit comments

Comments
 (0)