Skip to content

Commit f77b505

Browse files
committed
feat: adding latest enhancements
1 parent 2e78c01 commit f77b505

File tree

1 file changed

+100
-59
lines changed

1 file changed

+100
-59
lines changed

src/pyconverter/xml2py/ast_tree.py

Lines changed: 100 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,13 @@ def to_rst(self, indent="", max_length=100, links=None, base_url=None, fcache=No
736736
rst_item = item.to_rst(indent=indent)
737737
else:
738738
rst_item = str(item)
739-
740-
rst_item = resize_list_text(rst_item, max_length)
739+
740+
if type(item) != str and len(item.children)>1 and type(item[1]) != str:
741+
intersection_types = set(NO_RESIZE_LIST).intersection(set(item[1].children_types))
742+
if len(intersection_types)==0:
743+
rst_item = resize_list_text(rst_item, max_length)
744+
else:
745+
rst_item = resize_list_text(rst_item, max_length)
741746
active_items.append(rst_item)
742747

743748
return "\n".join(active_items) + "\n"
@@ -919,7 +924,7 @@ def to_rst(self, indent="", max_length=100, links=None, base_url=None, fcache=No
919924
class Term(Element):
920925
"""Provides the term element."""
921926

922-
def to_rst(self, indent="", links=None, base_url=None, fcache=None):
927+
def to_rst(self, indent="", max_length=100, links=None, base_url=None, fcache=None):
923928
"""Return a string to enable converting the element to an RST format."""
924929

925930
items = []
@@ -1946,14 +1951,57 @@ class ProductName(Element):
19461951
# pass
19471952
# # print(resize_length(self._description.to_rst(), max_length))
19481953

1954+
class ArgumentList:
1955+
1956+
def __init__(self, list_entry: VarlistEntry) -> None:
1957+
1958+
self._list_entry = list_entry
1959+
self._arguments = []
1960+
self._parse_list_entry()
1961+
1962+
def _parse_list_entry(self):
1963+
for item in self._list_entry:
1964+
if isinstance(item, VarlistEntry):
1965+
if len(Argument(item).multiple_args)>0:
1966+
print("IT'S TRUE : ")
1967+
print(Argument(item).multiple_args)
1968+
for arg in Argument(item).multiple_args:
1969+
self._arguments.append(arg)
1970+
else:
1971+
self._arguments.append(Argument(item))
1972+
1973+
@property
1974+
def arguments(self):
1975+
return self._arguments
1976+
1977+
@arguments.setter
1978+
def arguments(self, argument):
1979+
self._arguments.append(argument)
19491980

19501981
class Argument:
19511982
"""Argument object."""
19521983

1953-
def __init__(self, element):
1954-
self._name = element[0]
1955-
self._description = element[1]
1956-
1984+
def __init__(self, element:str | Element, description:Element | None=None) -> None:
1985+
if description is None:
1986+
name = element[0]
1987+
description = element[1]
1988+
else:
1989+
name = element
1990+
self._name = name
1991+
self._description = description
1992+
1993+
@property
1994+
def multiple_args(self):
1995+
additional_args = []
1996+
if "," in str(self._name):
1997+
for item_name in str(self._name).split(","):
1998+
print(item_name)
1999+
if item_name.strip() == "":
2000+
continue
2001+
arg_name = item_name.strip()
2002+
additional_args.append(Argument(arg_name, self._description))
2003+
return additional_args
2004+
19572005
def rec_find(self, _type: str, terms=None) -> Element | None:
19582006
"""Find the first type matching a given type string recursively."""
19592007
for item in self:
@@ -1985,9 +2033,7 @@ def types(self) -> List[type]:
19852033
terms = varlist.terms
19862034
if terms:
19872035
terms_numeric = [is_numeric(term) for term in terms]
1988-
if all(terms_numeric):
1989-
parm_types = [int]
1990-
elif any(terms_numeric):
2036+
if any(terms_numeric):
19912037
parm_types = [int, str]
19922038
else:
19932039
parm_types = [str]
@@ -2009,6 +2055,15 @@ def py_arg_name(self) -> str:
20092055

20102056
if arg == "type":
20112057
arg = "type_"
2058+
2059+
elif "," in arg:
2060+
2061+
for item_name in arg.split(","):
2062+
if item_name.strip() == "":
2063+
continue
2064+
arg_name = item_name.strip()
2065+
Argument(arg_name, self._description)
2066+
20122067

20132068
return f"{arg}"
20142069

@@ -2019,11 +2074,11 @@ def resized_description(self, description: str|None=None, max_length: int =100,
20192074
description = self._description
20202075
return resize_length(description, max_length, initial_indent=indent, subsequent_indent=indent, list=True)
20212076

2022-
def to_py_docstring(self, max_length=100, indent="", links=None, base_url=None) -> List[str]:
2077+
def to_py_docstring(self, max_length=100, indent="", links=None, base_url=None, fcache=None) -> List[str]:
20232078
"""Return a list of string to enable converting the element to an RST format."""
20242079

20252080
docstring = [f"{indent}{self.py_arg_name} : {self.str_types(" or ")}"]
2026-
rst_description = self._description.to_rst(indent=indent, max_length = max_length, links=links, base_url=base_url)
2081+
rst_description = self._description.to_rst(indent=indent, max_length = max_length, links=links, base_url=base_url, fcache=fcache)
20272082
if not "* " in rst_description:
20282083
list_description = self.resized_description(rst_description, max_length, indent)
20292084
else:
@@ -2034,8 +2089,12 @@ def to_py_docstring(self, max_length=100, indent="", links=None, base_url=None)
20342089
return docstring
20352090

20362091
def to_py_signature(self) -> str:
2037-
"""Return the Python signature of the argument."""
2038-
return f"{self.py_arg_name}: {self.str_types(" | ")}"
2092+
"""Return the Python signature of the argument."""
2093+
if self.py_arg_name != "--" and self.py_arg_name != "–":
2094+
kwarg = f'{self.py_arg_name}: {self.str_types(" | ")}=""'
2095+
else:
2096+
kwarg = None
2097+
return kwarg
20392098

20402099

20412100
class XMLCommand(Element):
@@ -2089,39 +2148,23 @@ def default(self):
20892148
def arg_desc(self) -> List[Argument]:
20902149
"""Argument object list of the command."""
20912150
refsyn = self.rec_find("Refsynopsisdiv")
2092-
arguments = None
20932151
# search by ID
2152+
arguments = []
20942153
if refsyn is None:
2095-
arguments = []
20962154
refsections = self.find_all("RefSection")
20972155
for elem in refsections:
20982156
if (
20992157
elem.id is not None and "argdescript" in elem.id
21002158
):
2101-
for child in elem[1]:
2102-
arguments.append(Argument(child))
2103-
return arguments
2159+
for child in elem:
2160+
if isinstance(child, Variablelist):
2161+
arguments = ArgumentList(child).arguments
2162+
continue
21042163
else:
2105-
# print("NUMBER OF CHILDREN : ", len(refsyn.children))
2106-
# for child in refsyn.children:
2107-
# print("| TYPE : ", type(child))
2108-
# print("| CHILD : ", child)
2109-
# print("| NUMBER OF GRANDCHILDREN : ", len(child.children))
2110-
# for gc in child.children:
2111-
# print("|| TYPE : ", type(gc))
2112-
# print("|| CHILD : ", gc)
2113-
# print("|| NUMBER OF GREAT GRANDCHILDREN : ", len(gc.children))
2114-
# for ggc in gc.children:
2115-
# print("||| TYPE : ", type(ggc))
2116-
# print("||| CHILD : ", ggc)
2117-
# print("||| NUMBER OF GREAT GREAT GRANDCHILDREN : ", len(ggc.children))
2118-
# for gggc in ggc.children:
2119-
# print("|||| TYPE : ", type(gggc))
2120-
# print("|||| CHILD : ", gggc)
2121-
# print("|||| NUMBER OF GREAT GREAT GREAT GRANDCHILDREN : ", len(gggc.children))
2122-
2123-
2124-
arguments = [refsyn]
2164+
for elem in refsyn:
2165+
if isinstance(elem, Variablelist):
2166+
arguments = ArgumentList(elem).arguments
2167+
continue
21252168
return arguments
21262169

21272170
@property
@@ -2173,11 +2216,15 @@ def group(self, group):
21732216
"""Set the group of the command."""
21742217
self._group = group
21752218

2176-
def py_signature(self, indent=""):
2219+
def py_signature(self, indent="") -> str:
21772220
"""Beginning of the Python command's definition."""
21782221
args = ["self"]
2179-
kwargs = [f'{arg}=""' for arg in self.py_args if "--" not in arg]
2180-
arg_sig = ", ".join(args + kwargs)
2222+
if len(self.arg_desc) > 0:
2223+
for argument in self.arg_desc:
2224+
if argument.to_py_signature() is not None:
2225+
args.append(argument.to_py_signature())
2226+
2227+
arg_sig = ", ".join(args)
21812228
return f"{indent}def {self.py_name}({arg_sig}, **kwargs):"
21822229

21832230
def py_docstring(self, custom_functions, max_length=100):
@@ -2201,7 +2248,7 @@ def py_docstring(self, custom_functions, max_length=100):
22012248
):
22022249
items += [""] + custom_functions.py_returns[self.py_name]
22032250
if self.notes is not None:
2204-
items += [""] + self.py_notes
2251+
items += [""] + self.py_notes(max_length)
22052252
if custom_functions is not None and (
22062253
self.py_name in custom_functions.py_names
22072254
and self.py_name in custom_functions.py_examples
@@ -2416,25 +2463,24 @@ def term_replacer(match):
24162463

24172464
return docstr
24182465

2419-
@property
2420-
def py_notes(self):
2466+
def py_notes(self, max_length=100):
24212467
"""Python-formatted notes string."""
24222468
lines = ["Notes", "-" * 5]
24232469
if self.notes.tag in item_needing_all:
2424-
lines.append(
2425-
self.notes.to_rst(
2470+
notes = self.notes.to_rst(
24262471
links=self._links,
24272472
base_url=self._base_url,
24282473
fcache=self._fcache,
2429-
)
2430-
)
2474+
)
24312475
elif self.notes.tag in item_needing_links_base_url:
2432-
lines.append(self.notes.to_rst(links=self._links, base_url=self._base_url))
2476+
notes = self.notes.to_rst(links=self._links, base_url=self._base_url)
24332477
elif self.notes.tag in item_needing_fcache:
2434-
lines.append(self.notes.to_rst(links=self._links, fcache=self._fcache))
2478+
notes = self.notes.to_rst(links=self._links, fcache=self._fcache)
24352479
else:
2436-
lines.append(self.notes.to_rst())
2480+
notes = self.notes.to_rst()
24372481

2482+
notes = resize_length(notes, 100, list=True)
2483+
lines.extend(notes)
24382484
return lines
24392485

24402486
@property
@@ -2481,17 +2527,12 @@ def __repr__(self):
24812527
def py_parm(self, max_length=100, indent="", links=None, base_url=None, fcache=None):
24822528
"""Python parameter's string."""
24832529
lines = []
2484-
if self.arg_desc is not None:
2530+
if len(self.arg_desc) > 0:
24852531
lines.append("Parameters")
24862532
lines.append("-" * 10)
2487-
print("COMMAND NAME : ", self.name)
24882533
for argument in self.arg_desc:
2489-
if isinstance(argument, Argument):
2490-
lines.extend(argument.to_py_docstring(max_length, indent, links, base_url))
2491-
elif isinstance(argument, Refsynopsisdiv):
2492-
lines.extend(argument.to_rst(max_length=max_length, links=links, base_url=base_url, fcache=fcache).split("\n"))
2534+
lines.extend(argument.to_py_docstring(max_length, indent, links, base_url, fcache))
24932535
lines.append("")
2494-
print(lines)
24952536
return lines
24962537

24972538
# def py_parm(self, max_length=100):

0 commit comments

Comments
 (0)