Skip to content

Commit

Permalink
chore: code maintenance (#947)
Browse files Browse the repository at this point in the history
Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
dipinknair and pyansys-ci-bot authored Oct 16, 2024
1 parent 2cf726e commit baaa546
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/947.maintenance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
code maintenance
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies = [
"protobuf>=3.12.2,<6",
"psutil==6.0.0",
"tqdm>=4.45.0",
"requests>=2,<3",
]

[project.urls]
Expand Down
8 changes: 4 additions & 4 deletions src/ansys/mechanical/core/embedding/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ def __init__(self, **kwargs):
time.sleep(0.05)
continue
else:
assert (
not BackgroundApp.__stopped
), "Cannot initialize a BackgroundApp once it has been stopped!"
if BackgroundApp.__stopped:
raise RuntimeError("Cannot initialize a BackgroundApp once it has been stopped!")

def new():
BackgroundApp.__app.new()
Expand All @@ -80,7 +79,8 @@ def app(self) -> mech.App:

def post(self, callable: typing.Callable):
"""Post callable method to the background app thread."""
assert not BackgroundApp.__stopped, "Cannot use background app after stopping it."
if BackgroundApp.__stopped:
raise RuntimeError("Cannot use BackgroundApp after stopping it.")
return BackgroundApp.__poster.post(callable)

def stop(self) -> None:
Expand Down
13 changes: 9 additions & 4 deletions src/ansys/mechanical/core/examples/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import shutil
from typing import Optional
from urllib.parse import urljoin
import urllib.request

import requests

import ansys.mechanical.core as pymechanical

Expand All @@ -53,9 +54,13 @@ def _get_filepath_on_default_server(filename: str, *directory: str):
return joiner(server, filename)


def _retrieve_url(url, dest):
saved_file, _ = urllib.request.urlretrieve(url, filename=dest)
return saved_file
def _retrieve_url(url: str, dest: str) -> str:
with requests.get(url, stream=True, timeout=10) as r:
r.raise_for_status()
with open(dest, "wb") as f:
for chunk in r.iter_content(chunk_size=4096):
f.write(chunk)
return dest


def _retrieve_data(url: str, filename: str, dest: str = None, force: bool = False):
Expand Down
4 changes: 2 additions & 2 deletions src/ansys/mechanical/core/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ def run(name_local=""):
LOG.error(f"Stopped instance because running failed.")
try:
obj.exit()
except:
pass
except Exception as e:
LOG.error(f"Unexpected error while exiting: {e}")

obj.locked = False
if pbar:
Expand Down
2 changes: 1 addition & 1 deletion tests/embedding/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_background_app_use_stopped(rootdir, run_subprocess, pytestconfig):
stderr = _run_background_app_test(
run_subprocess, rootdir, pytestconfig, "test_background_app_use_stopped", False
)
assert "Cannot use background app after stopping it" in stderr
assert "Cannot use BackgroundApp after stopping it" in stderr


@pytest.mark.embedding_scripts
Expand Down

0 comments on commit baaa546

Please sign in to comment.