File: test_howto.py

package info (click to toggle)
qm 1.1.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,628 kB
  • ctags: 10,249
  • sloc: python: 41,482; ansic: 20,611; xml: 12,837; sh: 485; makefile: 226
file content (173 lines) | stat: -rw-r--r-- 4,640 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

# Test suite containing code from the XML HOWTO

# SAX testing code

print "SAX tests:\n"

from xml.sax import saxlib, saxutils, make_parser, ContentHandler
from xml.sax.handler import feature_namespaces
import StringIO
import string

comic_xml = StringIO.StringIO("""<collection>
  <comic title="Sandman" number='62'>
    <writer>Neil Gaiman</writer>
    <penciller pages='1-9,18-24'>Glyn Dillon</penciller>
    <penciller pages="10-17">Charles Vess</penciller>
  </comic>
  <comic title="Shade, the Changing Man" number="7">
    <writer>Peter Milligan</writer>
    <penciller>Chris Bachalo</penciller>
  </comic>
</collection>""")

class FindIssue(saxutils.DefaultHandler):
    def __init__(self, title, number):
        self.search_title, self.search_number = title, number

    def startElement(self, name, attrs):
        # If it's not a comic element, ignore it
        if name != 'comic': return

        # Look for the title and number attributes (see text)
        title = attrs.get('title', None)
        number = attrs.get('number', None)
        if title == self.search_title and number == self.search_number:
            print title, '#'+str(number), 'found'

    def error(self, exception):
        import sys
        sys.stderr.write("%s\n" % exception)

if 1:
    # Create a parser
    parser = make_parser()
    # Disable namespace processing
    parser.setFeature(feature_namespaces, 0)

    # Create the handler
    dh = FindIssue('Sandman', '62')

    # Tell the parser to use our handler
    parser.setContentHandler(dh)
    parser.setErrorHandler(dh)

    # Parse the input
    parser.parse(comic_xml)


def normalize_whitespace(text):
    "Remove redundant whitespace from a string"
    return string.join(string.split(text), ' ')

class FindWriter(ContentHandler):
    def __init__(self, search_name):
        # Save the name we're looking for
        self.search_name = normalize_whitespace(search_name)

        # Initialize the flag to false
        self.inWriterContent = 0

    def startElement(self, name, attrs):
        # If it's a comic element, save the title and issue
        if name == 'comic':
            title = normalize_whitespace(attrs.get('title', ""))
            number = normalize_whitespace(attrs.get('number', ""))
            self.this_title = title
            self.this_number = number

        # If it's the start of a writer element, set flag
        elif name == 'writer':
            self.inWriterContent = 1
            self.writerName = ""

    def characters(self, ch):
        if self.inWriterContent:
            self.writerName = self.writerName + ch

    def endElement(self, name):
        if name == 'writer':
            self.inWriterContent = 0
            self.writerName = normalize_whitespace(self.writerName)
            if self.writerName == self.search_name:
                print self.this_title, self.this_number

if 1:
    # Create a parser
    parser = make_parser()

    # Disable namespace processing
    parser.setFeature(feature_namespaces, 0)

    # Create the handler
    dh = FindWriter('Peter Milligan')

    # Tell the parser to use our handler
    parser.setContentHandler(dh)

    # Print a title
    print '\nTitles by Peter Milligan:'

    # Parse the input
    comic_xml.seek(0)
    parser.parse(comic_xml)


# DOM tests

print "DOM tests:\n"

import sys
from xml.dom.ext.reader.Sax import FromXml
from xml.dom.ext import PrettyPrint

dom_xml = """<?xml version="1.0" encoding="iso-8859-1"?>
<xbel>  
  <?processing instruction?>
  <desc>No description</desc>
  <folder>
    <title>XML bookmarks</title>
    <bookmark href="http://www.python.org/sigs/xml-sig/" >
      <title>SIG for XML Processing in Python</title>
    </bookmark>
  </folder>
</xbel>"""

# Parse the input into a DOM tree
doc = FromXml(dom_xml)

# Print it
# for testing, we must explicitly pass sys.stdout, as regrtest will
# bind this to a different object
PrettyPrint(doc, sys.stdout)

# whitespace-removal currently not supported
# utils.strip_whitespace(doc)
# print ' With whitespace removed:'
# print doc.toxml()

# Builder code

print 'DOM creation tests'

from xml.dom.DOMImplementation import implementation
d = implementation.createDocument(None, None, None)

# Create the root element
r = d.createElement("html")
d.appendChild(r)

# Create an empty 'head' element
r.appendChild(d.createElement("head"))

# Start the 'body' element, giving it an attribute
b = d.createElement("body")
b.setAttribute('background','#ffffff')
r.appendChild(b)

# Add a text node
b.appendChild(d.createTextNode("The body text goes here."))

# Print the document
PrettyPrint(d, sys.stdout)