Added ItemDelta objects and modified replays to make use of them

--HG--
extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40308
This commit is contained in:
elpolilla 2008-10-06 10:14:52 +00:00
parent 88597e3a77
commit a309dfeb7d
3 changed files with 29 additions and 16 deletions

View File

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

View File

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

View File

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