Skip to content

Commit ec99972

Browse files
committed
fix: latest changes
1 parent c34cf9c commit ec99972

File tree

2 files changed

+36
-58
lines changed

2 files changed

+36
-58
lines changed

src/pyconverter/xml2py/ast_tree.py

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
# SOFTWARE.
2222

2323
import logging
24+
from pathlib import Path
2425
import textwrap
2526
from typing import List
2627
import warnings
27-
from pathlib import Path
2828

2929
from inflect import engine
3030
from lxml.etree import tostring
@@ -135,14 +135,14 @@ def get_quant_iter_pos(name: str) -> tuple:
135135

136136
def to_py_arg_name(name: str) -> str:
137137
"""Python-compatible term"""
138-
arg = str(name).lower().strip()
139-
if arg == "":
138+
initial_arg = str(name).lower().strip()
139+
arg = initial_arg
140+
if arg in ["--", "–", ""]:
140141
return arg
141142
elif arg.isdigit():
142143
return ""
143144
if arg[0].isdigit():
144145
p = engine()
145-
if arg[0].isdigit():
146146
if arg[1].isdigit():
147147
raise ValueError(f"The code needs to be expanded to handle numbers")
148148
elif arg[1:3] not in superlatif:
@@ -152,12 +152,6 @@ def to_py_arg_name(name: str) -> str:
152152
num_value = p.number_to_words(arg[:3])
153153
arg = f"{num_value}{arg[3:]}"
154154

155-
if "--" in arg or arg == "–":
156-
arg = arg.replace("--", "")
157-
arg = arg.replace("–", "")
158-
arg = arg.replace(" ", "")
159-
return arg
160-
161155
for key, value in PY_ARG_CLEANUP.items():
162156
arg = arg.replace(key, value)
163157
arg = arg.strip()
@@ -213,7 +207,7 @@ def str_types(types, join_str: str) -> str:
213207

214208
def to_py_signature(py_arg_name, types) -> str:
215209
"""Return the Python signature of the argument."""
216-
if "," not in py_arg_name and py_arg_name != "":
210+
if py_arg_name not in ["--", "–", ""]:
217211
kwarg = f'{py_arg_name}: {str_types(types, " | ")} = ""'
218212
else:
219213
kwarg = None
@@ -999,9 +993,9 @@ def py_term(self, links=None, base_url=None):
999993
return f"{arg}"
1000994

1001995
if self.term.tag in item_needing_links_base_url:
1002-
arg = self.term.to_rst(links=links, base_url=base_url).replace("--", "").strip()
996+
arg = self.term.to_rst(links=links, base_url=base_url).strip()
1003997
else:
1004-
arg = self.term.to_rst().replace("--", "").strip()
998+
arg = self.term.to_rst().strip()
1005999

10061000
# sanity check
10071001
if "blank" in arg.lower():
@@ -1605,6 +1599,7 @@ def raw_args(self):
16051599
cmd = cmd.replace("&fname2_arg;", self._terms["fname2_arg"])
16061600
cmd = cmd.replace("&pn006p;", self._terms["pn006p"])
16071601
cmd = cmd.replace("&ansysBrand;", self._terms["ansysBrand"])
1602+
cmd = cmd.replace("``", "")
16081603
split_args = cmd.split(",")[1:]
16091604
return split_args
16101605

@@ -1613,36 +1608,29 @@ def args(self):
16131608
"""Command arguments."""
16141609
args = []
16151610
for item in self.raw_args:
1616-
orig_arg = str(item).replace(",", "")
1617-
arg = orig_arg.lower().replace("--", "").replace("–", "").replace("-", "_").strip()
1618-
if arg == "":
1619-
continue
1620-
1621-
if arg == "class":
1622-
arg = "class_"
1623-
elif arg == "type":
1624-
arg = "type_"
1611+
arg = to_py_arg_name(str(item))
16251612

16261613
# simply check if we can use this as a valid Python kwarg
16271614
try:
16281615
exec(f"{arg} = 1.0")
16291616
except SyntaxError:
1630-
continue
1617+
arg = ""
16311618

16321619
if "blank" in arg:
1633-
continue
1620+
arg = ""
16341621

16351622
args.append(arg)
16361623

16371624
# rename duplicate arguments
16381625
if len(args) != len(set(args)):
16391626
for arg in args:
1640-
i = 0
1641-
if args.count(arg) > 1:
1642-
for j in range(len(args)):
1643-
if args[j] == arg:
1644-
args[j] = f"{arg}{i:d}"
1645-
i += 1
1627+
if arg not in ["", "--", "–"]:
1628+
i = 0
1629+
if args.count(arg) > 1:
1630+
for j in range(len(args)):
1631+
if args[j] == arg:
1632+
args[j] = f"{arg}{i:d}"
1633+
i += 1
16461634

16471635
return args
16481636

