Skip to content

Commit 7c8d791

Browse files
committed
feat: adding warning info at the top of a command
1 parent 7a754de commit 7c8d791

File tree

4 files changed

+96
-18
lines changed

4 files changed

+96
-18
lines changed

config.yaml

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,42 @@ specific_command_mapping:
2020
"/INQUIRE": inquire
2121

2222
ignored_commands:
23-
- "*VWR"
24-
- "*MWR"
25-
- "C***"
26-
- "*CFO"
27-
- "*CRE"
28-
- "*END"
29-
- "/EOF"
23+
# Non-available commands - PyMAPDL user guide
3024
- "*ASK"
31-
- "*IF"
25+
- "*VEDIT"
26+
- "/ERASE"
27+
- "ERASE"
28+
- "HELP"
29+
- "HELPDISP"
30+
- "NOERASE"
31+
- "*CYCLE"
32+
- "*DO"
33+
- "*DOWHILE"
3234
- "*ELSE"
33-
- "CMAT"
34-
- "*REP"
35+
- "*ELSEIF"
36+
- "*ENDDO"
37+
- "*GO"
38+
- "*IF"
39+
- "*REPEAT"
3540
- "*RETURN"
36-
- "LSRE"
41+
- "*DEL"
42+
- "/BATCH"
43+
- "/EOF"
44+
- "UNDO"
45+
# Additional commands to ignore
46+
- "C***"
47+
- "/DIRECTORY" # Defined in ``mapdl_core.py``
48+
- "*XPL" # Defined in ``mapdl_grpc.py``
49+
50+
warnings:
51+
"This command must be run using :func:`non_interactive <ansys.mapdl.core.Mapdl.non_interactive>`\nPlease visit `Unsupported Interactive Commands <https://mapdl.docs.pyansys.com/version/stable/user_guide/mapdl.html#unsupported-interactive-commands>`_\nfor further information.":
52+
- "*CREATE"
53+
- "CFOPEN"
54+
- "CFCLOSE"
55+
- "*VWRITE"
56+
- "*MWRITE"
57+
- "LSWRITE"
58+
- "LSREAD"
3759

3860

3961
specific_classes:

src/pyconverter/xml2py/ast_tree.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,12 +2898,35 @@ def py_signature(self, custom_functions: CustomFunctions, indent="") -> str:
28982898
arg_sig = ", ".join(args)
28992899
return f"{indent}def {self.py_name}({arg_sig}, **kwargs):"
29002900

2901-
def py_docstring(self, custom_functions: CustomFunctions) -> str:
2902-
"""Python docstring of the command."""
2901+
def py_docstring(self, custom_functions: CustomFunctions, warning_command_dict: None) -> str:
2902+
"""
2903+
Python docstring of the command.
2904+
2905+
Parameters
2906+
----------
2907+
2908+
custom_functions : CustomFunctions
2909+
Custom functions object.
2910+
2911+
warning_command_dict: dict, optional
2912+
Dictionary of commands associated to a list of warnings.
2913+
The default is ``None``.
2914+
2915+
2916+
"""
29032917
xml_cmd = f"{self._terms['pn006p']} Command: `{self.name} <{self.url}>`_"
29042918

2919+
# ``warning_command_dict`` need to be added here
2920+
29052921
items = [self.short_desc, "", xml_cmd]
29062922

