1 import unittest
2 import os.path
3 from StringIO import StringIO
4 import re, gc
5
6 from lxml import etree
7
8 try:
9 from xml.etree import ElementTree
10 except ImportError:
11 try:
12 from elementtree import ElementTree
13 except ImportError:
14 ElementTree = None
15
16 try:
17 import doctest
18
19 doctest.DocFileSuite
20 doctest.NORMALIZE_WHITESPACE
21 doctest.ELLIPSIS
22 except (ImportError, AttributeError):
23
24 import local_doctest as doctest
25
26 try:
27 from operator import itemgetter
28 except ImportError:
30 return lambda obj: obj[item]
31
35
39
41 return etree.tostring(tree.getroot()).replace(' ', '').replace('\n', '')
42
43
44 try:
45 unittest.TestCase.assertFalse
46 except AttributeError:
47 assertFalse = unittest.TestCase.failIf
48
50 - def __init__(self, xml_data='<foo><bar/></foo>'):
51 self.xml_data = xml_data
52
53 - def read(self, amount=None):
54 if self.xml_data:
55 if amount:
56 data = self.xml_data[:amount]
57 self.xml_data = self.xml_data[amount:]
58 else:
59 data = self.xml_data
60 self.xml_data = ''
61 return data
62 return ''
63
65 - def __init__(self, charlen=100, depth=4, children=5):
66 self.data = StringIO()
67 self.chars = 'a' * charlen
68 self.children = range(children)
69 self.more = self.iterelements(depth)
70
72 yield '<root>'
73 depth -= 1
74 if depth > 0:
75 for child in self.children:
76 for element in self.iterelements(depth):
77 yield element
78 yield self.chars
79 else:
80 yield self.chars
81 yield '</root>'
82
83 - def read(self, amount=None):
84 data = self.data
85 append = data.write
86 if amount:
87 for element in self.more:
88 append(element)
89 if data.tell() >= amount:
90 break
91 else:
92 for element in self.more:
93 append(element)
94 result = data.getvalue()
95 data.seek(0)
96 data.truncate()
97 if amount:
98 self.data.write(result[amount:])
99 result = result[:amount]
100 return result
101
103 _testdir = os.path.split(__file__)[0]
104 return os.path.join(_testdir, name)
105
107 f = StringIO(xml)
108 tree = etree.parse(f)
109 f = StringIO()
110 tree.write_c14n(f)
111 return f.getvalue()
112
114 for entity_name, value in re.findall("(&#([0-9]+);)", xml):
115 xml = xml.replace(entity_name, unichr(int(value)))
116 return xml
117