-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdocument_object.py
96 lines (70 loc) · 2.33 KB
/
document_object.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
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
import sys
from xml.sax import make_parser, handler
class categoryHandler(handler.ContentHandler):
def __init__(self):
self.document = None
self.in_importants = False
def startElement(self, name, attrs):
if name == "document":
self.document = Document(attrs)
if name == "category":
self.document.categories.append(Category(attrs))
elif name == "overviews":
category = self.document.categories[-1]
assert category.overviewItems is None, "category %r already has overviews" % (
category,)
category.overviewItems = OverviewItems(attrs)
elif name == "item":
item = Item(attrs)
if self.in_importants:
self.document.important.append(item)
elif self.document.categories:
category = self.document.categories[-1]
category.overviewItems.items.append(item)
else:
self.document.links.append(item)
elif name == "important":
self.in_importants = True
def endElement(self, name):
if name == "important":
self.in_importants = False
def endDocument(self):
pass
class Document:
def __init__(self, attrs):
self.__dict__.update(attrs)
self.categories = []
self.links = []
self.important = []
def __iter__(self):
return iter(self.categories)
class Category:
def __init__(self, attrs):
self.__dict__.update(attrs)
self.overviewItems = None
class OverviewItems:
def __init__(self, attrs):
self.__dict__.update(attrs)
self.items = []
def __iter__(self):
return iter(self.items)
class Item:
def __init__(self, attrs):
self.__dict__.update(attrs)
def GetDocument(fname="pywin32-document.xml"):
parser = make_parser()
handler = categoryHandler()
parser.setContentHandler(handler)
parser.parse(fname)
return handler.document
if __name__ == '__main__':
doc = GetDocument()
print("Important Notes")
for link in doc.important:
print((" ", link.name, link.href))
print("Doc links")
for link in doc.links:
print((" ", link.name, link.href))
print("Doc categories")
for c in doc:
print((" ", c.id, c.label))