From 6d45971411f88f134abc28051c678986db8e9df8 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 24 Mar 2011 12:25:17 -0400 Subject: Allow a stanza plugin to override a parent's interfaces. Each interface, say foo, may be overridden in three ways: set_foo get_foo del_foo To declare an override in a plugin, add the class field overrides as so: overrides = ['set_foo', 'del_foo'] Each override must have a matching set_foo(), etc method for implementing the new behaviour. To enable the overrides for a particular parent stanza, pass the option overrides=True to register_stanza_plugin. register_stanza_plugin(Stanza, Plugin, overrides=True) Example code: class Test(ElementBase): name = 'test' namespace = 'testing' interfaces = set(('foo', 'bar')) sub_interfaces = set(('bar',)) class TestOverride(ElementBase): name = 'test-override' namespace = 'testing' plugin_attrib = 'override' interfaces = set(('foo',)) overrides = ['set_foo'] def setup(self, xml): # Don't include an XML element in the parent stanza # since we're adding just an attribute. # If adding a regular subelement, no need to do this. self.xml = ET.Element('') def set_foo(self, value): print("overrides!") self.parent()._set_attr('foo', 'override-%s' % value) register_stanza_plugin(Test, TestOverride, overrides=True) Example usage: >>> t = TestStanza() >>> t['foo'] = 'bar' >>> t['foo'] 'override-bar' --- tests/test_stanza_element.py | 97 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test_stanza_element.py b/tests/test_stanza_element.py index f7387d36..dc67d1c5 100644 --- a/tests/test_stanza_element.py +++ b/tests/test_stanza_element.py @@ -53,9 +53,8 @@ class TestElementBase(SleekTest): name = "foo" namespace = "foo" interfaces = set(('bar', 'baz')) - subitem = set((TestSubStanza,)) - register_stanza_plugin(TestStanza, TestStanzaPlugin) + register_stanza_plugin(TestStanza, TestStanzaPlugin, iterable=True) stanza = TestStanza() stanza['bar'] = 'a' @@ -100,8 +99,8 @@ class TestElementBase(SleekTest): name = "foo" namespace = "foo" interfaces = set(('bar', 'baz')) - subitem = set((TestSubStanza,)) + register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) register_stanza_plugin(TestStanza, TestStanzaPlugin2) @@ -115,7 +114,7 @@ class TestElementBase(SleekTest): 'substanzas': [{'__childtag__': '{foo}subfoo', 'bar': 'c', 'baz': ''}]} - stanza.setStanzaValues(values) + stanza.values = values self.check(stanza, """ @@ -143,7 +142,7 @@ class TestElementBase(SleekTest): plugin_attrib = "foobar" interfaces = set(('fizz',)) - TestStanza.subitem = (TestStanza,) + register_stanza_plugin(TestStanza, TestStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() @@ -457,7 +456,6 @@ class TestElementBase(SleekTest): namespace = "foo" interfaces = set(('bar','baz', 'qux')) sub_interfaces = set(('qux',)) - subitem = (TestSubStanza,) def setQux(self, value): self._set_sub_text('qux', text=value) @@ -470,6 +468,7 @@ class TestElementBase(SleekTest): namespace = "http://test/slash/bar" interfaces = set(('attrib',)) + register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) register_stanza_plugin(TestStanza, TestStanzaPlugin) stanza = TestStanza() @@ -590,7 +589,8 @@ class TestElementBase(SleekTest): name = "foo" namespace = "foo" interfaces = set(('bar', 'baz')) - subitem = (TestSubStanza,) + + register_stanza_plugin(TestStanza, TestSubStanza, iterable=True) stanza = TestStanza() substanza1 = TestSubStanza() @@ -657,4 +657,87 @@ class TestElementBase(SleekTest): self.failUnless(stanza1 != stanza2, "Divergent stanza copies incorrectly compared equal.") + def testExtension(self): + """Testing using is_extension.""" + + class TestStanza(ElementBase): + name = "foo" + namespace = "foo" + interfaces = set(('bar', 'baz')) + + class TestExtension(ElementBase): + name = 'extended' + namespace = 'foo' + plugin_attrib = name + interfaces = set((name,)) + is_extension = True + + def set_extended(self, value): + self.xml.text = value + + def get_extended(self): + return self.xml.text + + def del_extended(self): + self.parent().xml.remove(self.xml) + + register_stanza_plugin(TestStanza, TestExtension) + + stanza = TestStanza() + stanza['extended'] = 'testing' + + self.check(stanza, """ + + testing + + """) + + self.failUnless(stanza['extended'] == 'testing', + "Could not retrieve stanza extension value.") + + del stanza['extended'] + self.check(stanza, """ + + """) + + def testOverrides(self): + """Test using interface overrides.""" + + class TestStanza(ElementBase): + name = "foo" + namespace = "foo" + interfaces = set(('bar', 'baz')) + + class TestOverride(ElementBase): + name = 'overrider' + namespace = 'foo' + plugin_attrib = name + interfaces = set(('bar',)) + overrides = ['set_bar'] + + def setup(self, xml): + # Don't create XML for the plugin + self.xml = ET.Element('') + + def set_bar(self, value): + if not value.startswith('override-'): + self.parent()._set_attr('bar', 'override-%s' % value) + else: + self.parent()._set_attr('bar', value) + + stanza = TestStanza() + stanza['bar'] = 'foo' + self.check(stanza, """ + + """) + + register_stanza_plugin(TestStanza, TestOverride, overrides=True) + + stanza = TestStanza() + stanza['bar'] = 'foo' + self.check(stanza, """ + + """) + + suite = unittest.TestLoader().loadTestsFromTestCase(TestElementBase) -- cgit v1.2.3