Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Added ItemDelta objects and modified replays to make use of them

elpolillaa309dfeb7d36
diff --git a/scrapy/trunk/scrapy/command/commands/replay.py b/scrapy/trunk/scrapy/command/commands/replay.py
index 36dbdcf43..590f976a9 100644
--- a/scrapy/trunk/scrapy/command/commands/replay.py
+++ b/scrapy/trunk/scrapy/command/commands/replay.py
@@ -164,24 +164,12 @@ class Command(ScrapyCommand):
return c, s
def _item_diff(self, old, new):
- olddic = old.__dict__
- newdic = new.__dict__
- ignored_attrs = set(self.opts.ignores or [])
- allattrs = [a for a in olddic.keys() + newdic.keys() if not a.startswith('_') and a not in ignored_attrs]
-
- diff = {}
- for attr in sorted(allattrs):
- oldval = olddic.get(attr, None)
- newval = newdic.get(attr, None)
- if oldval != newval:
- diff[attr] = {}
- diff[attr]['old'] = oldval
- diff[attr]['new'] = newval
+ delta = new - old
s = ""
- if diff:
+ if delta.diff:
s += ">>> Item guid=%s name=%s\n" % (old.guid, old.name)
- s += display.pformat(diff) + "\n"
+ s += display.pformat(delta.diff) + "\n"
return s
def _format_items(self, items):
diff --git a/scrapy/trunk/scrapy/contrib/item/models.py b/scrapy/trunk/scrapy/contrib/item/models.py
index ede4bef24..ac060865c 100644
--- a/scrapy/trunk/scrapy/contrib/item/models.py
+++ b/scrapy/trunk/scrapy/contrib/item/models.py
@@ -96,7 +96,8 @@ class RobustScrapedItem(ScrapedItem):
raise AttributeError("Attribute '%s' doesn't exist" % attr)
def __eq__(self, other):
- return self.version == other.version
+ if other:
+ return self.version == other.version
def __ne__(self, other):
return self.version != other.version
diff --git a/scrapy/trunk/scrapy/item/models.py b/scrapy/trunk/scrapy/item/models.py
index 1001df263..1e95640fd 100644
--- a/scrapy/trunk/scrapy/item/models.py
+++ b/scrapy/trunk/scrapy/item/models.py
@@ -17,3 +17,27 @@ class ScrapedItem(object):
value = self.adaptors_pipe.execute(attrname, value)
if not hasattr(self, attrname):
setattr(self, attrname, value)
+
+
+class ItemDelta(object):
+ """
+ This class represents the difference between
+ a pair of items.
+ """
+
+ def __init__(self, old, new):
+ self.diff = self.do_diff()
+
+ def do_diff(self):
+ """
+ This method should retreive a dictionary
+ containing the changes between both items
+ as in this example:
+
+ >>> delta.do_diff()
+ >>> {'attrib': {'new': 'New value', 'old': 'Old value'}, # Common attributes
+ 'attrib2': {'new': 'New value 2', 'old': 'Old value 2'},
+ 'attrib3': [{'new': 'New list value', 'old': 'Old list value'}, # List attributes
+ {'new': 'New list value 2', 'old': 'Old list value 2'}]}
+ """
+ pass