Skip to content

Commit b7d1f39

Browse files
committed
feat: enabling different types of message to be added at the beginning of a docstring
1 parent f8bf5c5 commit b7d1f39

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

config.yaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ ignored_commands:
5050
- "/DIRECTORY" # Defined in ``mapdl_core.py``
5151
- "*XPL" # Defined in ``mapdl_grpc.py``
5252

53-
warnings:
54-
- msg: 'This command must be run using :func:`non_interactive <ansys.mapdl.core.Mapdl.non_interactive>
53+
beginning_comments:
54+
- msg: 'This command must be run using :func:`non_interactive <ansys.mapdl.core.Mapdl.non_interactive>`.
5555
56-
Please visit `Unsupported Interactive Commands <https://mapdl.docs.pyansys.com/version/stable/user_guide/mapdl.html#unsupported-interactive-commands>`_
56+
Please visit `Unsupported Interactive Commands <https://mapdl.docs.pyansys.com/version/stable/user_guide/mapdl.html#unsupported-interactive-commands>`_
5757
58-
for further information.'
58+
for further information.'
59+
type: "warning"
5960
commands:
6061
- "*CREATE"
6162
- "CFOPEN"
@@ -65,6 +66,14 @@ warnings:
6566
- "LSWRITE"
6667
- "LSREAD"
6768

69+
- msg: 'Starting with PyMAPDL v0.66.0, you can use "P" as a second argument to select entities interactively. A window pops up
70+
71+
allowing you to select, unselect, add or reselect entities depending on the first argument ``type_``. An array with
72+
73+
the ids of new selection is returned when the window is closed.'
74+
type: "note"
75+
commands:
76+
- "ASEL"
6877

6978
specific_classes:
7079
2D to 3D Analysis: Analysis 2D to 3D

src/pyconverter/xml2py/ast_tree.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,7 +3097,7 @@ def custom_notes(self, custom_functions: CustomFunctions = None):
30973097
lines = custom_functions.py_notes[self.py_name]
30983098
return lines
30993099

3100-
def py_docstring(self, custom_functions: CustomFunctions, warning_command_dict: None) -> str:
3100+
def py_docstring(self, custom_functions: CustomFunctions, comment_command_dict: dict=None) -> str:
31013101
"""
31023102
Python docstring of the command.
31033103
@@ -3107,21 +3107,20 @@ def py_docstring(self, custom_functions: CustomFunctions, warning_command_dict:
31073107
custom_functions : CustomFunctions
31083108
Custom functions object.
31093109
3110-
warning_command_dict: dict, optional
3111-
Dictionary of commands associated to a list of warnings.
3110+
comment_command_dict: dict, optional
3111+
Dictionary of commands associated to a list of comments with the
3112+
following format: ``{"command": [["message_type", "message"]}``.
31123113
The default is ``None``.
3113-
3114-
31153114
"""
31163115
xml_cmd = f"{self._terms['pn006p']} Command: `{self.name} <{self.url}>`_"
31173116

31183117
items = [self.short_desc, "", xml_cmd]
31193118

3120-
if self.name in warning_command_dict.keys():
3121-
warnings_ = warning_command_dict[self.name]
3122-
for warning_ in warnings_:
3123-
warning_ = textwrap.indent(warning_, " " * 4)
3124-
items.extend([f"\n.. warning::\n\n{warning_}\n"])
3119+
if self.name in comment_command_dict.keys():
3120+
comments_ = comment_command_dict[self.name]
3121+
for (comment_type, comment_) in comments_:
3122+
comment_ = textwrap.indent(comment_, " " * 4)
3123+
items.extend([f"\n.. {comment_type}::\n\n{comment_}\n"])
31253124

