Skip to content

Commit 6e61f4f

Browse files
feat: cleaning up usage of unsupported stub (#2267)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent f241a7b commit 6e61f4f

File tree

6 files changed

+252
-46
lines changed

6 files changed

+252
-46
lines changed

doc/changelog.d/2267.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cleaning up usage of unsupported stub

src/ansys/geometry/core/_grpc/_services/_service.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from .base.patterns import GRPCPatternsService
4545
from .base.prepare_tools import GRPCPrepareToolsService
4646
from .base.repair_tools import GRPCRepairToolsService
47+
from .base.unsupported import GRPCUnsupportedService
4748

4849

4950
class _GRPCServices:
@@ -105,6 +106,7 @@ def __init__(self, channel: grpc.Channel, version: GeometryApiProtos | str | Non
105106
self._patterns = None
106107
self._prepare_tools = None
107108
self._repair_tools = None
109+
self._unsupported = None
108110

109111
@property
110112
def admin(self) -> GRPCAdminService:
@@ -648,4 +650,30 @@ def repair_tools(self) -> GRPCRepairToolsService:
648650
else: # pragma: no cover
649651
# This should never happen as the version is set in the constructor
650652
raise ValueError(f"Unsupported version: {self.version}")
653+
651654
return self._repair_tools
655+
656+
@property
657+
def unsupported(self) -> GRPCUnsupportedService:
658+
"""
659+
Get the unsupported service for the specified version.
660+
661+
Returns
662+
-------
663+
GRPCUnsupportedService
664+
The unsupported service for the specified version.
665+
"""
666+
if not self._unsupported:
667+
from .v0.unsupported import GRPCUnsupportedServiceV0
668+
from .v1.unsupported import GRPCUnsupportedServiceV1
669+
670+
if self.version == GeometryApiProtos.V0:
671+
self._unsupported = GRPCUnsupportedServiceV0(self.channel)
672+
elif self.version == GeometryApiProtos.V1: # pragma: no cover
673+
# V1 is not implemented yet
674+
self._unsupported = GRPCUnsupportedServiceV1(self.channel)
675+
else: # pragma: no cover
676+
# This should never happen as the version is set in the constructor
677+
raise ValueError(f"Unsupported version: {self.version}")
678+
679+
return self._unsupported
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module containing the unsupported service implementation (abstraction layer)."""
23+
24+
from abc import ABC, abstractmethod
25+
26+
import grpc
27+
28+
29+
class GRPCUnsupportedService(ABC): # pragma: no cover
30+
"""Unsupported service for gRPC communication with the Geometry server.
31+
32+
Parameters
33+
----------
34+
channel : grpc.Channel
35+
The gRPC channel to the server.
36+
"""
37+
38+
def __init__(self, channel: grpc.Channel):
39+
"""Initialize the GRPCUnsupportedService class."""
40+
pass
41+
42+
@abstractmethod
43+
def get_import_id_map(self, **kwargs) -> dict:
44+
"""Get the import ID map."""
45+
pass
46+
47+
@abstractmethod
48+
def set_export_ids(self, **kwargs) -> dict:
49+
"""Set the export IDs for the entities."""
50+
pass
51+
52+
@abstractmethod
53+
def set_single_export_id(self, **kwargs) -> dict:
54+
"""Set a single export ID for an entity."""
55+
pass
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module containing the unsupported service implementation for v0."""
23+
24+
import grpc
25+
26+
from ansys.geometry.core.errors import protect_grpc
27+
28+
from ..base.unsupported import GRPCUnsupportedService
29+
30+
31+
class GRPCUnsupportedServiceV0(GRPCUnsupportedService): # pragma: no cover
32+
"""Unsupported service for gRPC communication with the Geometry server.
33+
34+
Parameters
35+
----------
36+
channel : grpc.Channel
37+
The gRPC channel to the server.
38+
"""
39+
40+
def __init__(self, channel: grpc.Channel): # noqa: D102
41+
from ansys.api.geometry.v0.unsupported_pb2_grpc import UnsupportedStub
42+
43+
self.stub = UnsupportedStub(channel)
44+
45+
@protect_grpc
46+
def get_import_id_map(self, **kwargs) -> dict: # noqa: D102
47+
from ansys.api.geometry.v0.unsupported_pb2 import ImportIdRequest
48+
49+
# Create the request - assumes all inputs are valid and of the proper type
50+
request = ImportIdRequest(type=kwargs["id_type"].value)
51+
52+
# Call the gRPC service
53+
response = self.stub.GetImportIdMap(request)
54+
55+
# Return the response - formatted as a dictionary
56+
return {
57+
"id_map": {k: v.id for k, v in response.id_map.items()}, # dict[str, str]
58+
"id_type": kwargs["id_type"],
59+
}
60+
61+
@protect_grpc
62+
def set_export_ids(self, **kwargs) -> dict: # noqa: D102
63+
from ansys.api.geometry.v0.unsupported_pb2 import ExportIdRequest, SetExportIdsRequest
64+
65+
from .conversions import build_grpc_id
66+
67+
# Create the request - assumes all inputs are valid and of the proper type
68+
request = SetExportIdsRequest(
69+
export_data=[
70+
ExportIdRequest(
71+
moniker=build_grpc_id(data.moniker),
72+
id=data.value,
73+
type=data.id_type.value,
74+
)
75+
for data in kwargs["export_data"]
76+
]
77+
)
78+
79+
# Call the gRPC service
80+
_ = self.stub.SetExportIds(request)
81+
82+
# Return the response - formatted as a dictionary
83+
return {}
84+
85+
@protect_grpc
86+
def set_single_export_id(self, **kwargs) -> dict: # noqa: D102
87+
from ansys.api.geometry.v0.unsupported_pb2 import ExportIdRequest
88+
89+
from .conversions import build_grpc_id
90+
91+
# Create the request - assumes all inputs are valid and of the proper type
92+
request = ExportIdRequest(
93+
moniker=build_grpc_id(kwargs["export_data"].moniker),
94+
id=kwargs["export_data"].value,
95+
type=kwargs["export_data"].id_type.value,
96+
)
97+
98+
# Call the gRPC service
99+
_ = self.stub.SetExportId(request)
100+
101+
# Return the response - formatted as a dictionary
102+
return {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module containing the unsupported service implementation for v1."""
23+
24+
import grpc
25+
26+
from ansys.geometry.core.errors import protect_grpc
27+
28+
from ..base.unsupported import GRPCUnsupportedService
29+
30+
31+
class GRPCUnsupportedServiceV1(GRPCUnsupportedService): # pragma: no cover
32+
"""Unsupported service for gRPC communication with the Geometry server.
33+
34+
Parameters
35+
----------
36+
channel : grpc.Channel
37+
The gRPC channel to the server.
38+
"""
39+
40+
def __init__(self, channel: grpc.Channel): # noqa: D102
41+
from ansys.api.geometry.v1.unsupported_pb2_grpc import UnsupportedStub
42+
43+
self.stub = UnsupportedStub(channel)
44+
45+
@protect_grpc
46+
def get_import_id_map(self, **kwargs) -> dict: # noqa: D102
47+
raise NotImplementedError
48+
49+
@protect_grpc
50+
def set_export_ids(self, **kwargs) -> dict: # noqa: D102
51+
raise NotImplementedError
52+
53+
@protect_grpc
54+
def set_single_export_id(self, **kwargs) -> dict: # noqa: D102
55+
raise NotImplementedError

src/ansys/geometry/core/tools/unsupported.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,8 @@
2525
from enum import Enum, unique
2626
from typing import TYPE_CHECKING
2727

28-
from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier
29-
from ansys.api.geometry.v0.unsupported_pb2 import (
30-
ExportIdRequest,
31-
ImportIdRequest,
32-
SetExportIdsRequest,
33-
)
34-
from ansys.api.geometry.v0.unsupported_pb2_grpc import UnsupportedStub
35-
3628
from ansys.geometry.core.connection import GrpcClient
37-
from ansys.geometry.core.errors import GeometryRuntimeError, protect_grpc
29+
from ansys.geometry.core.errors import GeometryRuntimeError
3830
from ansys.geometry.core.misc.auxiliary import get_all_bodies_from_design
3931
from ansys.geometry.core.misc.checks import (
4032
min_backend_version,
@@ -97,12 +89,10 @@ def __init__(self, grpc_client: GrpcClient, modeler: "Modeler", _internal_use: b
9789
"Use 'modeler.unsupported' to access unsupported commands."
9890
)
9991
self._grpc_client = grpc_client
100-
self._unsupported_stub = UnsupportedStub(self._grpc_client.channel)
10192
self.__id_map = {}
10293
self.__modeler = modeler
10394
self.__current_design = modeler.get_active_design()
10495

105-
@protect_grpc
10696
@min_backend_version(25, 2, 0)
10797
def __fill_imported_id_map(self, id_type: PersistentIdType) -> None:
10898
"""Populate the persistent id map for caching.
@@ -118,24 +108,15 @@ def __fill_imported_id_map(self, id_type: PersistentIdType) -> None:
118108
--------
119109
This method is only available starting on Ansys release 25R2.
120110
"""
121-
request = ImportIdRequest(type=id_type.value)
122-
self.__id_map[id_type] = self._unsupported_stub.GetImportIdMap(request).id_map
123-
124-
def __clear_cache(self) -> None:
125-
"""Clear the cache of persistent id's.
126-
127-
Notes
128-
-----
129-
This should be called on design change.
130-
"""
131-
self.__id_map = {}
111+
result = self._grpc_client.services.unsupported.get_import_id_map(id_type=id_type)
112+
self.__id_map[id_type] = result.get("id_map", {})
132113

133-
def __is_occurrence(self, master: EntityIdentifier, occ: str) -> bool:
114+
def __is_occurrence(self, master: str, occ: str) -> bool:
134115
"""Determine if the master is the master of the occurrence.
135116
136117
Parameters
137118
----------
138-
master : EntityIdentifier
119+
master : str
139120
Master moniker.
140121
occ : str
141122
Occurrence moniker.
@@ -147,11 +128,9 @@ def __is_occurrence(self, master: EntityIdentifier, occ: str) -> bool:
147128
148129
"""
149130
master_id = occ.split("/")[-1]
150-
return master.id == master_id
131+
return master == master_id
151132

152-
def __get_moniker_from_import_id(
153-
self, id_type: PersistentIdType, import_id: str
154-
) -> EntityIdentifier | None:
133+
def __get_moniker_from_import_id(self, id_type: PersistentIdType, import_id: str) -> str | None:
155134
"""Look up the moniker from the id map.
156135
157136
Parameters
@@ -163,7 +142,7 @@ def __get_moniker_from_import_id(
163142
164143
Returns
165144
-------
166-
EntityIdentifier
145+
str | None
167146
Moniker associated with the id or None
168147
169148
Notes
@@ -178,7 +157,6 @@ def __get_moniker_from_import_id(
178157
moniker = self.__id_map[id_type].get(import_id, None)
179158
return moniker
180159

181-
@protect_grpc
182160
@min_backend_version(25, 2, 0)
183161
def set_export_id(self, moniker: str, id_type: PersistentIdType, value: str) -> None:
184162
"""Set the persistent id for the moniker.
@@ -196,13 +174,11 @@ def set_export_id(self, moniker: str, id_type: PersistentIdType, value: str) ->
196174
--------
197175
This method is only available starting on Ansys release 25R2.
198176
"""
199-
request = ExportIdRequest(
200-
moniker=EntityIdentifier(id=moniker), id=value, type=id_type.value
177+
self._grpc_client.services.unsupported.set_single_export_id(
178+
export_data=ExportIdData(moniker=moniker, id_type=id_type, value=value),
201179
)
202-
self._unsupported_stub.SetExportId(request)
203180
self.__id_map = {}
204181

205-
@protect_grpc
206182
@min_backend_version(26, 1, 0)
207183
def set_multiple_export_ids(
208184
self,
@@ -219,19 +195,8 @@ def set_multiple_export_ids(
219195
--------
220196
This method is only available starting on Ansys release 26R1.
221197
"""
222-
request = SetExportIdsRequest(
223-
export_data=[
224-
ExportIdRequest(
225-
moniker=EntityIdentifier(id=data.moniker),
226-
id=data.value,
227-
type=data.id_type.value,
228-
)
229-
for data in export_data
230-
]
231-
)
232-
233198
# Call the gRPC service
234-
self._unsupported_stub.SetExportIds(request)
199+
_ = self._grpc_client.services.unsupported.set_export_ids(export_data=export_data)
235200
self.__id_map = {}
236201

237202
def get_body_occurrences_from_import_id(

0 commit comments

Comments
 (0)