-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpgterms2yaml.py
More file actions
executable file
·129 lines (88 loc) · 3.06 KB
/
Copy pathpgterms2yaml.py
File metadata and controls
executable file
·129 lines (88 loc) · 3.06 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
#
# pgterms2yaml
#
# Copyright 2015 Gluejar, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import xml.sax
import xml.sax.handler
class Handler(xml.sax.handler.ContentHandler):
"""Quick XML ContentHandler"""
def __init__(self):
self.path = []
self.pathStr = ""
self.parentPathStr = ""
self.result = dict()
def startDocument(self):
print '-----'
def endDocument(self):
print '-----'
def startElement(self, name, attrs):
self.parentPathStr = "/" + "/".join(self.path)
self.path.append(name)
self.pathStr = "/" + "/".join(self.path)
self.stats.startElement(self.pathStr, self.parentPathStr)
def endElement(self, name):
self.stats.endElement(self.pathStr)
last = self.path.pop()
if last != name:
raise AssertionError("Closing and opening tags do not match.")
self.pathStr = "/" + "/".join(self.path)
def characters(self, content):
def printResults(self):
print "Results:"
print ""
print "{0}\t{1}\t{2}\t{3}".format(
"path",
"found total",
"min(found in parent)",
"max(found in parent)")
keys = self.stats.stats.keys()
keys.sort()
for p in keys:
el = self.stats.stats[p]
print "{0}\t{1}\t{2}\t{3}".format(
p,
el.times_total,
el.times_min,
el.times_max)
# Some debugging:
#for p in keys:
# print(p)
# self.stats.stats[p].printMe()
def main(argv):
# Create XmlReader:
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_validation, False)
parser.setFeature(xml.sax.handler.feature_external_ges, False)
parser.setFeature(xml.sax.handler.feature_external_pes, False)
# Create an assign a handler to the parser:
handler = Handler()
parser.setContentHandler(handler)
# process all arguments as filenames:
for f in argv:
print "Processing {0}...".format(f)
parser.parse(f)
print ""
handler.printResults()
def usage():
print "Usage: $ python pgterms2yaml.py FILE [FILE] [FILE] ..."
if __name__ == "__main__":
if len(sys.argv) > 1:
main(sys.argv[1:])
else:
usage()