From 82cf911192c5c36bea5d545b3bf0200cebdfa88a Mon Sep 17 00:00:00 2001 From: elpolilla Date: Mon, 24 Nov 2008 12:05:23 +0000 Subject: [PATCH] Added method for inserting adaptors into a pipeline --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40405 --- scrapy/trunk/scrapy/item/models.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scrapy/trunk/scrapy/item/models.py b/scrapy/trunk/scrapy/item/models.py index 004844f26..b36542941 100644 --- a/scrapy/trunk/scrapy/item/models.py +++ b/scrapy/trunk/scrapy/item/models.py @@ -18,11 +18,22 @@ class ScrapedItem(object): return self def set_attrib_adaptors(self, attrib, pipe): - """ - Set the adaptors (from a list or tuple) to be used for a specific attribute. - """ + """ Set the adaptors (from a list or tuple) to be used for a specific attribute. """ self._adaptors_dict[attrib] = AdaptorPipe(pipe) if hasattr(pipe, '__iter__') else None + def add_adaptor(self, attrib, adaptor, position=None): + """ + Add an adaptor for the specified attribute at the given position. + If position = None, then the adaptor is appended at the end of the pipeline. + """ + if callable(adaptor): + pipe = self._adaptors_dict.get(attrib, []) + if position is None: + pipe = pipe + [adaptor] + else: + pipe.insert(position, adaptor) + self.set_attrib_adaptors(attrib, pipe) + def attribute(self, attrname, value, **kwargs): pipe = self._adaptors_dict.get(attrname) if pipe: