Skip to content

Commit

Permalink
fix: most of indentation errors (except for fname)
Browse files Browse the repository at this point in the history
  • Loading branch information
clatapie committed Dec 20, 2024
1 parent 75b3373 commit 2778bc0
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions src/pyconverter/xml2py/ast_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,33 @@ def resize_length(text, max_length=100, initial_indent="", subsequent_indent="",
Resized text.
"""
text = text.replace(" .", ".")
while "\n\n\n" in text:
text = text.replace("\n\n\n", "\n\n")

wrapper = textwrap.TextWrapper(
width=max_length,
break_long_words=False,
initial_indent=initial_indent,
subsequent_indent=subsequent_indent,
)
if list is False:
return wrapper.fill(text=text)

if "\n\n" in text:
text = text.split("\n\n")
else:
text = [text]

for i, paragraph in enumerate(text):
text[i] = wrapper.fill(text=paragraph)

if len(text) > 1:
output = "\n\n".join(text)
else:
return wrapper.wrap(text=text)
output = text[0]

if list is True:
output = output.splitlines()

return output


# ############################################################################
Expand Down Expand Up @@ -669,7 +686,6 @@ class OrderedList(Element):

def to_rst(self, indent="", max_length=100, links=None, base_url=None):
"""Return a string to enable converting the element to an RST format."""
# indent += " " * 4
ordered_list = []
for item in self:
if item.tag in item_needing_links_base_url:
Expand Down Expand Up @@ -835,7 +851,7 @@ def to_rst(self, indent="", max_length=100, links=None, base_url=None, fcache=No
)
items.append(str_item)

rst_item = " ".join(items) + "\n"
rst_item = " ".join(items) + "\n\n"

return rst_item

Expand Down Expand Up @@ -876,7 +892,7 @@ def to_rst(self, indent="", max_length=100, links=None, base_url=None):
content = f"{self[0]} "
elif self.role == "italic":
# TODO: this isn't the correct way of making text itallic
content = f"`{self[0]}` "
content = f"{self[0]} "
# elif self.role == 'var':
# content = f"``{self[0]}`` "
else:
Expand Down Expand Up @@ -999,11 +1015,12 @@ def to_rst(self, indent="", max_length=100):
return rst_item


def resize_element_list(text, max_length=100):
def resize_element_list(text, max_length=100, indent=""):
element_list = re.finditer(r"^\* ", text)
subsequent_indent = " " * 2
initial_indent = indent + " "
subsequent_indent = indent + " " * 2
element_list = resize_length(
text, max_length, initial_indent="", subsequent_indent=subsequent_indent
text, max_length, initial_indent=initial_indent, subsequent_indent=subsequent_indent
)
return element_list

Expand Down Expand Up @@ -1040,10 +1057,10 @@ def to_rst(self, indent="", max_length=100, links=None, base_url=None, fcache=No
if type(item) != str and len(item.children) > 1 and type(item[1]) != str:
intersection_types = set(NO_RESIZE_LIST).intersection(set(item[1].children_types))
if len(intersection_types) == 0:
rst_item = resize_element_list(rst_item, max_length)
rst_item = resize_element_list(rst_item, max_length, indent=indent)

else:
rst_item = resize_element_list(rst_item, max_length)
rst_item = resize_element_list(rst_item, max_length, indent=indent)
active_items.append(rst_item)

return "\n".join(active_items) + "\n"
Expand Down Expand Up @@ -1740,11 +1757,9 @@ def terms(self, terms):
def raw_args(self):
"""Raws containing the command arguments."""
cmd = str(self)
cmd = cmd.replace("&fname_arg;", self._terms["fname_arg"])
cmd = cmd.replace("&fname1_arg;", self._terms["fname1_arg"])
cmd = cmd.replace("&fname2_arg;", self._terms["fname2_arg"])
cmd = cmd.replace("&pn006p;", self._terms["pn006p"])
cmd = cmd.replace("&ansysBrand;", self._terms["ansysBrand"])
for term in self._terms.keys():
if type(self._terms[term]) == str:
cmd = cmd.replace(f"&{term};", self._terms[term])
cmd = cmd.replace("``", "")
split_args = cmd.split(",")[1:]
return split_args
Expand Down Expand Up @@ -2419,32 +2434,26 @@ def resized_description(
if description is None:
description = self._description

if "* " in description:
output = description.split("\n")
else:
output = resize_length(
description, max_length, initial_indent=indent, subsequent_indent=indent, list=True
)
output = resize_length(
description, max_length, initial_indent=indent, subsequent_indent=indent, list=True
)

return output

def to_py_docstring(
self, max_length=100, indent="", links=None, base_url=None, fcache=None
) -> List[str]:
def to_py_docstring(self, max_length=100, links=None, base_url=None, fcache=None) -> List[str]:
"""Return a list of string to enable converting the element to an RST format."""
if self.py_arg_name != "":
docstring = [f'{indent}{self.py_arg_name} : {str_types(self.types, " or ")}']
docstring = [f'{self.py_arg_name} : {str_types(self.types, " or ")}']
if isinstance(self._description, str):
rst_description = self._description
else:
rst_description = self._description.to_rst(
indent=indent,
max_length=max_length,
links=links,
base_url=base_url,
fcache=fcache,
)
description_indent = " " * 4 + indent
description_indent = " " * 4
if not "* " in rst_description:
list_description = self.resized_description(
rst_description, max_length, description_indent
Expand All @@ -2453,11 +2462,12 @@ def to_py_docstring(
rst_description = textwrap.indent(rst_description, description_indent)
list_description = rst_description.split("\n")

docstring = [f'{indent}{self.py_arg_name} : {str_types(self.types, " or ")}']
docstring = [f'{self.py_arg_name} : {str_types(self.types, " or ")}']
docstring.extend(list_description)

else:
docstring = []

return docstring


Expand Down Expand Up @@ -2787,7 +2797,8 @@ def py_docstring(self, custom_functions: CustomFunctions) -> str:
):
items += [""] + custom_functions.py_returns[self.py_name]
if self.notes is not None:
items += [""] + self.py_notes(custom_functions)
items += [""]
items.extend(self.py_notes(custom_functions))
if custom_functions is not None and (
self.py_name in custom_functions.py_names
and self.py_name in custom_functions.py_examples
Expand Down Expand Up @@ -3024,7 +3035,7 @@ def py_notes(self, custom_functions: CustomFunctions = None):
else:
notes = self.notes.to_rst()

if "flat-table" not in "".join(notes) and ".. code::" not in "".join(notes):
if "flat-table" not in notes and ".. code::" not in notes:
notes = resize_length(notes, self._max_length, list=True)
lines.extend(notes)
else:
Expand Down Expand Up @@ -3079,7 +3090,7 @@ def __repr__(self):

return "\n".join(lines)

def py_parm(self, custom_functions=None, indent="", links=None, base_url=None, fcache=None):
def py_parm(self, custom_functions=None, links=None, base_url=None, fcache=None):
"""Python parameter's string."""
lines = []
arg_desc = self.arg_desc
Expand All @@ -3096,9 +3107,7 @@ def py_parm(self, custom_functions=None, indent="", links=None, base_url=None, f
lines.append("-" * 10)
for argument in arg_desc:
lines.extend(
argument.to_py_docstring(
self._max_length, indent, links, base_url, fcache
)
argument.to_py_docstring(self._max_length, links, base_url, fcache)
)
lines.append("")
else:
Expand All @@ -3107,9 +3116,7 @@ def py_parm(self, custom_functions=None, indent="", links=None, base_url=None, f
elif len(arg_desc) > 0:
lines.append("-" * 10)
for argument in arg_desc:
lines.extend(
argument.to_py_docstring(self._max_length, indent, links, base_url, fcache)
)
lines.extend(argument.to_py_docstring(self._max_length, links, base_url, fcache))
lines.append("")
return lines

Expand Down

0 comments on commit 2778bc0

Please sign in to comment.