Skip to content

Commit 271e6ff

Browse files
chg: simplify node() func
- move "toParseString" into func signature instead of iterating through kwargs to find it for special handling - similarly set "tag" via the first named arg rather than slicing args - ideally the same sort of thing would be done for the "unicode_args" by adding a "text: str" kwarg, but that would require a larger refactor since the text arg isn't necessarily always the second arg.
1 parent 15ff687 commit 271e6ff

1 file changed

Lines changed: 19 additions & 28 deletions

File tree

pyxform/utils.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,44 +96,35 @@ def writexml(self, writer, indent="", addindent="", newl=""):
9696
writer.write(data)
9797

9898

99-
def node(*args, **kwargs) -> DetachableElement:
99+
def node(tag: str, *args, toParseString: bool = False, **kwargs) -> DetachableElement:
100100
"""
101-
args[0] -- a XML tag
102-
args[1:] -- an array of children to append to the newly created node
103-
or if a unicode arg is supplied it will be used to make a text node
104-
kwargs -- attributes
105-
returns a xml.dom.minidom.Element
101+
Create an Element, with attached child elements (args) and attributes (kwargs).
102+
103+
:param tag: The Element XML tag name.
104+
:param toParseString: If True, parse the first text arg as XML and add it to the tag.
106105
"""
107-
blocked_attributes = {"tag"}
108-
tag = args[0] if len(args) > 0 else kwargs["tag"]
109-
args = args[1:]
110106
result = DetachableElement(tag)
111107
unicode_args = tuple(u for u in args if isinstance(u, str))
112108
if len(unicode_args) > 1:
113109
raise PyXFormError("""Invalid value for `unicode_args`.""")
114-
parsed_string = False
110+
elif len(unicode_args) == 1:
111+
if toParseString:
112+
# Add this header string so parseString can be used?
113+
s = f"""<?xml version="1.0" ?><{tag}>{unicode_args[0]}</{tag}>"""
114+
parsed_node = parseString(s.encode("utf-8")).documentElement
115+
# Move node's children to the result Element
116+
# discarding node's root
117+
for child in parsed_node.childNodes:
118+
result.appendChild(child.cloneNode(deep=False))
119+
else:
120+
text_node = PatchedText()
121+
text_node.data = unicode_args[0]
122+
result.appendChild(text_node)
115123

116124
# Convert the kwargs xml attribute dictionary to a xml.dom.minidom.Element.
117125
for k, v in kwargs.items():
118-
if k in blocked_attributes:
119-
continue
120-
if k == "toParseString":
121-
if v is True and len(unicode_args) == 1:
122-
parsed_string = True
123-
# Add this header string so parseString can be used?
124-
s = f"""<?xml version="1.0" ?><{tag}>{unicode_args[0]}</{tag}>"""
125-
parsed_node = parseString(s.encode("utf-8")).documentElement
126-
# Move node's children to the result Element
127-
# discarding node's root
128-
for child in parsed_node.childNodes:
129-
result.appendChild(child.cloneNode(deep=False))
130-
else:
131-
result.setAttribute(k, v)
126+
result.setAttribute(k, v)
132127

133-
if len(unicode_args) == 1 and not parsed_string:
134-
text_node = PatchedText()
135-
text_node.data = unicode_args[0]
136-
result.appendChild(text_node)
137128
for n in args:
138129
if isinstance(n, int | float | bytes):
139130
text_node = PatchedText()

0 commit comments

Comments
 (0)