Skip to content

Commit 6060cab

Browse files
feat: allow passing strings to the converter from the terminal (#3679)
* feat: allow passing raw strings to the converter * chore: adding changelog file 3679.miscellaneous.md [dependabot-skip] * refactor: using click options to avoid having to set the files manual. Better behavior. Adding short options. Improved tests * chore: adding changelog file 3679.documentation.md [dependabot-skip] * chore: adding changelog file 3679.documentation.md [dependabot-skip] * fix: removing unused variables. * fix: test * fix: test * test: skip test if click is not available. --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 7e86b2a commit 6060cab

File tree

6 files changed

+218
-116
lines changed

6 files changed

+218
-116
lines changed

doc/changelog.d/3679.documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: allow passing strings to the converter from the terminal

doc/source/user_guide/convert.rst

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ file to a Python file named ``python.py``:
1717

1818
.. code:: console
1919
20-
$ pymapdl convert mapdl.dat -o python.py
20+
$ echo -ne "/prep7" > mapdl.dat
21+
$ pymapdl convert --file mapdl.dat --output python.py
2122
22-
File mapdl.dat successfully converted to python.py.
23-
24-
The output argument is completely optional. If you don't specify it,
25-
the ``py`` extension is used for the file that is outputted:
23+
The output argument is optional. If you don't specify it,
24+
the converted code is shown on the terminal:
2625

2726
.. code:: console
2827
29-
$ pymapdl convert mapdl.dat
28+
$ pymapdl convert -f mapdl.dat
29+
"""Script generated by ansys-mapdl-core version 0.69.dev0"""
30+
31+
from ansys.mapdl.core import launch_mapdl
32+
mapdl = launch_mapdl(loglevel="WARNING", print_com=True, check_parameter_names=False)
33+
mapdl.prep7()
3034
31-
File mapdl.dat successfully converted to mapdl.py.
35+
mapdl.exit()
3236
3337
You can use any option from the
3438
:func:`convert_script() <ansys.mapdl.core.convert_script>` function.
@@ -38,19 +42,26 @@ the script, you can use ``--auto-exit`` argument:
3842

3943
.. code:: console
4044
41-
$ pymapdl convert mapdl.dat --auto-exit False
42-
43-
File mapdl.dat successfully converted to mapdl.py.
45+
$ pymapdl convert -f mapdl.dat --auto-exit False
4446
4547
You can skip the imports by setting the ``--add_imports`` option
4648
to ``False``:
4749

4850
.. code:: console
4951
50-
$ pymapdl convert mapdl.dat --filename_out mapdl.out --add_imports
52+
$ pymapdl convert -f mapdl.dat --output mapdl.out --add_imports
5153
False
5254
53-
File mapdl.dat successfully converted to mapdl.out.
55+
You can also skip adding the import, the
56+
:func:`ansys.mapdl.core.launcher.launch_mapdl` call,
57+
and the :meth:`mapdl.exit() <ansys.mapdl.core.Mapdl.exit>` call by using
58+
the argument ``--only-code`` (``-oc``):
59+
60+
.. code:: console
61+
62+
$ pymapdl convert -f mapdl.dat --only-code
63+
mapdl.prep7()
64+
5465
5566
For more information about possible options, use the help
5667
command (``pymapdl convert --help``) or the

src/ansys/mapdl/core/cli/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
def main(ctx: click.Context):
3333
pass
3434

35-
from ansys.mapdl.core.cli.convert import convert
35+
from ansys.mapdl.core.cli.convert import convert as convert_cmd
3636
from ansys.mapdl.core.cli.list_instances import list_instances
37-
from ansys.mapdl.core.cli.start import start
38-
from ansys.mapdl.core.cli.stop import stop
37+
from ansys.mapdl.core.cli.start import start as start_cmd
38+
from ansys.mapdl.core.cli.stop import stop as stop_cmd
3939

40-
main.add_command(convert)
41-
main.add_command(start)
42-
main.add_command(stop)
40+
main.add_command(convert_cmd, name="convert")
41+
main.add_command(start_cmd, name="start")
42+
main.add_command(stop_cmd, name="stop")
4343
main.add_command(list_instances, name="list")
4444

4545

src/ansys/mapdl/core/cli/convert.py

Lines changed: 81 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,10 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23-
import os
24-
from typing import Any, List
23+
import sys
2524

2625
import click
2726

28-
_USING_PIPE: List[bool] = [False]
29-
30-
31-
def get_input_source(ctx: click.Context, param: Any, value: Any):
32-
if not value and not click.get_text_stream("stdin").isatty():
33-
_USING_PIPE[0] = True
34-
return click.get_text_stream("stdin").read().strip()
35-
else:
36-
return value
37-
3827

3928
@click.command(
4029
short_help="Convert APDL code to PyMAPDL code.",
@@ -44,99 +33,139 @@ def get_input_source(ctx: click.Context, param: Any, value: Any):
4433
4534
This example demonstrates the main use of this tool:
4635
47-
$ pymapdl convert mapdl.dat -o python.py
36+
$ pymapdl convert -f mapdl.dat -o python.py
4837
49-
File mapdl.dat successfully converted to python.py.
38+
If you omit the output argument, the converted code is shown on the screen.
5039
51-
The output argument is optional, in which case the "py" extension is used:
40+
You can use any option from ``ansys.mapdl.core.convert.convert_apdl_block`` function:
5241
53-
$ pymapdl convert mapdl.dat
42+
$ pymapdl convert -f mapdl.dat --auto-exit False
43+
\"\"\"Script generated by ansys-mapdl-core version 0.69.dev0\"\"\"
5444
55-
File mapdl.dat successfully converted to mapdl.py.
45+
from ansys.mapdl.core import launch_mapdl
46+
mapdl = launch_mapdl(loglevel="WARNING", print_com=True, check_parameter_names=False)
47+
mapdl.prep7()
5648
57-
You can use any option from ``ansys.mapdl.core.convert.convert_script`` function:
49+
mapdl.exit()
5850
59-
$ pymapdl convert mapdl.dat --auto-exit False
51+
You can skip the imports, and the launching and exit calls if the option `--only-code` (`-oc`)
52+
is given.
6053
61-
File mapdl.dat successfully converted to mapdl.py.
54+
$ pymapdl convert -f mapdl.dat -oc
55+
mapdl.prep7()
6256
63-
$ pymapdl convert mapdl.dat --filename_out mapdl.out --add_imports False
57+
You can also pipe content from files o command line into the converter.
6458
65-
File mapdl.dat successfully converted to mapdl.out.""",
59+
$ echo -ne "/prep7" | pymapdl convert -oc
60+
mapdl.prep7()
61+
62+
$ echo -ne "/prep7" > my_file.inp
63+
$ pymapdl convert -oc < my_file.inp
64+
mapdl.prep7()
65+
""",
66+
)
67+
@click.option(
68+
"--file",
69+
"-f",
70+
help="Name of the APDL input file to convert to PyMAPDL code.",
71+
type=click.File("r"),
72+
default=sys.stdin,
73+
)
74+
@click.option(
75+
"--output",
76+
"-o",
77+
default=sys.stdout,
78+
type=click.File("at"),
79+
help="Name of the output Python script.",
6680
)
67-
@click.argument("filename_in", callback=get_input_source, required=False)
68-
@click.option("-o", default=None, help="Name of the output Python script.")
69-
@click.option("--filename_out", default=None, help="Name of the output Python script.")
7081
@click.option(
7182
"--loglevel",
83+
"-ll",
7284
default="WARNING",
7385
help="Logging level of the ansys object within the script.",
7486
)
7587
@click.option(
7688
"--auto_exit",
89+
"-ae",
7790
default=True,
7891
type=bool,
7992
help="Adds a line to the end of the script to exit MAPDL. Default ``True``",
8093
)
8194
@click.option(
82-
"--line_ending", type=bool, default=None, help="When None, automatically is ``\n.``"
95+
"--line_ending",
96+
"-le",
97+
type=str,
98+
default=None,
99+
help="When None, automatically is ``\n.``",
83100
)
84101
@click.option(
85102
"--exec_file",
103+
"-e",
86104
default=None,
87105
type=str,
88106
help="Specify the location of the ANSYS executable and include it in the converter output ``launch_mapdl`` call.",
89107
)
90108
@click.option(
91109
"--macros_as_functions",
110+
"-mf",
92111
default=True,
93112
type=bool,
94113
help="Attempt to convert MAPDL macros to python functions.",
95114
)
96115
@click.option(
97116
"--use_function_names",
117+
"-fn",
98118
default=True,
99119
type=bool,
100120
help="Convert MAPDL functions to ansys.mapdl.core.Mapdl class methods. When ``True``, the MAPDL command ``K`` will be converted to ``mapdl.k``. When ``False``, it will be converted to ``mapdl.run('k')``.",
101121
)
102122
@click.option(
103123
"--show_log",
124+
"-sl",
104125
default=False,
105126
type=bool,
106127
help="Print the converted commands using a logger (from ``logging`` Python module).",
107128
)
108129
@click.option(
109130
"--add_imports",
131+
"-ai",
110132
default=True,
111133
type=bool,
112134
help='If ``True``, add the lines ``from ansys.mapdl.core import launch_mapdl`` and ``mapdl = launch_mapdl(loglevel="WARNING")`` to the beginning of the output file. This option is useful if you are planning to use the output script from another mapdl session. See examples section. This option overrides ``auto_exit``.',
113135
)
114136
@click.option(
115137
"--comment_solve",
138+
"-cs",
116139
default=False,
117140
type=bool,
118141
help='If ``True``, it will pythonically comment the lines that contain ``"SOLVE"`` or ``"/EOF"``.',
119142
)
120143
@click.option(
121144
"--cleanup_output",
145+
"-co",
122146
default=True,
123147
type=bool,
124148
help="If ``True`` the output is formatted using ``autopep8`` before writing the file or returning the string. This requires ``autopep8`` to be installed.",
125149
)
126150
@click.option(
127151
"--header",
152+
"-h",
128153
default=True,
129154
help="If ``True``, the default header is written in the first line of the output. If a string is provided, this string will be used as header.",
130155
)
131156
@click.option(
132157
"--print_com",
158+
"-pc",
133159
default=True,
134160
type=bool,
135161
help="Print command ``/COM`` arguments to python console. Defaults to ``True``.",
136162
)
137163
@click.option(
138164
"--only_commands",
165+
"-oc",
139166
default=False,
167+
is_flag=True,
168+
flag_value=True,
140169
type=bool,
141170
help="""If ``True``, it converts only the commands, meaning that header
142171
(``header=False``), imports (``add_imports=False``),
@@ -158,13 +187,13 @@ def get_input_source(ctx: click.Context, param: Any, value: Any):
158187
)
159188
@click.option(
160189
"--check_parameter_names",
190+
"--cpn",
161191
default=False,
162192
help="""Set MAPDL object to avoid parameter name checks (do not raise leading underscored parameter exceptions). Defaults to `False`.""",
163193
)
164194
def convert(
165-
filename_in: str,
166-
o: str,
167-
filename_out: str,
195+
file: str,
196+
output: str,
168197
loglevel: str,
169198
auto_exit: bool,
170199
line_ending: str,
@@ -183,64 +212,26 @@ def convert(
183212
check_parameter_names: bool,
184213
) -> None:
185214
"""Convert MAPDL code to PyMAPDL"""
186-
from ansys.mapdl.core.convert import convert_apdl_block, convert_script
187-
188-
if o:
189-
filename_out = o
190-
191-
if _USING_PIPE[0]:
192-
code_block = filename_in # from PIPE
193-
click.echo(
194-
convert_apdl_block(
195-
code_block,
196-
loglevel=loglevel,
197-
auto_exit=auto_exit,
198-
line_ending=line_ending,
199-
exec_file=exec_file,
200-
macros_as_functions=macros_as_functions,
201-
use_function_names=use_function_names,
202-
show_log=show_log,
203-
add_imports=add_imports,
204-
comment_solve=comment_solve,
205-
cleanup_output=cleanup_output,
206-
header=header,
207-
print_com=print_com,
208-
only_commands=only_commands,
209-
use_vtk=use_vtk,
210-
clear_at_start=clear_at_start,
211-
check_parameter_names=check_parameter_names,
212-
)
213-
)
214-
215-
else:
216-
convert_script(
217-
filename_in,
218-
filename_out,
219-
loglevel,
220-
auto_exit,
221-
line_ending,
222-
exec_file,
223-
macros_as_functions,
224-
use_function_names,
225-
show_log,
226-
add_imports,
227-
comment_solve,
228-
cleanup_output,
229-
header,
230-
print_com,
231-
only_commands,
232-
use_vtk,
233-
clear_at_start,
234-
check_parameter_names,
235-
)
236-
237-
if filename_out:
238-
click.echo(
239-
click.style("Success: ", fg="green")
240-
+ f"File {filename_in} successfully converted to {filename_out}."
241-
)
242-
else:
243-
click.echo(
244-
click.style("Success: ", fg="green")
245-
+ f"File {filename_in} successfully converted to {os.path.splitext(filename_in)[0] + '.py'}."
246-
)
215+
from ansys.mapdl.core.convert import convert_apdl_block
216+
217+
converted_code = convert_apdl_block(
218+
apdl_strings=file.read(),
219+
loglevel=loglevel,
220+
auto_exit=auto_exit,
221+
line_ending=line_ending,
222+
exec_file=exec_file,
223+
macros_as_functions=macros_as_functions,
224+
use_function_names=use_function_names,
225+
show_log=show_log,
226+
add_imports=add_imports,
227+
comment_solve=comment_solve,
228+
cleanup_output=cleanup_output,
229+
header=header,
230+
print_com=print_com,
231+
only_commands=only_commands,
232+
use_vtk=use_vtk,
233+
clear_at_start=clear_at_start,
234+
check_parameter_names=check_parameter_names,
235+
)
236+
237+
click.echo(converted_code, file=output)

0 commit comments

Comments
 (0)