コード例 #1
0
def getDescription(xmlfile):
    """Extract test data

  Each test case is an XML file which contains not only a test feed
  but also the description of the test, i.e. the condition that we
  would expect the parser to create when it parses the feed.  Example:
  <!--
  Description: feed title
  Expect:      feed['title'] == u'Example feed'
  -->
  """

    data = open(xmlfile, 'rb').read()
    if data[:4] == _l2bytes([0x4c, 0x6f, 0xa7, 0x94]):
        data = feedparser._ebcdic_to_ascii(data)
    elif data[:4] == _l2bytes([0x00, 0x00, 0xfe, 0xff]):
        if not _utf32_available: return None, None, None, '0'
        data = unicode(data, 'utf-32be').encode('utf-8')
    elif data[:4] == _l2bytes([0xff, 0xfe, 0x00, 0x00]):
        if not _utf32_available: return None, None, None, '0'
        data = unicode(data, 'utf-32le').encode('utf-8')
    elif data[:4] == _l2bytes([0x00, 0x00, 0x00, 0x3c]):
        if not _utf32_available: return None, None, None, '0'
        data = unicode(data, 'utf-32be').encode('utf-8')
    elif data[:4] == _l2bytes([0x3c, 0x00, 0x00, 0x00]):
        if not _utf32_available: return None, None, None, '0'
        data = unicode(data, 'utf-32le').encode('utf-8')
    elif data[:4] == _l2bytes([0x00, 0x3c, 0x00, 0x3f]):
        data = unicode(data, 'utf-16be').encode('utf-8')
    elif data[:4] == _l2bytes([0x3c, 0x00, 0x3f, 0x00]):
        data = unicode(data, 'utf-16le').encode('utf-8')
    elif (data[:2] == _l2bytes([0xfe, 0xff
                                ])) and (data[2:4] != _l2bytes([0x00, 0x00])):
        data = unicode(data[2:], 'utf-16be').encode('utf-8')
    elif (data[:2] == _l2bytes([0xff, 0xfe
                                ])) and (data[2:4] != _l2bytes([0x00, 0x00])):
        data = unicode(data[2:], 'utf-16le').encode('utf-8')
    elif data[:3] == _l2bytes([0xef, 0xbb, 0xbf]):
        data = data[3:]
    skip_results = skip_re.search(data)
    if skip_results:
        skipUnless = skip_results.group(1).strip()
    else:
        skipUnless = '1'
    search_results = desc_re.search(data)
    if not search_results:
        raise RuntimeError, "can't parse %s" % xmlfile
    description, evalString = map(string.strip, list(search_results.groups()))
    description = xmlfile + ": " + unicode(description, 'utf8')
    return TestCase.failUnlessEval, description, evalString, skipUnless
