def __delitem__(self, key): todelete = self[key] if hasattr(self, '_todelete'): self._todelete.append(todelete) else: self._todelete = [todelete] AdapterNode.__delitem__(self, key)
def __getitem__(self, key): try: return AdapterNode.__getitem__(self, key) except KeyError: if not key in self.iterkeys(): raise KeyError(key) revision = RevisionAdapter(self.model[key], key, self) self[key] = revision return revision
def __getitem__(self, key): try: return AdapterNode.__getitem__(self, key) except KeyError: if not key in self.iterkeys(): raise KeyError(key) media = MediaAdapter(self.model[key], key, self) self[key] = media return media
def test_AdapterNode(self): toadapt = BaseNode() toadapt['foo'] = BaseNode() toadapt['bar'] = BaseNode() toadapt.attrs.title = 'Some title' toadapt.attrs.description = 'Some description' # Adapt created node structure adapter = AdapterNode(toadapt, 'name', None) # Check ``AdapterNode`` expected = "<BaseNode object 'foo' at" self.assertTrue(str(adapter.model['foo']).startswith(expected)) # ``attrs`` self.assertEqual(adapter.attrs.title, 'Some title') # The adapter node is responsible to return other adapter node or # application nodes on ``__getitem__`` if application hierarchy # continues. It's possible to do key aliasing as well at this place. # This dummy class does a static mapping on ``__getitem__``. class TestAdapterNode(AdapterNode): def __getitem__(self, key): return AdapterNode(self.model['bar'], key, self) node = TestAdapterNode(toadapt, 'adapter', None) child = node['aliased'] expected = "<AdapterNode object 'aliased' at" self.assertTrue(str(child).startswith(expected)) self.assertTrue(child.model is toadapt['bar']) self.assertEqual([key for key in node], ['foo', 'bar']) # The application node path differs from the adapted node path. This is # essential to keep the application path sane while not violating the # adapted node's structure self.assertEqual(child.path, ['adapter', 'aliased']) self.assertEqual(child.model.path, [None, 'bar'])
def __getitem__(self, key): return AdapterNode(self.model['bar'], key, self)