Skip to content

Commit

Permalink
add new xpath tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mokko committed Jun 20, 2024
1 parent 4256681 commit 9dd6b11
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lido = 'zml2lido:lido'
lvalidate = 'zml2lido:validate'
saxon = 'zml2lido:saxon'
vocmap = 'zml2lido:vocmap'

xpath = 'zml2lido:xpath'

[project.urls]
Home = "https://github.com/mokko/zml2lido"
Expand Down
32 changes: 32 additions & 0 deletions zml2lido/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from zml2lido.lidoTool import LidoTool
from zml2lido.linkChecker import LinkChecker
from zml2lido.vocmap import Vocmap
from zml2lido.xpathTool import xpathTool

NSMAP = {"l": "http://www.lido-schema.org"}

Expand Down Expand Up @@ -157,3 +158,34 @@ def vocmap():

args = parser.parse_args()
vm = Vocmap(Input=args.Input, output=args.output)


def xpath():
""" "
Little utility that applies xpath expressions to one or multiple files
"""
parser = argparse.ArgumentParser(description="apply xpath to file(s)")
parser.add_argument(
"-i",
"--Input",
help="input file or filemask (in case of globbing)",
required=True,
)

parser.add_argument(
"-f",
"--file",
help="output to file xpath.xml",
action="store_true",
default=False,
)

parser.add_argument(
"-x",
"--xpath",
help="the xpath expression",
required=True,
)

args = parser.parse_args()
xpathTool(Input=args.Input, xpath=args.xpath, file=args.file)
38 changes: 38 additions & 0 deletions zml2lido/xpathTool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from lxml import etree
from pathlib import Path

# NSMAP is already present in linkChecker TODO
NSMAP = {"l": "http://www.lido-schema.org"}


def xpathTool(
*, Input: str, xpath: str, file: bool = False, globbing: bool = False
) -> None:
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):
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)


#
# 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
xml = etree.tostring(resultN, pretty_print=True, encoding="unicode")
with open(out_fn, "a") as f:
f.write(xml)
else:
print(resultN) # not stringified

0 comments on commit 9dd6b11

Please sign in to comment.