1 import unittest
2 import os.path
3 import re, gc, sys
4
5 from lxml import etree
6
8 l = []
9 for part in re.findall('([0-9]+|[^0-9.]+)', version_string):
10 try:
11 l.append(int(part))
12 except ValueError:
13 l.append(part)
14 return tuple(l)
15
16 try:
17 from elementtree import ElementTree
18 except ImportError:
19 try:
20 from xml.etree import ElementTree
21 except ImportError:
22 ElementTree = None
23
24 if hasattr(ElementTree, 'VERSION'):
25 if make_version_tuple(ElementTree.VERSION)[:2] < (1,3):
26
27 ElementTree = None
28
29 try:
30 import cElementTree
31 except ImportError:
32 try:
33 from xml.etree import cElementTree
34 except ImportError:
35 cElementTree = None
36
37 if hasattr(cElementTree, 'VERSION'):
38 if make_version_tuple(cElementTree.VERSION)[:2] <= (1,0):
39
40 cElementTree = None
41
42 try:
43 import doctest
44
45 doctest.DocFileSuite
46 doctest.DocTestParser
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 else:
61 locals()['sorted'] = sorted
62
64 module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
65 return os.path.normpath(os.path.join(
66 os.path.dirname(getattr(module, '__file__', '')), filename))
67
68 if sys.version_info[0] >= 3:
69
70 unicode = str
71 - def _str(s, encoding="UTF-8"):
73 - def _bytes(s, encoding="UTF-8"):
75 from io import StringIO, BytesIO as _BytesIO
77 if args and isinstance(args[0], str):
78 args = (args[0].encode("UTF-8"),)
79 return _BytesIO(*args)
80
81 doctest_parser = doctest.DocTestParser()
82 _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
83 _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
92 else:
93
94 - def _str(s, encoding="UTF-8"):
96 - def _bytes(s, encoding="UTF-8"):
98 from StringIO import StringIO
99 BytesIO = StringIO
100
101 doctest_parser = doctest.DocTestParser()
102 _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
103 _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
104 _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
114
118
119 - def parse(self, text, parser=None):
122
126
127
128 try:
129 unittest.TestCase.assertFalse
130 except AttributeError:
131 assertFalse = unittest.TestCase.failIf
132
135 self.xml_data = xml_data
136
137 - def read(self, amount=None):
138 if self.xml_data:
139 if amount:
140 data = self.xml_data[:amount]
141 self.xml_data = self.xml_data[amount:]
142 else:
143 data = self.xml_data
144 self.xml_data = _bytes('')
145 return data
146 return _bytes('')
147
149 - def __init__(self, charlen=100, depth=4, children=5):
150 self.data = BytesIO()
151 self.chars = _bytes('a') * charlen
152 self.children = range(children)
153 self.more = self.iterelements(depth)
154
156 yield _bytes('<root>')
157 depth -= 1
158 if depth > 0:
159 for child in self.children:
160 for element in self.iterelements(depth):
161 yield element
162 yield self.chars
163 else:
164 yield self.chars
165 yield _bytes('</root>')
166
167 - def read(self, amount=None):
168 data = self.data
169 append = data.write
170 if amount:
171 for element in self.more:
172 append(element)
173 if data.tell() >= amount:
174 break
175 else:
176 for element in self.more:
177 append(element)
178 result = data.getvalue()
179 data.seek(0)
180 data.truncate()
181 if amount:
182 append(result[amount:])
183 result = result[:amount]
184 return result
185
187 - def __init__(self, charlen=100, depth=4, children=5):
192
194 yield _str('<root>')
195 depth -= 1
196 if depth > 0:
197 for child in self.children:
198 for element in self.iterelements(depth):
199 yield element
200 yield self.chars
201 else:
202 yield self.chars
203 yield _str('</root>')
204
206 _testdir = os.path.dirname(__file__)
207 return os.path.join(_testdir, name)
208
210 f = BytesIO(xml)
211 tree = etree.parse(f)
212 f = BytesIO()
213 tree.write_c14n(f)
214 return f.getvalue()
215
217 for entity_name, value in re.findall("(&#([0-9]+);)", xml):
218 xml = xml.replace(entity_name, unichr(int(value)))
219 return xml
220