forked from machinalis/refo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_reader.py
39 lines (30 loc) · 1.09 KB
/
xml_reader.py
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
35
36
37
38
39
#!/usr/bin/python
# This script opens an xml file and prints its document tree... sort of.
# This example uses refo syntax to simple express an otherwise more-or-less
# complicated regular expression.
import argparse
parser = argparse.ArgumentParser(description="Prints an xml document tree" +
"... sort of.")
parser.add_argument("filename", action="store")
cfg = parser.parse_args()
text = open(cfg.filename).read()
from refo import finditer, Predicate, Literal, Any, Group, Star
def notin(xs):
return lambda x: x not in xs
name = Predicate(notin("/")) + Star(Predicate(notin(" >")))
name = Group(name, "name")
inside = name + Star(Any(), greedy=False)
opentag = Literal("<") + inside + Literal(">")
opentag = Group(opentag, "open")
closetag = Literal("<") + Literal("/") + inside + Literal(">")
closetag = Group(closetag, "close")
regex = closetag | opentag
depth = 0
for m in finditer(regex, text):
if "open" in m:
i, j = m["name"]
print " " * depth + text[i:j]
depth += 1
else:
assert "close" in m
depth -= 1