1 import unittest
2 import os.path
3 from StringIO import StringIO
4 import re, gc
5
6 from lxml import etree
7
9 l = []
10 for part in re.findall('([0-9]+|[^0-9.]+)', version_string):
11 try:
12 l.append(int(part))
13 except ValueError:
14 l.append(part)
15 return tuple(l)
16
17 try:
18 from elementtree import ElementTree
19 except ImportError:
20 try:
21 from xml.etree import ElementTree
22 except ImportError:
23 ElementTree = None
24
25 if hasattr(ElementTree, 'VERSION'):
26 if make_version_tuple(ElementTree.VERSION)[:2] < (1,3):
27
28 ElementTree = None
29
30 try:
31 import cElementTree
32 except ImportError:
33 try:
34 from xml.etree import cElementTree
35 except ImportError:
36 cElementTree = None
37
38 if hasattr(cElementTree, 'VERSION'):
39 if make_version_tuple(cElementTree.VERSION)[:2] <= (1,0):
40
41 cElementTree = None
42
43 try:
44 import doctest
45
46 doctest.DocFileSuite
47 doctest.NORMALIZE_WHITESPACE
48 doctest.ELLIPSIS
49 except (ImportError, AttributeError):
50
51 import local_doctest as doctest
52
53 try:
54 sorted
55 except NameError:
57 seq = list(seq)
58 seq.sort(**kwargs)
59 return seq
60
64
65 - def parse(self, text, parser=None):
68
70 return etree.tostring(tree.getroot()).replace(' ', '').replace('\n', '')
71
72
73 try:
74 unittest.TestCase.assertFalse
75 except AttributeError:
76 assertFalse = unittest.TestCase.failIf
77
79 - def __init__(self, xml_data='<foo><bar/></foo>'):
80 self.xml_data = xml_data
81
82 - def read(self, amount=None):
83 if self.xml_data:
84 if amount:
85 data = self.xml_data[:amount]
86 self.xml_data = self.xml_data[amount:]
87 else:
88 data = self.xml_data
89 self.xml_data = ''
90 return data
91 return ''
92
94 - def __init__(self, charlen=100, depth=4, children=5):
95 self.data = StringIO()
96 self.chars = 'a' * charlen
97 self.children = range(children)
98 self.more = self.iterelements(depth)
99
101 yield '<root>'
102 depth -= 1
103 if depth > 0:
104 for child in self.children:
105 for element in self.iterelements(depth):
106 yield element
107 yield self.chars
108 else:
109 yield self.chars
110 yield '</root>'
111
112 - def read(self, amount=None):
113 data = self.data
114 append = data.write
115 if amount:
116 for element in self.more:
117 append(element)
118 if data.tell() >= amount:
119 break
120 else:
121 for element in self.more:
122 append(element)
123 result = data.getvalue()
124 data.seek(0)
125 data.truncate()
126 if amount:
127 self.data.write(result[amount:])
128 result = result[:amount]
129 return result
130
132 _testdir = os.path.split(__file__)[0]
133 return os.path.join(_testdir, name)
134
136 f = StringIO(xml)
137 tree = etree.parse(f)
138 f = StringIO()
139 tree.write_c14n(f)
140 return f.getvalue()
141
143 for entity_name, value in re.findall("(&#([0-9]+);)", xml):
144 xml = xml.replace(entity_name, unichr(int(value)))
145 return xml
146