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