3434"""
3535
3636import atexit
37- import os
37+ from pathlib import Path
3838import sys
39+ from IPython import get_ipython
40+ import tempfile
3941
4042aedt_process_id = int (sys .argv [1 ])
4143version = sys .argv [2 ]
4244print ("Loading the PyAEDT Console." )
4345
44- try :
46+ try : # pragma: no cover
4547 if version <= "2023.1" :
4648 from pyaedt import Desktop
4749 from pyaedt .generic .general_methods import active_sessions
4850 from pyaedt .generic .general_methods import is_windows
4951 else :
52+ from ansys .aedt .core import *
53+ import ansys .aedt .core # noqa: F401
5054 from ansys .aedt .core import Desktop
5155 from ansys .aedt .core .generic .general_methods import active_sessions
5256 from ansys .aedt .core .generic .general_methods import is_windows
53- except ImportError :
57+ from ansys .aedt .core .generic .file_utils import available_file_name
58+
59+ except ImportError : # pragma: no cover
5460 # Debug only purpose. If the tool is added to the ribbon from a GitHub clone, then a link
5561 # to PyAEDT is created in the personal library.
56- console_setup_dir = os . path . dirname (__file__ )
57- if "PersonalLib" in console_setup_dir :
58- sys .path .append (os . path . join (console_setup_dir , "../.." , ".." , ".." ))
62+ console_setup_dir = Path (__file__ ). resolve (). parent
63+ if "PersonalLib" in console_setup_dir . parts :
64+ sys .path .append (str (console_setup_dir / ".." / ".." / ".." ))
5965 if version <= "2023.1" :
6066 from pyaedt import Desktop
6167 from pyaedt .generic .general_methods import active_sessions
6268 from pyaedt .generic .general_methods import is_windows
6369 else :
70+ from ansys .aedt .core import * # noqa: F401
71+ import ansys .aedt .core # noqa: F401
6472 from ansys .aedt .core import Desktop
6573 from ansys .aedt .core .generic .general_methods import active_sessions
6674 from ansys .aedt .core .generic .general_methods import is_windows
75+ from ansys .aedt .core .generic .file_utils import available_file_name
6776
6877
69- def release (d ):
78+ def release (d ): # pragma: no cover
7079 d .logger .info ("Exiting the PyAEDT Console." )
7180
7281 d .release_desktop (False , False )
@@ -78,11 +87,11 @@ def release(d):
7887
7988
8089sessions = active_sessions (version = version , student_version = False )
81- if aedt_process_id in sessions :
90+ if aedt_process_id in sessions : # pragma: no cover
8291 session_found = True
8392 if sessions [aedt_process_id ] != - 1 :
8493 port = sessions [aedt_process_id ]
85- if not session_found :
94+ if not session_found : # pragma: no cover
8695 sessions = active_sessions (version = version , student_version = True )
8796 if aedt_process_id in sessions :
8897 session_found = True
@@ -91,7 +100,7 @@ def release(d):
91100 port = sessions [aedt_process_id ]
92101
93102error = False
94- if port :
103+ if port : # pragma: no cover
95104 desktop = Desktop (
96105 version = version ,
97106 port = port ,
@@ -100,7 +109,7 @@ def release(d):
100109 close_on_exit = False ,
101110 student_version = student_version ,
102111 )
103- elif is_windows :
112+ elif is_windows : # pragma: no cover
104113 desktop = Desktop (
105114 version = version ,
106115 aedt_process_id = aedt_process_id ,
@@ -109,26 +118,25 @@ def release(d):
109118 close_on_exit = False ,
110119 student_version = student_version ,
111120 )
112- else :
121+ else : # pragma: no cover
113122 print ("Error. AEDT should be started in gRPC mode in Linux to connect to PyAEDT" )
114123 print ("use ansysedt -grpcsrv portnumber command." )
115124 error = True
116- if not error : # pragma: no cover
125+
126+ if not error : # pragma: no cover
117127 print (" " )
118128
119129 print ("\033 [92m****************************************************************" )
120130 print (f"* ElectronicsDesktop { version } Process ID { aedt_process_id } " )
121131 print (f"* CPython { sys .version .split (' ' )[0 ]} " )
122132 print ("*---------------------------------------------------------------" )
123- print ("* Example: \033 [94m hfss = ansys.aedt.core.Hfss() \033 [92m" )
124- print ("* Example: \033 [94m m2d = ansys.aedt.core.Maxwell2d() \033 [92m" )
133+ print ("* Example: \033 [94m hfss = Hfss() \033 [92m" )
134+ print ("* Example: \033 [94m m2d = Maxwell2d() \033 [92m" )
135+ print ("* Desktop object is initialized: \033 [94mdesktop.logger.info('Hello world')\033 [92m" )
125136 print ("* \033 [31mType exit() to close the console and release the desktop. \033 [92m " )
126- print ("* desktop object is initialized and available. Example: " )
127- print ("* \033 [94mdesktop.logger.info('Hello world')\033 [92m" )
128137 print ("****************************************************************\033 [0m" )
129138 print (" " )
130- print (" " )
131- print (" " )
139+
132140 if is_windows :
133141 try :
134142 import win32api
@@ -156,3 +164,44 @@ def signal_handler(sig, frame):
156164 except ImportError :
157165 pass
158166 atexit .register (release , desktop )
167+
168+ if version > "2023.1" : # pragma: no cover
169+
170+ log_file = Path (tempfile .gettempdir ()) / "pyaedt_script.py"
171+ log_file = available_file_name (log_file )
172+
173+ with open (log_file , 'a' , encoding = 'utf-8' ) as f :
174+ f .write ("# PyAEDT script recorded from PyAEDT Console:\n \n " )
175+ f .write ("import ansys.aedt.core\n " )
176+ f .write ("from ansys.aedt.core import *\n " )
177+
178+ def log_successful_command (result ):
179+ """
180+ IPython Hook: Executes after every command (cell).
181+ Logs the input command only if 'result.error_in_exec' is False (no exception).
182+ """
183+ # Check for execution error
184+ if not result .error_in_exec :
185+ command = result .info .raw_cell .strip ()
186+
187+ # Avoid logging empty lines, comments, or the hook code itself
188+ if command and not command .startswith ('#' ) and "log_successful_command" not in command :
189+ try :
190+ # Append the successful command to the log file
191+ with open (log_file , 'a' , encoding = 'utf-8' ) as f :
192+ f .write (command + "\n " )
193+ except Exception as e :
194+ # Handle potential file writing errors
195+ print (f"ERROR: Failed to write to log file: { e } " )
196+
197+
198+ # Register the Hook
199+ ip = get_ipython ()
200+ if ip :
201+ # Register the function to run after every command execution
202+ ip .events .register ('post_run_cell' , log_successful_command )
203+ # Inform the user that logging is active
204+ print (f"Successful commands will be saved to: \033 [94m'{ log_file } '\033 [92m" )
205+ print (" " )
206+ print (" " )
207+ print (" " )
0 commit comments