- 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
This commit is contained in:
olveyra 2008-09-04 19:11:53 +00:00
parent daf51203e3
commit 51bdd6944b
2 changed files with 15 additions and 69 deletions

View File

@ -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

View File

@ -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