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 elementtree import ElementTree # standard ET
     10  except ImportError:
     11      try:
     12          from xml.etree import ElementTree # Python 2.5+
     13      except ImportError:
     14          ElementTree = None
     15
     16  if hasattr(ElementTree, 'VERSION'):
     17      if tuple(ElementTree.VERSION.split('.')) < (1,3):
     18          # compatibility tests require ET 1.3+
     19          ElementTree = None
     20
     21  try:
     22      import cElementTree # standard ET
     23  except ImportError:
     24      try:
     25          from xml.etree import cElementTree # Python 2.5+
     26      except ImportError:
     27          cElementTree = None
     28
     29  if hasattr(cElementTree, 'VERSION'):
     30      if tuple(cElementTree.VERSION.split('.')) < (1,0,7):
     31          # compatibility tests require cET 1.0.7+
     32          cElementTree = None
     33
     34  try:
     35      import doctest
     36      # check if the system version has everything we need
     37      doctest.DocFileSuite
     38      doctest.NORMALIZE_WHITESPACE
     39      doctest.ELLIPSIS
     40  except (ImportError, AttributeError):
     41      # we need our own version to make it work (Python 2.3?)
     42      import local_doctest as doctest
     43
     44  try:
     45      sorted
     46  except NameError:
     47 -    def sorted(seq, **kwargs):
     48          seq = list(seq)
     49          seq.sort(**kwargs)
     50          return seq
     51
     52 -class HelperTestCase(unittest.TestCase):
     53 -    def tearDown(self):
     54          gc.collect()
     55
     56 -    def parse(self, text, parser=None):
     57          f = StringIO(text)
     58          return etree.parse(f, parser=parser)
     59
     60 -    def _rootstring(self, tree):
     61          return etree.tostring(tree.getroot()).replace(' ', '').replace('\n', '')
     62
     63      # assertFalse doesn't exist in Python 2.3
     64      try:
     65          unittest.TestCase.assertFalse
     66      except AttributeError:
     67          assertFalse = unittest.TestCase.failIf
     68
     69 -class SillyFileLike:
     70 -    def __init__(self, xml_data='<foo><bar/></foo>'):
     71          self.xml_data = xml_data
     72
     73 -    def read(self, amount=None):
     74          if self.xml_data:
     75              if amount:
     76                  data = self.xml_data[:amount]
     77                  self.xml_data = self.xml_data[amount:]
     78              else:
     79                  data = self.xml_data
     80                  self.xml_data = ''
     81              return data
     82          return ''
     83
     84 -class LargeFileLike:
     85 -    def __init__(self, charlen=100, depth=4, children=5):
     86          self.data = StringIO()
     87          self.chars  = 'a' * charlen
     88          self.children = range(children)
     89          self.more = self.iterelements(depth)
     90
     91 -    def iterelements(self, depth):
     92          yield '<root>'
     93          depth -= 1
     94          if depth > 0:
     95              for child in self.children:
     96                  for element in self.iterelements(depth):
     97                      yield element
     98                  yield self.chars
     99          else:
    100              yield self.chars
    101          yield '</root>'
    102
    103 -    def read(self, amount=None):
    104          data = self.data
    105          append = data.write
    106          if amount:
    107              for element in self.more:
    108                  append(element)
    109                  if data.tell() >= amount:
    110                      break
    111          else:
    112              for element in self.more:
    113                  append(element)
    114          result = data.getvalue()
    115          data.seek(0)
    116          data.truncate()
    117          if amount:
    118              self.data.write(result[amount:])
    119              result = result[:amount]
    120          return result
    121
    122 -def fileInTestDir(name):
    123      _testdir = os.path.split(__file__)[0]
    124      return os.path.join(_testdir, name)
    125
    126 -def canonicalize(xml):
    127      f = StringIO(xml)
    128      tree = etree.parse(f)
    129      f = StringIO()
    130      tree.write_c14n(f)
    131      return f.getvalue()
    132
    133 -def unentitify(xml):
    134      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    135          xml = xml.replace(entity_name, unichr(int(value)))
    136      return xml
    137