2923+
if self.name in warning_command_dict.keys():
2924+
warnings_ = warning_command_dict[self.name]
2925+
for warning_ in warnings_:
2926+
warning_ = textwrap.indent(warning_, " " * 3)
2927+
items.extend([f"\n.. warning::\n{warning_}\n"])
2928+
print(items)
2929+
29072930
if self.default is not None:
29082931
if self.default.tag in item_needing_links_base_url:
29092932
items += [""] + textwrap.wrap(
@@ -3247,14 +3270,20 @@ def py_source(self, custom_functions=None, indent=""):
32473270
source = textwrap.indent("".join(custom_functions.py_code[self.py_name]), prefix=indent)
32483271
return source
32493272

3250-
def to_python(self, custom_functions=None, indent=""):
3273+
def to_python(self, custom_functions=None, warning_command_dict=None, indent=""):
32513274
"""
32523275
Return the complete Python definition of the command.
32533276
32543277
Parameters
32553278
----------
32563279
custom_functions: CustomFunctions, optional
3257-
Custom functions to add to the command. The default is ``None``.
3280+
Custom functions to add to the command.
3281+
The default is ``None``.
3282+
3283+
warning_command_dict: dict, optional
3284+
Dictionary of commands associated to a list of warnings.
3285+
The default is ``None``.
3286+
32583287
indent: str, optional
32593288
Indentation of the Python function. The default is ``""``.
32603289
@@ -3265,7 +3294,8 @@ def to_python(self, custom_functions=None, indent=""):
32653294
"""
32663295

32673296
docstr = textwrap.indent(
3268-
f'r"""{self.py_docstring(custom_functions)}\n"""', prefix=indent + " " * 4
3297+
f'r"""{self.py_docstring(custom_functions, warning_command_dict)}\n"""',
3298+
prefix=indent + " " * 4,
32693299
)
32703300
if custom_functions is not None and self.py_name in custom_functions.lib_import:
32713301
imports = "\n".join(custom_functions.lib_import[self.py_name])

src/pyconverter/xml2py/utils/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ def get_config_data_value(yaml_path: Path, value: str) -> Union[str, dict, list,
6464
return config_data.get(value)
6565

6666

67+
def get_warning_command_dict(yaml_path: Path) -> dict:
68+
"""
69+
Get the list of commands that will raise a warning.
70+
71+
Parameters
72+
----------
73+
yaml_path: Path
74+
Path object of the YAML file.
75+
"""
76+
warnings_ = get_config_data_value(yaml_path, "warnings")
77+
warning_command_dict = {}
78+
for warning_, command_list in warnings_.items():
79+
for command in command_list:
80+
try:
81+
warning_command_dict[command].append(warning_)
82+
except KeyError:
83+
warning_command_dict[command] = [warning_]
84+
85+
return warning_command_dict
86+
87+
6788
def create_name_map(meta_command: list[str], yaml_file_path: Path) -> dict:
6889
"""
6990
Create a mapping between the initial command name and the Python function name.

src/pyconverter/xml2py/writer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
create_name_map,
3636
get_config_data_value,
3737
get_refentry,
38+
get_warning_command_dict,
3839
import_handler,
3940
)
4041
import regex as re
@@ -430,6 +431,8 @@ def write_source(
430431

431432
library_path = Path(get_library_path(new_package_path, config_path))
432433

434+
warning_command_dict = get_warning_command_dict(config_path)
435+
433436
if not library_path.is_dir():
434437
library_path.mkdir(parents=True, exist_ok=True)
435438

@@ -440,7 +443,7 @@ def write_source(
440443
continue
441444
python_name = name_map[initial_command_name]
442445
path = library_path / f"{python_name}.py"
443-
python_method = command_obj.to_python(custom_functions)
446+
python_method = command_obj.to_python(custom_functions, warning_command_dict, indent="")
444447
try:
445448
exec(python_method)
446449
with open(path, "w", encoding="utf-8") as fid:
@@ -482,7 +485,9 @@ def write_source(
482485
class_structure.append(command.py_name)
483486

484487
package_structure[module_name][file_name] = [class_name, class_structure]
485-
python_method = command.to_python(custom_functions, indent=4 * " ")
488+
python_method = command.to_python(
489+
custom_functions, warning_command_dict, indent=4 * " "
490+
)
486491

487492
# Check if there are any imports to be added before the function definition.
488493
reg_before_def = pat.BEFORE_DEF + f"{command.py_name})"

0 commit comments

Comments
 (0)