Skip to content

Commit

Permalink
fix: minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RobPasMue committed Oct 31, 2024
1 parent 2819b6c commit 6cbfe65
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/ansys/geometry/core/connection/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from typing import Optional
import warnings

from ansys.geometry.core.errors import protect_grpc

# TODO: Remove this context and filter once the protobuf UserWarning issue is downgraded to INFO
# https://github.com/grpc/grpc/issues/37609
with warnings.catch_warnings():
Expand Down Expand Up @@ -334,7 +336,8 @@ def get_name(self) -> str:
return self._target

@check_input_types
def get_service_logs(
@protect_grpc
def _get_service_logs(
self,
all_logs: bool = False,
dump_to_file: bool = False,
Expand Down Expand Up @@ -377,16 +380,16 @@ def get_service_logs(
null_period=None,
)
logs_generator = self._admin_stub.GetLogs(request)
logs = {}
logs: dict[str, str] = {}

for chunk in logs_generator:
if chunk.relative_path not in logs:
logs[chunk.relative_path] = ""
logs[chunk.relative_path] += chunk.log_chunk.decode()
if chunk.log_name not in logs:
logs[chunk.log_name] = ""
logs[chunk.log_name] += chunk.log_chunk.decode()

# Let's handle the various scenarios...
if not dump_to_file:
return logs if all_logs else logs.values()[0]
return logs if all_logs else next(iter(logs.values()))
else:
if logs_folder is None:
logs_folder = Path.cwd()
Expand All @@ -395,7 +398,7 @@ def get_service_logs(

logs_folder.mkdir(parents=True, exist_ok=True)
for log_name, log_content in logs.items():
with (logs_folder / f"{log_name}.log").open("w") as f:
with (logs_folder / log_name).open("w") as f:
f.write(log_content)

return logs_folder / f"{logs.keys()[0]}.log" if len(logs) == 1 else logs_folder
return (logs_folder / log_name) if len(logs) == 1 else logs_folder
3 changes: 2 additions & 1 deletion src/ansys/geometry/core/modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ def measurement_tools(self) -> MeasurementTools:
"""Access to measurement tools."""
return self._measurement_tools

@min_backend_version(25, 1, 0)
def get_service_logs(
self,
all_logs: bool = False,
Expand Down Expand Up @@ -534,6 +535,6 @@ def get_service_logs(
the current logs are retrieved). The ``dump_to_file`` parameter
must be set to ``True``.
"""
return self.client.get_service_logs(
return self.client._get_service_logs(
all_logs=all_logs, dump_to_file=dump_to_file, logs_folder=logs_folder
)
27 changes: 21 additions & 6 deletions tests/integration/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,35 @@ def test_client_close(client: GrpcClient):

def test_client_get_service_logs(client: GrpcClient):
"""Test the retrieval of the service logs."""
logs = client.get_service_logs()
logs = client._get_service_logs()

# Make sure the logs are not empty
assert logs
assert isinstance(logs, str)
assert logs # is not empty

# Sanitize the logs to avoid any issues with the test
logs = logs.replace("\r", "\n")

# Let's request them again on file dump
logs_folder = Path(__file__).parent / "logs"
logs_file_dump = client.get_service_logs(dump_to_file=True, logs_folder=logs_folder)
logs_folder = str(Path(__file__).parent / "logs")
logs_file_dump = client._get_service_logs(dump_to_file=True, logs_folder=logs_folder)

assert logs_file_dump.exists()
assert logs_file_dump.read_text() == logs

# Do not provide a folder
logs_file_dump = client._get_service_logs(dump_to_file=True)

assert logs_file_dump == logs_folder
assert logs_file_dump.exists()
assert logs_file_dump.read_text() == logs
logs_file_dump.unlink() # Delete the file

# Let's request all logs now
logs_all = client._get_service_logs(all_logs=True)
assert isinstance(logs_all, dict)
assert logs_all # is not empty

# Let's do the same directly from a Modeler object
modeler = Modeler(channel=client.channel)
logs_modeler = modeler.get_service_logs()
assert logs == logs_modeler
assert logs == logs_modeler.replace("\r", "\n")

0 comments on commit 6cbfe65

Please sign in to comment.