Skip to content

Commit b0c891f

Browse files
authored
Make model more permissive (#132)
1 parent 7fe5b3a commit b0c891f

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

ansys/dpf/core/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ansys.dpf.core import server as serverlib
1616
from ansys.dpf.core import misc
1717
from ansys.dpf.core.common import _common_progress_bar
18+
from ansys.dpf.core.cache import class_handling_cache
1819

1920
LOG = logging.getLogger(__name__)
2021
LOG.setLevel("DEBUG")
@@ -262,6 +263,7 @@ def _description(dpf_entity_message, server=None):
262263
return BaseService(server, load_operators=False)._description(dpf_entity_message)
263264

264265

266+
@class_handling_cache
265267
class BaseService:
266268
"""The Base Service class allows to make generic requests to dpf's server.
267269
For example, information about the server can be requested,
@@ -401,6 +403,9 @@ def server_info(self):
401403
dictionary with "server_ip", "server_port", "server_process_id"
402404
"server_version" keys
403405
"""
406+
return self._get_server_info()
407+
408+
def _get_server_info(self):
404409
request = base_pb2.ServerInfoRequest()
405410
try:
406411
response = self._stub.GetServerInfo(request)
@@ -736,3 +741,7 @@ def __file_chunk_yielder(self, file_path, to_server_file_path, use_tmp_dir=False
736741
bar.finish()
737742
except:
738743
pass
744+
745+
_to_cache = {
746+
_get_server_info: None
747+
}

ansys/dpf/core/errors.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,25 @@ def wrapper(*args, **kwargs):
116116
return out
117117

118118
return wrapper
119+
120+
121+
def protect_source_op_not_found(func):
122+
"""Capture gRPC server exceptions when a source operator is not found
123+
and return a more succinct error message.
124+
"""
125+
126+
@wraps(func)
127+
def wrapper(*args, **kwargs):
128+
"""Capture gRPC exceptions."""
129+
# Capture gRPC exceptions
130+
try:
131+
out = func(*args, **kwargs)
132+
except DPFServerException as error:
133+
details = str(error)
134+
if "source operator not found" in details:
135+
return None
136+
raise DPFServerException(details)
137+
138+
return out
139+
140+
return wrapper

ansys/dpf/core/model.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ansys.dpf.core.results import Results, CommonResults
1616
from ansys.dpf.core.server import LOG
1717
from ansys.dpf.core import misc
18+
from ansys.dpf.core.errors import protect_source_op_not_found
1819
from grpc._channel import _InactiveRpcError
1920

2021

@@ -267,6 +268,7 @@ def _cache_streams_provider(self):
267268
self._stream_provider.inputs.connect(self._data_sources)
268269

269270
@property
271+
@protect_source_op_not_found
270272
def time_freq_support(self):
271273
"""Time frequency support.
272274
@@ -384,6 +386,7 @@ def _load_result_info(self):
384386
return result_info
385387

386388
@property
389+
@protect_source_op_not_found
387390
def meshed_region(self):
388391
"""Meshed region instance.
389392
@@ -425,6 +428,7 @@ def mesh_provider(self):
425428
return mesh_provider
426429

427430
@property
431+
@protect_source_op_not_found
428432
def result_info(self):
429433
"""Result Info instance.
430434

ansys/dpf/core/server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ def __init__(
339339
self._input_ip = ip
340340
self._input_port = port
341341
self._own_process = launch_server
342+
self._base_service_instance = None
342343

343344
@property
344345
def _base_service(self):
345-
if not hasattr(self, "__base_service"):
346+
if not self._base_service_instance:
346347
from ansys.dpf.core.core import BaseService
347348

348-
self.__base_service = BaseService(self, timeout=1)
349-
return self.__base_service
349+
self._base_service_instance = BaseService(self, timeout=1)
350+
return self._base_service_instance
350351

351352
@property
352353
def info(self):

tests/test_cache.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ def test_physics_type_cache(simple_bar):
5353
res_info.unit_system
5454
assert len(res_info._cache.cached) == 1
5555
res_info.physics_type
56-
assert len(res_info._cache.cached) == 1
56+
assert len(res_info._cache.cached) == 1
57+
58+
59+
def test_server_info_cache():
60+
if not dpf.SERVER:
61+
dpf.start_local_server()
62+
dpf.SERVER.info
63+
identifier = dpf.cache.MethodIdentifier("_get_server_info", (), {})
64+
assert identifier in dpf.SERVER._base_service._cache.cached

0 commit comments

Comments
 (0)