From c57833dab7ba13166fcd8ced1456297c42c4cd72 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Mon, 2 Jul 2012 12:24:19 -0400 Subject: [PATCH] Define equality for XModuleDescriptors --- common/lib/xmodule/raw_module.py | 1 + common/lib/xmodule/template_module.py | 1 + common/lib/xmodule/x_module.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/common/lib/xmodule/raw_module.py b/common/lib/xmodule/raw_module.py index 59f28ff4f0..9fe9a9198b 100644 --- a/common/lib/xmodule/raw_module.py +++ b/common/lib/xmodule/raw_module.py @@ -3,6 +3,7 @@ from lxml import etree from xmodule.mako_module import MakoModuleDescriptor from xmodule.xml_module import XmlDescriptor + class RawDescriptor(MakoModuleDescriptor, XmlDescriptor): """ Module that provides a raw editing view of it's data and children diff --git a/common/lib/xmodule/template_module.py b/common/lib/xmodule/template_module.py index 1057fc2a25..064d48f431 100644 --- a/common/lib/xmodule/template_module.py +++ b/common/lib/xmodule/template_module.py @@ -37,5 +37,6 @@ class CustomTagModule(XModule): def get_html(self): return self.html + class CustomTagDescriptor(RawDescriptor): module_class = CustomTagModule diff --git a/common/lib/xmodule/x_module.py b/common/lib/xmodule/x_module.py index 0af68a2a1a..9f56d95fe5 100644 --- a/common/lib/xmodule/x_module.py +++ b/common/lib/xmodule/x_module.py @@ -213,6 +213,10 @@ class XModuleDescriptor(Plugin): # A list of metadata that this module can inherit from its parent module inheritable_metadata = ('graded', 'due', 'graceperiod', 'showanswer', 'rerandomize') + # A list of descriptor attributes that must be equal for the discriptors to be + # equal + equality_attributes = ('definition', 'metadata', 'location', 'shared_state_key', '_inherited_metadata') + # ============================= STRUCTURAL MANIPULATION =========================== def __init__(self, system, @@ -395,6 +399,27 @@ class XModuleDescriptor(Plugin): """ raise NotImplementedError("get_html() must be provided by specific modules") + # =============================== BUILTIN METHODS =========================== + def __eq__(self, other): + eq = (self.__class__ == other.__class__ and + all(getattr(self, attr, None) == getattr(other, attr, None) + for attr in self.equality_attributes)) + + if not eq: + for attr in self.equality_attributes: + print getattr(self, attr, None), getattr(other, attr, None), getattr(self, attr, None) == getattr(other, attr, None) + + return eq + + def __repr__(self): + return "{class_}({system!r}, {definition!r}, location={location!r}, metadata={metadata!r})".format( + class_=self.__class__.__name__, + system=self.system, + definition=self.definition, + location=self.location, + metadata=self.metadata + ) + class DescriptorSystem(object): def __init__(self, load_item, resources_fs):