From 6db5a37cdb53bbbde6f9234e59f88c4461563a98 Mon Sep 17 00:00:00 2001 From: Daniel Grana Date: Fri, 9 Oct 2009 17:00:31 -0200 Subject: [PATCH] do specialized xmliter public functions and add tests --- scrapy/tests/test_utils_iterators.py | 36 +++++++++++++++++++--------- scrapy/utils/iterators.py | 8 +++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/scrapy/tests/test_utils_iterators.py b/scrapy/tests/test_utils_iterators.py index e534a393f..b9a097184 100644 --- a/scrapy/tests/test_utils_iterators.py +++ b/scrapy/tests/test_utils_iterators.py @@ -1,15 +1,16 @@ import os -import unittest import libxml2 +from twisted.trial import unittest -from scrapy.utils.iterators import csviter, xmliter +from scrapy.utils.iterators import csviter, xmliter, xmliter_lxml, xmliter_regex from scrapy.http import XmlResponse, TextResponse from scrapy.tests import get_testdata -class UtilsIteratorsTestCase(unittest.TestCase): - ### NOTE: Encoding issues have been found with BeautifulSoup for utf-16 files, utf-16 test removed ### - # pablo: Tests shouldn't be removed, but commented with proper steps on how - # to reproduce the missing functionality + +class XmliterTestCase(unittest.TestCase): + + xmliter = staticmethod(xmliter) + def test_xmliter(self): body = """\ \ @@ -25,7 +26,7 @@ class UtilsIteratorsTestCase(unittest.TestCase): response = XmlResponse(url="http://example.com", body=body) attrs = [] - for x in xmliter(response, 'product'): + for x in self.xmliter(response, 'product'): attrs.append((x.select("@id").extract(), x.select("name/text()").extract(), x.select("./type/text()").extract())) self.assertEqual(attrs, @@ -34,7 +35,7 @@ class UtilsIteratorsTestCase(unittest.TestCase): def test_xmliter_text(self): body = u"""onetwo""" - self.assertEqual([x.select("text()").extract() for x in xmliter(body, 'product')], + self.assertEqual([x.select("text()").extract() for x in self.xmliter(body, 'product')], [[u'one'], [u'two']]) def test_xmliter_namespaces(self): @@ -57,7 +58,7 @@ class UtilsIteratorsTestCase(unittest.TestCase): """ response = XmlResponse(url='http://mydummycompany.com', body=body) - my_iter = xmliter(response, 'item') + my_iter = self.xmliter(response, 'item') node = my_iter.next() node.register_namespace('g', 'http://base.google.com/ns/1.0') @@ -74,7 +75,7 @@ class UtilsIteratorsTestCase(unittest.TestCase): def test_xmliter_exception(self): body = u"""onetwo""" - iter = xmliter(body, 'product') + iter = self.xmliter(body, 'product') iter.next() iter.next() @@ -84,10 +85,23 @@ class UtilsIteratorsTestCase(unittest.TestCase): body = '\n\n Some Turkish Characters \xd6\xc7\xde\xdd\xd0\xdc \xfc\xf0\xfd\xfe\xe7\xf6\n\n\n' response = XmlResponse('http://www.example.com', body=body) self.assertEqual( - xmliter(response, 'item').next().extract(), + self.xmliter(response, 'item').next().extract(), u'Some Turkish Characters \xd6\xc7\u015e\u0130\u011e\xdc \xfc\u011f\u0131\u015f\xe7\xf6' ) + +class RegexXmliterTestCase(XmliterTestCase): + xmliter = staticmethod(xmliter_regex) + + +class LxmlXmliterTestCase(XmliterTestCase): + xmliter = staticmethod(xmliter_lxml) + try: + import lxml + except ImportError: + skip = True + + class UtilsCsvTestCase(unittest.TestCase): sample_feeds_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sample_data', 'feeds') sample_feed_path = os.path.join(sample_feeds_dir, 'feed-sample3.csv') diff --git a/scrapy/utils/iterators.py b/scrapy/utils/iterators.py index 436f89792..f778116f8 100644 --- a/scrapy/utils/iterators.py +++ b/scrapy/utils/iterators.py @@ -7,7 +7,7 @@ from scrapy.utils.python import re_rsearch, str_to_unicode from scrapy.utils.response import body_or_str -def _xmliter_regex(obj, nodename): +def xmliter_regex(obj, nodename): """Return a iterator of XPathSelector's over all nodes of a XML document, given tha name of the node to iterate. Useful for parsing XML feeds. @@ -31,7 +31,7 @@ def _xmliter_regex(obj, nodename): yield XmlXPathSelector(text=nodetext).select('//' + nodename)[0] -def _xmliter_lxml(obj, nodename, encoding='utf-8'): +def xmliter_lxml(obj, nodename): reader = _StreamReader(obj) iterable = etree.iterparse(reader, tag=nodename, encoding=reader.encoding) for _, node in iterable: @@ -66,9 +66,9 @@ class _StreamReader(object): try: from lxml import etree - xmliter = _xmliter_lxml + xmliter = xmliter_lxml except ImportError: - xmliter = _xmliter_regex + xmliter = xmliter_regex def csviter(obj, delimiter=None, headers=None, encoding=None):