aspen_pysys.app
Package containing all Pythonic functionality for the HYSYS app.
class
HysysApp:
22class HysysApp: 23 """Class that interfaces with the HYSYS application.""" 24 25 _logger = logging.getLogger(__name__) 26 27 def __init__(self, app: CDispatch) -> None: 28 self._app: CDispatch = app 29 30 @staticmethod 31 def dispatch( 32 *, 33 should_be_visible: bool = True, 34 should_log_version: bool = False, 35 ) -> HysysApp: 36 """Dispatch the HYSYS app. 37 38 Args: 39 should_be_visible (bool, optional): If the app should be visible. Defaults to True. 40 should_log_version (bool, optional): If the app version should be logged. Defaults to False. 41 42 Returns: 43 HysysApp: HYSYS app wrapper 44 45 Raises: 46 PysysError: App was unable to be dispatched. 47 """ # noqa: E501 48 app: CDispatch | None = win32com.client.Dispatch(APP_STR) 49 50 if app is None: 51 message = "The HYSYS app was unable to be dispatched." 52 raise PysysError(message) 53 54 if should_be_visible: 55 app.Visible = 1 56 57 if should_log_version: 58 HysysApp._logger.info(app.Application.LongVersion) 59 60 return HysysApp(app) 61 62 def open(self, filepath: str) -> HysysCase: 63 """Open a simulation case. 64 65 Args: 66 filepath (str): Path to the simulation case 67 68 Returns: 69 HysysCase: Simulation case 70 71 Raises: 72 PysysError: Cannot open case at provided filepath. 73 """ 74 try: 75 case_object: CDispatch = self._app.SimulationCases.Open(filepath) 76 77 simcase = HysysCase(case_object) 78 simcase.activate() 79 except com_error as err: 80 message = f"Error opening filepath '{filepath}'." 81 raise PysysError(message) from err 82 else: 83 return simcase 84 85 def quit(self) -> None: 86 """Quit the HYSYS app. 87 88 Raises: 89 PysysError: Error quitting app. 90 """ 91 try: 92 self._app.Quit() 93 except com_error as err: 94 message = "Error quitting app." 95 raise PysysError(message) from err 96 97 def _get_active_document(self) -> CDispatch: 98 return self._app.ActiveDocument 99 100 def has_active_document(self) -> bool: 101 """Check if the app has an active document. 102 103 Returns: 104 bool: If the app has an active document 105 """ 106 return self._get_active_document() is not None 107 108 def open_active_document(self) -> HysysCase: 109 """Open the active document if present. 110 111 Returns: 112 HysysCase: Opened simulation case 113 114 Raises: 115 PysysError: No active document. 116 PysysError: Error opening active document. 117 """ 118 if not self.has_active_document(): 119 message = "There is no active document." 120 raise PysysError(message) 121 122 try: 123 case_object = self._get_active_document() 124 125 simcase = HysysCase(case_object) 126 simcase.activate() 127 except com_error as err: 128 message = "Error opening active document." 129 raise PysysError(message) from err 130 else: 131 return simcase 132 133 def close_all(self) -> None: 134 """Close all open simulation cases. 135 136 Raises: 137 PysysError: Error closing simulation cases. 138 """ 139 try: 140 self._app.SimulationCases.Close() 141 except com_error as err: 142 message = "Error closing simulation cases." 143 raise PysysError(message) from err
Class that interfaces with the HYSYS application.
@staticmethod
def
dispatch( *, should_be_visible: bool = True, should_log_version: bool = False) -> HysysApp:
30 @staticmethod 31 def dispatch( 32 *, 33 should_be_visible: bool = True, 34 should_log_version: bool = False, 35 ) -> HysysApp: 36 """Dispatch the HYSYS app. 37 38 Args: 39 should_be_visible (bool, optional): If the app should be visible. Defaults to True. 40 should_log_version (bool, optional): If the app version should be logged. Defaults to False. 41 42 Returns: 43 HysysApp: HYSYS app wrapper 44 45 Raises: 46 PysysError: App was unable to be dispatched. 47 """ # noqa: E501 48 app: CDispatch | None = win32com.client.Dispatch(APP_STR) 49 50 if app is None: 51 message = "The HYSYS app was unable to be dispatched." 52 raise PysysError(message) 53 54 if should_be_visible: 55 app.Visible = 1 56 57 if should_log_version: 58 HysysApp._logger.info(app.Application.LongVersion) 59 60 return HysysApp(app)
Dispatch the HYSYS app.
Arguments:
- should_be_visible (bool, optional): If the app should be visible. Defaults to True.
- should_log_version (bool, optional): If the app version should be logged. Defaults to False.
Returns:
HysysApp: HYSYS app wrapper
Raises:
- PysysError: App was unable to be dispatched.
def
open(self, filepath: str) -> aspen_pysys.case.hysys_case.HysysCase:
62 def open(self, filepath: str) -> HysysCase: 63 """Open a simulation case. 64 65 Args: 66 filepath (str): Path to the simulation case 67 68 Returns: 69 HysysCase: Simulation case 70 71 Raises: 72 PysysError: Cannot open case at provided filepath. 73 """ 74 try: 75 case_object: CDispatch = self._app.SimulationCases.Open(filepath) 76 77 simcase = HysysCase(case_object) 78 simcase.activate() 79 except com_error as err: 80 message = f"Error opening filepath '{filepath}'." 81 raise PysysError(message) from err 82 else: 83 return simcase
Open a simulation case.
Arguments:
- filepath (str): Path to the simulation case
Returns:
HysysCase: Simulation case
Raises:
- PysysError: Cannot open case at provided filepath.
def
quit(self) -> None:
85 def quit(self) -> None: 86 """Quit the HYSYS app. 87 88 Raises: 89 PysysError: Error quitting app. 90 """ 91 try: 92 self._app.Quit() 93 except com_error as err: 94 message = "Error quitting app." 95 raise PysysError(message) from err
Quit the HYSYS app.
Raises:
- PysysError: Error quitting app.
def
has_active_document(self) -> bool:
100 def has_active_document(self) -> bool: 101 """Check if the app has an active document. 102 103 Returns: 104 bool: If the app has an active document 105 """ 106 return self._get_active_document() is not None
Check if the app has an active document.
Returns:
bool: If the app has an active document
def
open_active_document(self) -> aspen_pysys.case.hysys_case.HysysCase:
108 def open_active_document(self) -> HysysCase: 109 """Open the active document if present. 110 111 Returns: 112 HysysCase: Opened simulation case 113 114 Raises: 115 PysysError: No active document. 116 PysysError: Error opening active document. 117 """ 118 if not self.has_active_document(): 119 message = "There is no active document." 120 raise PysysError(message) 121 122 try: 123 case_object = self._get_active_document() 124 125 simcase = HysysCase(case_object) 126 simcase.activate() 127 except com_error as err: 128 message = "Error opening active document." 129 raise PysysError(message) from err 130 else: 131 return simcase
Open the active document if present.
Returns:
HysysCase: Opened simulation case
Raises:
- PysysError: No active document.
- PysysError: Error opening active document.
def
close_all(self) -> None:
133 def close_all(self) -> None: 134 """Close all open simulation cases. 135 136 Raises: 137 PysysError: Error closing simulation cases. 138 """ 139 try: 140 self._app.SimulationCases.Close() 141 except com_error as err: 142 message = "Error closing simulation cases." 143 raise PysysError(message) from err
Close all open simulation cases.
Raises:
- PysysError: Error closing simulation cases.