From 51bdd6944b610aaa6431cb5cae1d988748ecac87 Mon Sep 17 00:00:00 2001 From: olveyra Date: Thu, 4 Sep 2008 19:11:53 +0000 Subject: [PATCH] - removed contrib/adaptors.py - display adaptor name when an exception raises inside the adaptor pipeline - add debug parameter to item attribute method to display input/output of each adaptor --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40206 --- scrapy/trunk/scrapy/contrib/adaptors.py | 67 ------------------------- scrapy/trunk/scrapy/item/adaptors.py | 17 ++++++- 2 files changed, 15 insertions(+), 69 deletions(-) delete mode 100644 scrapy/trunk/scrapy/contrib/adaptors.py diff --git a/scrapy/trunk/scrapy/contrib/adaptors.py b/scrapy/trunk/scrapy/contrib/adaptors.py deleted file mode 100644 index 304d06bb5..000000000 --- a/scrapy/trunk/scrapy/contrib/adaptors.py +++ /dev/null @@ -1,67 +0,0 @@ -""" -Generic Adaptors for item adaptation pipe. Performs tasks -usually needed by most cases -""" - -import re - -from scrapy.item.models import BaseAdaptor -from scrapy.utils.python import flatten, unique -from scrapy.xpath import XPathSelector -from scrapy.utils.misc import unquote_html, location_str -from scrapy.conf import settings - -class ExtendedAdaptor(BaseAdaptor): - - def do(self, function, a, *args, **kwargs): - """Executes an adaptor printing a lot of useful debugging info.""" - - debug = kwargs.get('debug') - fname = function.__name__ - - if debug: - print repr(kwargs) - print " %07s | input >" % fname, location_str(a) - a = function(a, *args, **kwargs) - if debug: - print " %07s | output>" % fname, a - - return a - -class ExtractAdaptor(ExtendedAdaptor): - - def function(self, location, **pipeargs): - return self.do(self.extract, location, **pipeargs) - - def extract(self, location, **kwargs): - """Extract a list of strings from the location passed. - Receives a list of XPathSelectors or an XPathSelector, - or a list of strings or a string. - - Return a list of strings extracted. - - This function *always* returns a list. - """ - if not location: - return [] - if isinstance(location, (list, tuple)): - strings = flatten([self.extract(o, **kwargs) for o in location]) - if kwargs.get('remove_dupes', False): - strings = unique(strings) - if kwargs.get('first', False): - return strings[:1] - else: - return strings - # XPathSelector - elif isinstance(location, XPathSelector): - strings = [location.extract()] - # Strings - elif isinstance(location, unicode): - strings = [unquote_html(location, keep_reserved=True)] - elif isinstance(location, str): - encoding = kwargs.get("encoding", settings.get('DEFAULT_DATA_ENCODING')) - strings = [unquote_html(unicode(location, encoding), keep_reserved=True)] - else: - raise TypeError, "unsupported location type: %s" % type(location) - - return strings \ No newline at end of file diff --git a/scrapy/trunk/scrapy/item/adaptors.py b/scrapy/trunk/scrapy/item/adaptors.py index 59e192fe4..f5e8af5ca 100644 --- a/scrapy/trunk/scrapy/item/adaptors.py +++ b/scrapy/trunk/scrapy/item/adaptors.py @@ -1,4 +1,6 @@ import re +from traceback import format_exc +from scrapy.core import log class DuplicatedAdaptorName(Exception): pass @@ -67,7 +69,7 @@ class AdaptorPipe: self.__adaptorspipe.insert(pos, adaptor) return pos - def execute(self, attrname, value, **pipeargs): + def execute(self, attrname, value, debug=False, **pipeargs): """ Execute pipeline for attribute name "attrname" and value "value". Pass the given pipeargs to each adaptor function in the pipe. @@ -85,6 +87,17 @@ class AdaptorPipe: else: adapt = True if adapt: - value = adaptor.function(value, **pipeargs) + try: + if debug: + print "pipeargs: %s" % repr(pipeargs) + print " %07s | input >" % adaptor.name, repr(value) + value = adaptor.function(value, **pipeargs) + if debug: + print " %07s | output>" % adaptor.name, repr(value) + except Exception, e: + print "Error in '%s' adaptor. Traceback text:" % adaptor.name + print format_exc() + return + return value