Skip to content

Commit

Permalink
refactor new xpath tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mokko committed Jun 20, 2024
1 parent 9dd6b11 commit fe3c2cc
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions zml2lido/xpathTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,31 @@ def xpathTool(
print(f"{Input=}")
print(f"{xpath=}")
print(f"{file=} (stringify implicit with file option)")
out_fn = Path("xpath.xml")
if file:
if out_fn.exists():
out_fn.unlink()
for xml_fn in Path(".").glob(Input):
hit_count = 0
for file_count, xml_fn in enumerate(Path(".").glob(Input), start=1):
print(f"parsing {xml_fn}")
tree = etree.parse(xml_fn)
resL = tree.xpath(xpath, namespaces=NSMAP)
_output(file=file, results=resL, out_fn=out_fn)
hit_count += len(resL)
_output(file=file, results=resL)
print(f"Total {hit_count} hits in {file_count} files.")


#
# somewhat restricted
#


def _output(*, file: bool, results: list, out_fn: Path) -> None:
# delete file first then append
for resultN in results:
if file: # stringify automatically
def _output(*, file: bool, results: list) -> None:
out_fn = Path("xpath.xml")
for idx, resultN in enumerate(results):
if file: # if output to file then stringify
xml = etree.tostring(resultN, pretty_print=True, encoding="unicode")
with open(out_fn, "a") as f:
f.write(xml)
if idx == 0:
with open(out_fn, "w") as f:
f.write(xml)
else:
with open(out_fn, "a") as f:
f.write(xml)
else:
print(resultN) # not stringified

0 comments on commit fe3c2cc

Please sign in to comment.