11import os
22from warnings import warn
3+ from logging import Logger , StreamHandler
34
45from ansys .mapdl .core import __version__
56from ansys .mapdl .core .misc import is_float
@@ -21,6 +22,7 @@ def convert_script(
2122 exec_file = None ,
2223 macros_as_functions = True ,
2324 use_function_names = True ,
25+ show_log = False
2426):
2527 """Converts an ANSYS input file to a python PyMAPDL script.
2628
@@ -51,6 +53,10 @@ def convert_script(
5153 converted to ``mapdl.k``. When ``False``, it will be
5254 converted to ``mapdl.run('k')``.
5355
56+ show_log : bool, optional
57+ Print the converted commands using a logger (from ``logging``
58+ Python module).
59+
5460 Returns
5561 -------
5662 list
@@ -71,21 +77,22 @@ def convert_script(
7177 line_ending = line_ending ,
7278 exec_file = exec_file ,
7379 macros_as_functions = macros_as_functions ,
74- use_function_names = use_function_names
80+ use_function_names = use_function_names ,
81+ show_log = show_log
7582 )
7683
7784 translator .save (filename_out )
7885 return translator .lines
7986
8087
8188def convert_apdl_block (apdl_strings ,
82- loglevel = "WARNING" ,
83- auto_exit = True ,
84- line_ending = None ,
85- exec_file = None ,
86- macros_as_functions = True ,
87- use_function_names = True ,
88- ):
89+ loglevel = "WARNING" ,
90+ auto_exit = True ,
91+ line_ending = None ,
92+ exec_file = None ,
93+ macros_as_functions = True ,
94+ use_function_names = True ,
95+ show_log = False ):
8996 """Converts an ANSYS input string to a python PyMAPDL string.
9097
9198 Parameters
@@ -115,6 +122,10 @@ def convert_apdl_block(apdl_strings,
115122 converted to ``mapdl.k``. When ``False``, it will be
116123 converted to ``mapdl.run('k')``.
117124
125+ show_log : bool, optional
126+ Print the converted commands using a logger (from ``logging``
127+ Python module).
128+
118129 Returns
119130 -------
120131 list
@@ -128,7 +139,8 @@ def convert_apdl_block(apdl_strings,
128139 line_ending = line_ending ,
129140 exec_file = exec_file ,
130141 macros_as_functions = macros_as_functions ,
131- use_function_names = use_function_names )
142+ use_function_names = use_function_names ,
143+ show_log = show_log )
132144
133145 if isinstance (apdl_strings , str ):
134146 return translator .line_ending .join (translator .lines )
@@ -142,6 +154,7 @@ def _convert(apdl_strings,
142154 exec_file = None ,
143155 macros_as_functions = True ,
144156 use_function_names = True ,
157+ show_log = False
145158 ):
146159
147160 translator = FileTranslator (
@@ -150,6 +163,7 @@ def _convert(apdl_strings,
150163 exec_file = exec_file ,
151164 macros_as_functions = macros_as_functions ,
152165 use_function_names = use_function_names ,
166+ show_log = show_log
153167 )
154168
155169 if isinstance (apdl_strings , str ):
@@ -163,6 +177,27 @@ def _convert(apdl_strings,
163177 return translator
164178
165179
180+ class Lines (list ):
181+ def __init__ (self , mute ):
182+ self ._log = Logger ('convert_logger' )
183+ self ._setup_logger ()
184+ self ._mute = mute
185+ super ().__init__ ()
186+
187+ def append (self , item , mute = True ):
188+ # append the item to itself (the list)
189+ if not self ._mute :
190+ self ._log .info (msg = f"Converted: '{ item } '" )
191+ super (Lines , self ).append (item )
192+
193+ def _setup_logger (self ):
194+ stdhdl = StreamHandler ()
195+ stdhdl .setLevel (10 )
196+ stdhdl .set_name ('stdout' )
197+ self ._log .addHandler (stdhdl )
198+ self ._log .propagate = True
199+
200+
166201class FileTranslator :
167202 obj_name = "mapdl"
168203 indent = ""
@@ -175,9 +210,10 @@ def __init__(
175210 exec_file = None ,
176211 macros_as_functions = True ,
177212 use_function_names = True ,
213+ show_log = False
178214 ):
179215 self ._non_interactive_level = 0
180- self .lines = []
216+ self .lines = Lines ( mute = not show_log )
181217 self ._functions = []
182218 if line_ending :
183219 self .line_ending = line_ending
0 commit comments