Skip to content

Commit 9ce53dd

Browse files
germa89akaszynski
andauthored
implement logger in convert_script (#717)
* Added convert logger * Added test unit * Removed unused package. * Apply suggestions from code review Co-authored-by: Alex Kaszynski <[email protected]> * Update test logger. * Update tests/test_convert.py Co-authored-by: Alex Kaszynski <[email protected]> Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 98b7bb4 commit 9ce53dd

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

ansys/mapdl/core/convert.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from warnings import warn
3+
from logging import Logger, StreamHandler
34

45
from ansys.mapdl.core import __version__
56
from 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

8188
def 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+
166201
class 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

tests/test_convert.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22
import pytest
3+
34
from ansys.mapdl import core as pymapdl
45
from ansys.mapdl.core import examples
5-
from ansys.mapdl.core.convert import convert_apdl_block
6+
from ansys.mapdl.core.convert import convert_apdl_block, FileTranslator
67

78
nblock = """nblock,3,,326253
89
(1i9,3e20.9e3)
@@ -94,3 +95,16 @@ def test_convert_block_commands(tmpdir, cmd):
9495
pyblock = convert_apdl_block(apdl_strings=apdl_block.split('\n'))
9596
pyblock = '\n'.join(pyblock)
9697
assert pymapdl_output[cmd] in pyblock
98+
99+
100+
def test_logger(capsys):
101+
102+
apdl_ = """FINISH
103+
/PREP7
104+
""".split('\n')
105+
106+
translator = FileTranslator(line_ending='\n', show_log=True)
107+
for line in apdl_:
108+
translator.translate_line(line)
109+
std = capsys.readouterr()
110+
assert all(['Converted' in each for each in std.err.split('\n')[:-1]]) # last one is an empty line.

0 commit comments

Comments
 (0)