-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpath.py
More file actions
34 lines (30 loc) · 900 Bytes
/
xpath.py
File metadata and controls
34 lines (30 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import argparse, sys
from dharma import common, tree, languages
# TODO
# in xpath maybe support yielding strings (only need this for the last component,
# for use in the command-line tool)
# allow selection of attrs in xpath, useful for doing searches from the cmd line
@common.transaction("texts")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("expression")
parser.add_argument("file", nargs="*")
args = parser.parse_args()
f = tree.generator.compile(args.expression)
if not args.file:
sys.stdout.write(f.source_code)
return
for file in args.file:
try:
t = tree.parse(file)
except tree.Error:
continue
for result in f(t):
print(f"{tree.term_color('#9d40b4')}>>> {file}: {result.path}{tree.term_color()}")
print(result.xml())
sys.stdout.flush()
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, BrokenPipeError):
exit(1)