@@ -2130,7 +2118,7 @@ def _parse_list_entry(self):
21302118
if len(additional_args) > 0:
21312119
for arg in additional_args:
21322120
arg_name = arg.py_arg_name
2133-
if ("," in arg_name or arg_name == "") or (arg_name not in self.py_arg_names):
2121+
if (arg_name in self._initial_args) and (arg_name not in self.py_arg_names):
21342122
self._arguments.append(arg)
21352123

21362124
else:
@@ -2139,7 +2127,7 @@ def _parse_list_entry(self):
21392127
def __iadd__(self, argument_list):
21402128
for arg in argument_list.arguments:
21412129
arg_name = arg.py_arg_name
2142-
if ("," in arg_name or arg_name == "") or (arg_name not in self.py_arg_names):
2130+
if (arg_name in self._initial_args) and (arg_name not in self.py_arg_names):
21432131
self._arguments.append(arg)
21442132
return self
21452133

@@ -2150,7 +2138,7 @@ def arguments(self):
21502138
@arguments.setter
21512139
def arguments(self, argument):
21522140
self._arguments.append(argument)
2153-
2141+
21542142
@property
21552143
def py_name(self):
21562144
return self._py_name
@@ -2216,10 +2204,9 @@ def multiple_args(self):
22162204
if not self.is_arg_elipsis:
22172205
for item_name in split_name:
22182206
arg_name = item_name.strip()
2219-
if arg_name not in ["--", ""]:
2220-
new_arg = Argument(arg_name, self._initial_argument, self._description)
2221-
if new_arg.py_arg_name != "":
2222-
additional_args.append(new_arg)
2207+
new_arg = Argument(arg_name, self._initial_argument, self._description)
2208+
if new_arg.py_arg_name != "":
2209+
additional_args.append(new_arg)
22232210
else:
22242211

22252212
complete_args = get_complete_args_from_initial_arg(
@@ -2304,19 +2291,6 @@ def multiple_args(self):
23042291

23052292
return additional_args
23062293

2307-
def rec_find(self, _type: str, terms=None) -> Element | None:
2308-
"""Find the first type matching a given type string recursively."""
2309-
for item in self:
2310-
if type(item).__name__ == _type:
2311-
if _type == "Refname" or _type == "Refnamediv":
2312-
item.terms = terms
2313-
return item
2314-
if isinstance(item, Element):
2315-
subitem = item.rec_find(_type)
2316-
if subitem is not None:
2317-
return subitem
2318-
return None
2319-
23202294
@property
23212295
def types(self) -> List[type]:
23222296
"""One or more parameter types.
@@ -2466,24 +2440,26 @@ def arg_desc(self) -> List[Argument]:
24662440
arguments = ArgumentList(self.py_name, elem, self.args)
24672441
else:
24682442
arguments += ArgumentList(self.py_name, elem, self.args)
2469-
2443+
2444+
arg_file = Path("args.txt")
2445+
24702446
if arguments is not None:
24712447
if len(arguments.py_arg_names) != len(arguments.initial_args):
24722448
# This function needs a special treatment
2473-
if Path("args.txt").exists():
2474-
with open("args.txt", "r") as f:
2449+
if arg_file.exists():
2450+
with open(arg_file, "r") as f:
24752451
for line in f:
24762452
pass
24772453
last_line = line
24782454
else:
24792455
last_line = ""
2480-
with open("args.txt", "a") as f:
2456+
with open(arg_file, "a") as f:
24812457
if last_line != f"{arguments.py_arg_names}\n":
24822458
f.write("--------------------------------------------------\n")
24832459
f.write(f"{self.py_name}: {self.group}\n")
24842460
f.write(f"{arguments.initial_args}\n")
24852461
f.write(f"{arguments.py_arg_names}\n")
2486-
2462+
24872463
# for arg in arguments.initial_args:
24882464
# if arg not in arguments.py_arg_names:
24892465
# new_arg = Argument(arg, arguments.initial_args, "")
@@ -2930,9 +2906,7 @@ def py_source(self, custom_functions=None, indent=""):
29302906
command = 'command = f"' + self.name
29312907
for arg in self.arg_desc:
29322908
name = arg.py_arg_name
2933-
if "," in name:
2934-
command += f",{name}"
2935-
elif name == "":
2909+
if name in ["--", "–", ""]:
29362910
command += ","
29372911
else:
29382912
command += ",{"

src/pyconverter/xml2py/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def create_package(
105105
download.download_template()
106106

107107
command_map, name_map = wr.convert(xml_path)
108+
arg_file = Path("args.txt")
109+
if arg_file.exists():
110+
# Delete the file if it exists
111+
arg_file.unlink()
108112
package_structure = wr.write_source(
109113
command_map, name_map, xml_path, target_path, custom_functions_path
110114
)

0 commit comments

Comments
 (0)