Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor function handler #4463

Merged
merged 9 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
- name: 'Unit testing'
uses: nick-fields/retry@v3
with:
max_attempts: 3
max_attempts: 1
retry_on: error
timeout_minutes: 40
command: |
Expand Down Expand Up @@ -132,13 +132,13 @@ jobs:
- name: 'Unit testing'
uses: nick-fields/retry@v3
with:
max_attempts: 3
max_attempts: 2
retry_on: error
timeout_minutes: 50
command: |
testenv\Scripts\Activate.ps1
Set-Item -Path env:PYTHONMALLOC -Value "malloc"
pytest -n 6 --dist loadfile --durations=50 -v --cov=pyaedt --cov-report=xml --cov-report=html --junitxml=junit/test-results.xml _unittest
pytest -n 4 --dist loadfile --durations=50 -v --cov=pyaedt --cov-report=xml --cov-report=html --junitxml=junit/test-results.xml _unittest

- uses: codecov/codecov-action@v4
env:
Expand Down
2 changes: 1 addition & 1 deletion _unittest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
settings.enable_error_handler = False
settings.enable_desktop_logs = False
settings.desktop_launch_timeout = 180

settings.release_on_exception = False

from pyaedt import Edb
from pyaedt import Hfss
Expand Down
18 changes: 9 additions & 9 deletions _unittest/test_12_PostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,18 @@ def test_18_diff_plot(self, diff_test):
context="Differential Pairs",
)
assert data1.primary_sweep == "Freq"
data1.plot(formula="db20")
data1.plot(formula="db20", snapshot_path=os.path.join(self.local_scratch.path, "temp1.jpg"))
data1.primary_sweep = "l1"
assert data1.primary_sweep == "l1"
assert len(data1.data_magnitude()) == 5
assert data1.plot("S(Diff1, Diff1)")
assert data1.plot(formula="db20")
assert data1.plot(formula="db10")
assert data1.plot(formula="mag")
assert data1.plot(formula="re")
assert data1.plot(formula="im")
assert data1.plot(formula="phasedeg")
assert data1.plot(formula="phaserad")
assert data1.plot("S(Diff1, Diff1)", snapshot_path=os.path.join(self.local_scratch.path, "temp2.jpg"))
assert data1.plot(formula="db20", snapshot_path=os.path.join(self.local_scratch.path, "temp3.jpg"))
assert data1.plot(formula="db10", snapshot_path=os.path.join(self.local_scratch.path, "temp4.jpg"))
assert data1.plot(formula="mag", snapshot_path=os.path.join(self.local_scratch.path, "temp5.jpg"))
assert data1.plot(formula="re", snapshot_path=os.path.join(self.local_scratch.path, "temp6.jpg"))
assert data1.plot(formula="im", snapshot_path=os.path.join(self.local_scratch.path, "temp7.jpg"))
assert data1.plot(formula="phasedeg", snapshot_path=os.path.join(self.local_scratch.path, "temp8.jpg"))
assert data1.plot(formula="phaserad", snapshot_path=os.path.join(self.local_scratch.path, "temp9.jpg"))

assert diff_test.create_touchstone_report(
plot_name="Diff_plot",
Expand Down
8 changes: 5 additions & 3 deletions pyaedt/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def __new__(cls, *args, **kwargs):
pyaedt_logger.info("Initializing new Desktop session.")
return object.__new__(cls)

@pyaedt_function_handler()
def __init__(
self,
specified_version=None,
Expand Down Expand Up @@ -942,8 +943,6 @@ def _initialize(
self.is_grpc_api = True
last_session.parent_desktop_id.append(self.aedt_process_id)
return True
if new_session:
self.launched_by_pyaedt = new_session
oapp = python_grpc_wrapper.CreateAedtApplication(machine, port, non_graphical, new_session)
if oapp:

Expand Down Expand Up @@ -1471,7 +1470,10 @@ def release_desktop(self, close_projects=True, close_on_exit=True):
if not result:
self.logger.error("Error releasing desktop.")
return False
self.logger.info("Desktop has been released")
if close_on_exit:
self.logger.info("Desktop has been released and closed.")
else:
self.logger.info("Desktop has been released.")
del _desktop_sessions[self.aedt_process_id]
props = [a for a in dir(self) if not a.startswith("__")]
for a in props:
Expand Down
66 changes: 38 additions & 28 deletions pyaedt/generic/general_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,38 +198,48 @@ def _check_types(arg):
return ""


def raise_exception(e):
if not settings.enable_error_handler:
if settings.release_on_exception:
from pyaedt.generic.desktop_sessions import _desktop_sessions

for v in list(_desktop_sessions.values())[:]:
v.release_desktop(v.launched_by_pyaedt, v.launched_by_pyaedt)
raise e
else:
return False


def _function_handler_wrapper(user_function, **deprecated_kwargs):

def wrapper(*args, **kwargs):

if deprecated_kwargs and kwargs:
deprecate_kwargs(user_function.__name__, kwargs, deprecated_kwargs)
if not settings.enable_error_handler:
result = user_function(*args, **kwargs)
return result
else:
try:
settings.time_tick = time.time()
out = user_function(*args, **kwargs)
if settings.enable_debug_logger or settings.enable_debug_edb_logger:
_log_method(user_function, args, kwargs)
return out
except MethodNotSupportedError:
message = "This method is not supported in current AEDT design type."
if settings.enable_screen_logs:
pyaedt_logger.error("**************************************************************")
pyaedt_logger.error(
"PyAEDT error on method {}: {}. Check again".format(user_function.__name__, message)
)
pyaedt_logger.error("**************************************************************")
pyaedt_logger.error("")
if settings.enable_file_logs:
settings.error(message)
return False
except GrpcApiError:
_exception(sys.exc_info(), user_function, args, kwargs, "AEDT grpc API call Error")
return False
except BaseException:
_exception(sys.exc_info(), user_function, args, kwargs, str(sys.exc_info()[1]).capitalize())
return False
try:
settings.time_tick = time.time()
out = user_function(*args, **kwargs)
if settings.enable_debug_logger or settings.enable_debug_edb_logger:
_log_method(user_function, args, kwargs)
return out
except MethodNotSupportedError as e:
message = "This method is not supported in current AEDT design type."
if settings.enable_screen_logs:
pyaedt_logger.error("**************************************************************")
pyaedt_logger.error(
"PyAEDT error on method {}: {}. Check again".format(user_function.__name__, message)
)
pyaedt_logger.error("**************************************************************")
pyaedt_logger.error("")
if settings.enable_file_logs:
settings.error(message)
raise_exception(e)
except GrpcApiError as e:
_exception(sys.exc_info(), user_function, args, kwargs, "AEDT grpc API call Error")
raise_exception(e)
except BaseException as e:
_exception(sys.exc_info(), user_function, args, kwargs, str(sys.exc_info()[1]).capitalize())
raise_exception(e)

return wrapper

Expand Down
15 changes: 15 additions & 0 deletions pyaedt/generic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self):
self._enable_debug_internal_methods_logger = False
self._enable_debug_logger = False
self._enable_error_handler = True
self._release_on_exception = True
self._aedt_version = None
self._aedt_install_dir = None
self._use_multi_desktop = False
Expand Down Expand Up @@ -72,6 +73,20 @@ def __init__(self):
self.__lazy_load = True
self.__objects_lazy_load = False

@property
def release_on_exception(self):
"""

Returns
-------

"""
return self._release_on_exception

@release_on_exception.setter
def release_on_exception(self, value):
self._release_on_exception = value

@property
def objects_lazy_load(self):
"""Flag for enabling and disabling the lazy load.
Expand Down
Loading