31263125
if self.default:
31273126
if self.default.tag in item_needing_links_base_url:
@@ -3472,7 +3471,7 @@ def py_source(self, custom_functions=None, indent=""):
34723471
def to_python(
34733472
self,
34743473
custom_functions=None,
3475-
warning_command_dict=None,
3474+
comment_command_dict=None,
34763475
indent="",
34773476
image_folder_path: Path = None,
34783477
):
@@ -3485,8 +3484,9 @@ def to_python(
34853484
Custom functions to add to the command.
34863485
The default is ``None``.
34873486
3488-
warning_command_dict: dict, optional
3489-
Dictionary of commands associated to a list of warnings.
3487+
comment_command_dict: dict, optional
3488+
Dictionary of commands associated to a list of comments with the
3489+
following format: ``{"command": [["message_type", "message"]}``.
34903490
The default is ``None``.
34913491
34923492
indent: str, optional
@@ -3503,7 +3503,7 @@ def to_python(
35033503
IMAGE_FOLDER_PATH = image_folder_path
35043504

35053505
docstr = textwrap.indent(
3506-
f'r"""{self.py_docstring(custom_functions, warning_command_dict)}\n"""',
3506+
f'r"""{self.py_docstring(custom_functions, comment_command_dict)}\n"""',
35073507
prefix=indent + " " * 4,
35083508
)
35093509
if custom_functions is not None and self.py_name in custom_functions.lib_import:

src/pyconverter/xml2py/utils/utils.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,42 @@ def get_config_data_value(yaml_path: Path, value: str) -> Union[str, dict, list,
6868
return config_data.get(value)
6969

7070

71-
def get_warning_command_dict(yaml_path: Path) -> dict:
71+
def get_comment_command_dict(yaml_path: Path) -> dict:
7272
"""
73-
Get the list of commands that will raise a warning.
73+
Get a dictionnary of messages to be added as warning, note, or info at the beginning of
74+
a command documentation.
7475
7576
Parameters
7677
----------
7778
yaml_path: Path
7879
Path object of the YAML file.
79-
"""
80-
warnings_ = get_config_data_value(yaml_path, "warnings")
81-
if warnings_ is None:
82-
logger.info("No warning commands found in the YAML file.")
83-
return {}
84-
warning_command_dict = {}
85-
for warning_ in warnings_:
86-
message = warning_["msg"]
87-
commands = warning_["commands"]
88-
for command in commands:
89-
try:
90-
warning_command_dict[command].append(message)
91-
except KeyError:
92-
warning_command_dict[command] = [message]
93-
94-
if warning_command_dict == {}:
95-
logger.info("No warning commands found in the YAML file.")
96-
97-
else:
98-
logger.info("Warning commands found in the YAML file.")
9980
100-
return warning_command_dict
81+
Returns
82+
-------
83+
dict
84+
Dictionary of comment to be added with the following format: ``{"command": [["message_type", "message"]}``.
85+
"""
86+
beginning_comments = get_config_data_value(yaml_path, "beginning_comments")
87+
if beginning_comments is None:
88+
logger.info("No comments to be added found in the YAML file.")
89+
comment_command_dict = {}
90+
if beginning_comments:
91+
for comment_ in beginning_comments:
92+
message = comment_["msg"]
93+
comment_type = comment_["type"]
94+
if comment_type not in ["note", "warning", "info"]:
95+
raise ValueError(f"Comment type '{comment_type}' not supported. Use 'note', 'warning', or 'info'.")
96+
commands = comment_["commands"]
97+
for command in commands:
98+
try:
99+
comment_command_dict[command].append([comment_type, message])
100+
except KeyError:
101+
comment_command_dict[command] = [[comment_type, message]]
102+
103+
if comment_command_dict == {}:
104+
logger.info("No message found in the YAML file.")
105+
106+
return comment_command_dict
101107

102108

103109
def create_name_map(meta_command: list[str], yaml_file_path: Path) -> dict:

src/pyconverter/xml2py/writer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
create_name_map,
3636
get_config_data_value,
3737
get_refentry,
38-
get_warning_command_dict,
38+
get_comment_command_dict,
3939
import_handler,
4040
)
4141
import regex as re
@@ -432,7 +432,7 @@ def write_source(
432432

433433
library_path = Path(get_library_path(new_package_path, config_path))
434434

435-
warning_command_dict = get_warning_command_dict(config_path)
435+
comment_command_dict = get_comment_command_dict(config_path)
436436

437437
if not library_path.is_dir():
438438
library_path.mkdir(parents=True, exist_ok=True)
@@ -444,7 +444,7 @@ def write_source(
444444
continue
445445
python_name = name_map[initial_command_name]
446446
path = library_path / f"{python_name}.py"
447-
python_method = command_obj.to_python(custom_functions, warning_command_dict, indent="")
447+
python_method = command_obj.to_python(custom_functions, comment_command_dict, indent="")
448448
try:
449449
exec(python_method)
450450
with open(path, "w", encoding="utf-8") as fid:
@@ -488,7 +488,7 @@ def write_source(
488488
package_structure[module_name][file_name] = [class_name, class_structure]
489489
python_method = command.to_python(
490490
custom_functions,
491-
warning_command_dict,
491+
comment_command_dict,
492492
indent=4 * " ",
493493
image_folder_path=image_folder_path,
494494
)

0 commit comments

Comments
 (0)