Mercurial > libervia-pubsub
changeset 202:77c61e2b8c75
Use `domish.Element`s to represent items, instead of serialized XML.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Wed, 25 Jun 2008 12:27:55 +0000 |
parents | d829db6095d6 |
children | 2c46e6664680 |
files | idavoll/backend.py idavoll/memory_storage.py idavoll/pgsql_storage.py idavoll/test/test_storage.py |
diffstat | 4 files changed, 50 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/idavoll/backend.py Tue Jun 24 08:42:16 2008 +0000 +++ b/idavoll/backend.py Wed Jun 25 12:27:55 2008 +0000 @@ -147,8 +147,6 @@ if persistItems or deliverPayloads: for item in items: - item.uri = None - item.defaultUri = None if not item.getAttribute("id"): item["id"] = str(uuid.uuid4()) @@ -224,32 +222,11 @@ def _sendLastPublished(self, node, subscriber): - class StringParser(object): - def __init__(self): - self.elementStream = domish.elementStream() - self.elementStream.DocumentStartEvent = self.docStart - self.elementStream.ElementEvent = self.elem - self.elementStream.DocumentEndEvent = self.docEnd - def docStart(self, elem): - self.document = elem - - def elem(self, elem): - self.document.addChild(elem) - - def docEnd(self): - pass - - def parse(self, string): - self.elementStream.parse(string) - return self.document - - def notifyItem(result): - if not result: + def notifyItem(items): + if not items: return - items = [domish.SerializedXML(item) for item in result] - reactor.callLater(0, self.dispatch, {'items': items, 'nodeIdentifier': node.nodeIdentifier,
--- a/idavoll/memory_storage.py Tue Jun 24 08:42:16 2008 +0000 +++ b/idavoll/memory_storage.py Wed Jun 25 12:27:55 2008 +0000 @@ -169,6 +169,24 @@ +class PublishedItem(object): + """ + A published item. + + This represent an item as it was published by an entity. + + @ivar element: The DOM representation of the item that was published. + @type element: L{Element<twisted.words.xish.domish.Element>} + @ivar publisher: The entity that published the item. + @type publisher: L{JID<twisted.words.protocols.jabber.jid.JID>} + """ + + def __init__(self, element, publisher): + self.element = element + self.publisher = publisher + + + class LeafNodeMixin: nodeType = 'leaf' @@ -179,12 +197,9 @@ def storeItems(self, items, publisher): - for data in items: - itemIdentifier = data["id"] - data = data.toXml() - if isinstance(data, str): - data = data.decode('utf-8') - item = (data, publisher) + for element in items: + item = PublishedItem(element, publisher) + itemIdentifier = element["id"] if itemIdentifier in self._items: self._itemlist.remove(self._items[itemIdentifier]) self._items[itemIdentifier] = item @@ -211,10 +226,10 @@ def getItems(self, maxItems=None): if maxItems: - list = self._itemlist[-maxItems:] + itemList = self._itemlist[-maxItems:] else: - list = self._itemlist - return defer.succeed([item[0] for item in list]) + itemList = self._itemlist + return defer.succeed([item.element for item in itemList]) def getItemsById(self, itemIdentifiers): @@ -225,7 +240,7 @@ except KeyError: pass else: - items.append(item[0]) + items.append(item.element) return defer.succeed(items)
--- a/idavoll/pgsql_storage.py Tue Jun 24 08:42:16 2008 +0000 +++ b/idavoll/pgsql_storage.py Wed Jun 25 12:27:55 2008 +0000 @@ -2,9 +2,13 @@ # See LICENSE for details. import copy + +from zope.interface import implements + from twisted.enterprise import adbapi from twisted.words.protocols.jabber import jid -from zope.interface import implements + +from wokkel.generic import parseXml from idavoll import error, iidavoll @@ -418,7 +422,7 @@ cursor.execute(query, (self.nodeIdentifier)) result = cursor.fetchall() - return [unicode(r[0], 'utf-8') for r in result] + return [parseXml(r[0]) for r in result] def getItemsById(self, itemIdentifiers): @@ -436,7 +440,7 @@ itemIdentifier)) result = cursor.fetchone() if result: - items.append(unicode(result[0], 'utf-8')) + items.append(parseXml(result[0])) return items
--- a/idavoll/test/test_storage.py Tue Jun 24 08:42:16 2008 +0000 +++ b/idavoll/test/test_storage.py Wed Jun 25 12:27:55 2008 +0000 @@ -261,7 +261,7 @@ return self.node.getItemsById(['new']) def cb2(result): - self.assertEqual(result[0], decode(ITEM_NEW.toXml())) + self.assertEqual(ITEM_NEW.toXml(), result[0].toXml()) d = self.node.storeItems([ITEM_NEW], PUBLISHER) d.addCallback(cb1) @@ -274,7 +274,7 @@ return self.node.getItemsById(['current']) def cb2(result): - self.assertEqual(result[0], decode(ITEM_UPDATED.toXml())) + self.assertEqual(ITEM_UPDATED.toXml(), result[0].toXml()) d = self.node.storeItems([ITEM_UPDATED], PUBLISHER) d.addCallback(cb1) @@ -284,11 +284,11 @@ def test_removeItems(self): def cb1(result): - self.assertEqual(result, ['to-be-deleted']) + self.assertEqual(['to-be-deleted'], result) return self.node.getItemsById(['to-be-deleted']) def cb2(result): - self.assertEqual(len(result), 0) + self.assertEqual(0, len(result)) d = self.node.removeItems(['to-be-deleted']) d.addCallback(cb1) @@ -298,7 +298,7 @@ def test_removeNonExistingItems(self): def cb(result): - self.assertEqual(result, []) + self.assertEqual([], result) d = self.node.removeItems(['non-existing']) d.addCallback(cb) @@ -307,7 +307,8 @@ def test_getItems(self): def cb(result): - self.assertIn(decode(ITEM.toXml()), result) + items = [item.toXml() for item in result] + self.assertIn(ITEM.toXml(), items) d = self.node.getItems() d.addCallback(cb) @@ -316,7 +317,8 @@ def test_lastItem(self): def cb(result): - self.assertEqual([decode(ITEM.toXml())], result) + self.assertEqual(1, len(result)) + self.assertEqual(ITEM.toXml(), result[0].toXml()) d = self.node.getItems(1) d.addCallback(cb) @@ -325,7 +327,7 @@ def test_getItemsById(self): def cb(result): - self.assertEqual(len(result), 1) + self.assertEqual(1, len(result)) d = self.node.getItemsById(['current']) d.addCallback(cb) @@ -334,7 +336,7 @@ def test_getNonExistingItemsById(self): def cb(result): - self.assertEqual(len(result), 0) + self.assertEqual(0, len(result)) d = self.node.getItemsById(['non-existing']) d.addCallback(cb) @@ -378,8 +380,9 @@ class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): def setUp(self): - from idavoll.memory_storage import Storage, LeafNode, Subscription, \ - defaultConfig + from idavoll.memory_storage import Storage, PublishedItem, LeafNode + from idavoll.memory_storage import Subscription, defaultConfig + self.s = Storage() self.s._nodes['pre-existing'] = \ LeafNode('pre-existing', OWNER, defaultConfig) @@ -397,12 +400,12 @@ subscriptions[SUBSCRIBER_PENDING.full()] = \ Subscription('pending') - item = (decode(ITEM_TO_BE_DELETED.toXml()), PUBLISHER) + item = PublishedItem(ITEM_TO_BE_DELETED, PUBLISHER) self.s._nodes['pre-existing']._items['to-be-deleted'] = item self.s._nodes['pre-existing']._itemlist.append(item) self.s._nodes['to-be-purged']._items['to-be-deleted'] = item self.s._nodes['to-be-purged']._itemlist.append(item) - item = (decode(ITEM.toXml()), PUBLISHER) + item = PublishedItem(ITEM, PUBLISHER) self.s._nodes['pre-existing']._items['current'] = item self.s._nodes['pre-existing']._itemlist.append(item)