Package lxml :: Package tests :: Module common_imports
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.common_imports

  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 # Python 2.5 
 10  except ImportError: 
 11      try: 
 12          from elementtree import ElementTree # standard ET 
 13      except ImportError: 
 14          ElementTree = None 
 15   
 16  try: 
 17      import doctest 
 18      # check if the system version has everything we need 
 19      doctest.DocFileSuite 
 20      doctest.NORMALIZE_WHITESPACE 
 21      doctest.ELLIPSIS 
 22  except (ImportError, AttributeError): 
 23      # we need our own version to make it work (Python 2.3?) 
 24      import local_doctest as doctest 
 25   
 26  try: 
 27      from operator import itemgetter 
 28  except ImportError: 
29 - def itemgetter(item):
30 return lambda obj: obj[item]
31
32 -class HelperTestCase(unittest.TestCase):
33 - def tearDown(self):
34 gc.collect()
35
36 - def parse(self, text):
37 f = StringIO(text) 38 return etree.parse(f)
39
40 - def _rootstring(self, tree):
41 return etree.tostring(tree.getroot()).replace(' ', '').replace('\n', '')
42 43 # assertFalse doesn't exist in Python 2.3 44 try: 45 unittest.TestCase.assertFalse 46 except AttributeError: 47 assertFalse = unittest.TestCase.failIf
48
49 -class SillyFileLike:
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
64 -class LargeFileLike:
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
71 - def iterelements(self, depth):
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
102 -def fileInTestDir(name):
103 _testdir = os.path.split(__file__)[0] 104 return os.path.join(_testdir, name)
105
106 -def canonicalize(xml):
107 f = StringIO(xml) 108 tree = etree.parse(f) 109 f = StringIO() 110 tree.write_c14n(f) 111 return f.getvalue()
112
113 -def unentitify(xml):
114 for entity_name, value in re.findall("(&#([0-9]+);)", xml): 115 xml = xml.replace(entity_name, unichr(int(value))) 116 return xml
117