lxml.tests.common_imports

Package lxml :: Package tests :: Module common_imports |
[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 -def make_version_tuple(version_string):
      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 # standard ET
     19  except ImportError:
     20      try:
     21          from xml.etree import ElementTree # Python 2.5+
     22      except ImportError:
     23          ElementTree = None
     24
     25  if hasattr(ElementTree, 'VERSION'):
     26      if make_version_tuple(ElementTree.VERSION)[:2] < (1,3):
     27          # compatibility tests require ET 1.3+
     28          ElementTree = None
     29
     30  try:
     31      import cElementTree # standard ET
     32  except ImportError:
     33      try:
     34          from xml.etree import cElementTree # Python 2.5+
     35      except ImportError:
     36          cElementTree = None
     37
     38  if hasattr(cElementTree, 'VERSION'):
     39      if make_version_tuple(cElementTree.VERSION)[:2] <= (1,0):
     40          # compatibility tests do not run with cET 1.0.7
     41          cElementTree = None
     42
     43  try:
     44      import doctest
     45      # check if the system version has everything we need
     46      doctest.DocFileSuite
     47      doctest.NORMALIZE_WHITESPACE
     48      doctest.ELLIPSIS
     49  except (ImportError, AttributeError):
     50      # we need our own version to make it work (Python 2.3?)
     51      import local_doctest as doctest
     52
     53  try:
     54      sorted
     55  except NameError:
     56 -    def sorted(seq, **kwargs):
     57          seq = list(seq)
     58          seq.sort(**kwargs)
     59          return seq
     60
     61 -class HelperTestCase(unittest.TestCase):
     62 -    def tearDown(self):
     63          gc.collect()
     64
     65 -    def parse(self, text, parser=None):
     66          f = StringIO(text)
     67          return etree.parse(f, parser=parser)
     68
     69 -    def _rootstring(self, tree):
     70          return etree.tostring(tree.getroot()).replace(' ', '').replace('\n', '')
     71
     72      # assertFalse doesn't exist in Python 2.3
     73      try:
     74          unittest.TestCase.assertFalse
     75      except AttributeError:
     76          assertFalse = unittest.TestCase.failIf
     77
     78 -class SillyFileLike:
     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
     93 -class LargeFileLike:
     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
    100 -    def iterelements(self, depth):
    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
    131 -def fileInTestDir(name):
    132      _testdir = os.path.split(__file__)[0]
    133      return os.path.join(_testdir, name)
    134
    135 -def canonicalize(xml):
    136      f = StringIO(xml)
    137      tree = etree.parse(f)
    138      f = StringIO()
    139      tree.write_c14n(f)
    140      return f.getvalue()
    141
    142 -def unentitify(xml):
    143      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    144          xml = xml.replace(entity_name, unichr(int(value)))
    145      return xml
    146