99
1010import platformdirs
1111
12+ from ansys .tools .path .applications import ApplicationPlugin , dyna , mapdl , mechanical
1213from ansys .tools .path .misc import is_float , is_linux , is_windows
1314
15+ PLUGINS : Dict [str , ApplicationPlugin ] = {"mechanical" : mechanical , "dyna" : dyna , "mapdl" : mapdl }
16+
1417LOG = logging .getLogger (__name__ )
1518
16- PRODUCT_TYPE = Literal ["mapdl" , "mechanical" ]
19+ PRODUCT_TYPE = Literal ["mapdl" , "mechanical" , "dyna" ]
1720SUPPORTED_VERSIONS_TYPE = Dict [int , str ]
1821
1922LINUX_DEFAULT_DIRS = [["/" , "usr" , "ansys_inc" ], ["/" , "ansys_inc" ], ["/" , "install" , "ansys_inc" ]]
@@ -409,10 +412,11 @@ def find_dyna(
409412
410413
411414def _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+
438446def 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
463450def _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:
609596def _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
951937def _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