Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 60532b5

Browse files
koubaaMohamed KoubaaRobPasMue
authored
support any application with save-ansys-path (#164)
Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]>
1 parent 7c91384 commit 60532b5

File tree

5 files changed

+140
-36
lines changed

5 files changed

+140
-36
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Application plugin for ansys-tools-path.
3+
4+
This defines the interface of a plugin, which is implemented using a module.
5+
"""
6+
7+
# TODO - consider using pluggy?
8+
9+
10+
class ApplicationPlugin:
11+
def is_valid_executable_path(exe_loc: str) -> bool:
12+
raise Exception("This is just a base class.")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (C) 2023 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+
23+
"""dyna-specific logic for ansys-tools-path."""
24+
25+
26+
def is_valid_executable_path(exe_loc: str) -> bool:
27+
# dyna paths can be anything
28+
return True
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (C) 2023 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+
23+
"""MAPDL-specific logic for ansys-tools-path."""
24+
25+
import os
26+
import re
27+
28+
29+
def is_valid_executable_path(exe_loc: str) -> bool:
30+
return (
31+
os.path.isfile(exe_loc)
32+
and re.search(r"ansys\d\d\d", os.path.basename(os.path.normpath(exe_loc))) is not None
33+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (C) 2023 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+
23+
"""Mechanical-specific logic for ansys-tools-path."""
24+
25+
import os
26+
import re
27+
28+
from ansys.tools.path.misc import is_windows
29+
30+
31+
def is_valid_executable_path(exe_loc: str) -> bool:
32+
if is_windows(): # pragma: no cover
33+
return (
34+
os.path.isfile(exe_loc)
35+
and re.search(
36+
"AnsysWBU.exe", os.path.basename(os.path.normpath(exe_loc)), re.IGNORECASE
37+
)
38+
is not None
39+
)
40+
return (
41+
os.path.isfile(exe_loc)
42+
and re.search(".workbench", os.path.basename(os.path.normpath(exe_loc))) is not None
43+
)

src/ansys/tools/path/path.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
import platformdirs
1111

12+
from ansys.tools.path.applications import ApplicationPlugin, dyna, mapdl, mechanical
1213
from ansys.tools.path.misc import is_float, is_linux, is_windows
1314

15+
PLUGINS: Dict[str, ApplicationPlugin] = {"mechanical": mechanical, "dyna": dyna, "mapdl": mapdl}
16+
1417
LOG = logging.getLogger(__name__)
1518

16-
PRODUCT_TYPE = Literal["mapdl", "mechanical"]
19+
PRODUCT_TYPE = Literal["mapdl", "mechanical", "dyna"]
1720
SUPPORTED_VERSIONS_TYPE = Dict[int, str]
1821

1922
LINUX_DEFAULT_DIRS = [["/", "usr", "ansys_inc"], ["/", "ansys_inc"], ["/", "install", "ansys_inc"]]
@@ -409,10 +412,11 @@ def find_dyna(
409412

410413

411414
def _find_installation(
412-
product: PRODUCT_TYPE,
415+
product: str,
413416
version: Optional[float] = None,
414417
supported_versions: SUPPORTED_VERSIONS_TYPE = SUPPORTED_ANSYS_VERSIONS,
415418
) -> Union[Tuple[str, float], Tuple[Literal[""], Literal[""]]]:
419+
416420
if product == "mapdl":
417421
return find_mapdl(version, supported_versions)
418422
elif product == "mechanical":
@@ -435,29 +439,12 @@ def find_ansys(
435439
return _find_installation("mapdl", version, supported_versions)
436440

437441

442+
def _has_plugin(product: str) -> bool:
443+
return product in PLUGINS
444+
445+
438446
def is_valid_executable_path(product: PRODUCT_TYPE, exe_loc: str) -> bool:
439-
if product == "mapdl":
440-
return (
441-
os.path.isfile(exe_loc)
442-
and re.search(r"ansys\d\d\d", os.path.basename(os.path.normpath(exe_loc))) is not None
443-
)
444-
elif product == "dyna":
445-
# dyna executable paths could be anything, really
446-
return True
447-
elif product == "mechanical":
448-
if is_windows(): # pragma: no cover
449-
return (
450-
os.path.isfile(exe_loc)
451-
and re.search(
452-
"AnsysWBU.exe", os.path.basename(os.path.normpath(exe_loc)), re.IGNORECASE
453-
)
454-
is not None
455-
)
456-
return (
457-
os.path.isfile(exe_loc)
458-
and re.search(".workbench", os.path.basename(os.path.normpath(exe_loc))) is not None
459-
)
460-
raise Exception("unexpected application")
447+
return PLUGINS[product].is_valid_executable_path(exe_loc)
461448

462449

463450
def _is_common_executable_path(product: PRODUCT_TYPE, exe_loc: str) -> bool:
@@ -505,10 +492,10 @@ def _is_common_executable_path(product: PRODUCT_TYPE, exe_loc: str) -> bool:
505492
raise Exception("unexpected application")
506493

507494

508-
def _change_default_path(product: PRODUCT_TYPE, exe_loc: str) -> None:
495+
def _change_default_path(application: str, exe_loc: str) -> None:
509496
if os.path.isfile(exe_loc):
510497
config_data = _read_config_file()
511-
config_data[product] = exe_loc
498+
config_data[application] = exe_loc
512499
_write_config_file(config_data)
513500
else:
514501
raise FileNotFoundError("File %s is invalid or does not exist" % exe_loc)
@@ -609,17 +596,16 @@ def change_default_ansys_path(exe_loc: str) -> None:
609596
def _save_path(
610597
product: PRODUCT_TYPE, exe_loc: Optional[str] = None, allow_prompt: bool = True
611598
) -> str:
612-
if exe_loc is None:
599+
has_plugin = _has_plugin(product)
600+
if exe_loc is None and has_plugin:
613601
exe_loc, _ = _find_installation(product)
614-
615-
if is_valid_executable_path(product, exe_loc):
616-
_check_uncommon_executable_path(product, exe_loc)
617-
618-
_change_default_path(product, exe_loc)
619-
return exe_loc
620-
621-
if allow_prompt:
602+
if exe_loc == "" and allow_prompt:
622603
exe_loc = _prompt_path(product) # pragma: no cover
604+
605+
if has_plugin:
606+
if is_valid_executable_path(product, exe_loc):
607+
_check_uncommon_executable_path(product, exe_loc)
608+
_change_default_path(product, exe_loc)
623609
return exe_loc
624610

625611

@@ -949,7 +935,7 @@ def _read_executable_path_from_config_file(product_name: PRODUCT_TYPE) -> Option
949935

950936

951937
def _get_application_path(
952-
product: PRODUCT_TYPE,
938+
product: str,
953939
allow_input: bool = True,
954940
version: Optional[float] = None,
955941
find: bool = True,
@@ -960,6 +946,8 @@ def _get_application_path(
960946
return exe_loc
961947

962948
LOG.debug(f"{product} path not found in config file")
949+
if not _has_plugin(product):
950+
raise Exception(f"Application {product} not registered.")
963951

964952
if find:
965953
try:

0 commit comments

Comments
 (0)