コード例 #2
0
ファイル: feedparsertest.py プロジェクト: fteem/feedzor
def getDescription(xmlfile):
  """Extract test data

  Each test case is an XML file which contains not only a test feed
  but also the description of the test, i.e. the condition that we
  would expect the parser to create when it parses the feed.  Example:
  <!--
  Description: feed title
  Expect:      feed['title'] == u'Example feed'
  -->
  """

  data = open(xmlfile, 'rb').read()
  if data[:4] == _l2bytes([0x4c, 0x6f, 0xa7, 0x94]):
    data = feedparser._ebcdic_to_ascii(data)
  elif data[:4] == _l2bytes([0x00, 0x00, 0xfe, 0xff]):
    if not _utf32_available: return None, None, None, '0'
    data = str(data, 'utf-32be').encode('utf-8')
  elif data[:4] == _l2bytes([0xff, 0xfe, 0x00, 0x00]):
    if not _utf32_available: return None, None, None, '0'
    data = str(data, 'utf-32le').encode('utf-8')
  elif data[:4] == _l2bytes([0x00, 0x00, 0x00, 0x3c]):
    if not _utf32_available: return None, None, None, '0'
    data = str(data, 'utf-32be').encode('utf-8')
  elif data[:4] == _l2bytes([0x3c, 0x00, 0x00, 0x00]):
    if not _utf32_available: return None, None, None, '0'
    data = str(data, 'utf-32le').encode('utf-8')
  elif data[:4] == _l2bytes([0x00, 0x3c, 0x00, 0x3f]):
    data = str(data, 'utf-16be').encode('utf-8')
  elif data[:4] == _l2bytes([0x3c, 0x00, 0x3f, 0x00]):
    data = str(data, 'utf-16le').encode('utf-8')
  elif (data[:2] == _l2bytes([0xfe, 0xff])) and (data[2:4] != _l2bytes([0x00, 0x00])):
    data = str(data[2:], 'utf-16be').encode('utf-8')
  elif (data[:2] == _l2bytes([0xff, 0xfe])) and (data[2:4] != _l2bytes([0x00, 0x00])):
    data = str(data[2:], 'utf-16le').encode('utf-8')
  elif data[:3] == _l2bytes([0xef, 0xbb, 0xbf]):
    data = data[3:]
  skip_results = skip_re.search(data)
  if skip_results:
    skipUnless = skip_results.group(1).strip()
  else:
    skipUnless = '1'
  search_results = desc_re.search(data)
  if not search_results:
    raise RuntimeError("can't parse %s" % xmlfile)
  description, evalString = list(map(string.strip, list(search_results.groups())))
  description = xmlfile + ": " + str(description, 'utf8')
  return TestCase.failUnlessEval, description, evalString, skipUnless
コード例 #3
0
ファイル: feedparsertest.py プロジェクト: c24b/zaptext
def getDescription(xmlfile):
  """Extract test data

  Each test case is an XML file which contains not only a test feed
  but also the description of the test, i.e. the condition that we
  would expect the parser to create when it parses the feed.  Example:
  <!--
  Description: feed title
  Expect:      feed['title'] == u'Example feed'
  -->
  """

  data = open(xmlfile).read()
  if data[:4] == '\x4c\x6f\xa7\x94':
    data = feedparser._ebcdic_to_ascii(data)
  elif data[:4] == '\x00\x00\xfe\xff':
    if not _utf32_available: return None, None, None, '0'
    data = unicode(data, 'utf-32be').encode('utf-8')
  elif data[:4] == '\xff\xfe\x00\x00':
    if not _utf32_available: return None, None, None, '0'
    data = unicode(data, 'utf-32le').encode('utf-8')
  elif data[:4] == '\x00\x00\x00\x3c':
    if not _utf32_available: return None, None, None, '0'
    data = unicode(data, 'utf-32be').encode('utf-8')
  elif data[:4] == '\x3c\x00\x00\x00':
    if not _utf32_available: return None, None, None, '0'
    data = unicode(data, 'utf-32le').encode('utf-8')
  elif data[:4] == '\x00\x3c\x00\x3f':
    data = unicode(data, 'utf-16be').encode('utf-8')
  elif data[:4] == '\x3c\x00\x3f\x00':
    data = unicode(data, 'utf-16le').encode('utf-8')
  elif (data[:2] == '\xfe\xff') and (data[2:4] != '\x00\x00'):
    data = unicode(data[2:], 'utf-16be').encode('utf-8')
  elif (data[:2] == '\xff\xfe') and (data[2:4] != '\x00\x00'):
    data = unicode(data[2:], 'utf-16le').encode('utf-8')
  elif data[:3] == '\xef\xbb\xbf':
    data = data[3:]
  skip_results = skip_re.search(data)
  if skip_results:
    skipUnless = skip_results.group(1).strip()
  else:
    skipUnless = '1'
  search_results = desc_re.search(data)
  if not search_results:
    raise RuntimeError, "can't parse %s" % xmlfile
  description, evalString = map(string.strip, list(search_results.groups()))
  description = xmlfile + ": " + description
  return TestCase.failUnlessEval, description, evalString, skipUnless