comparison idavoll/memory_storage.py @ 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 e404775b12df
children b4bf0a5ce50d
comparison
equal deleted inserted replaced
201:d829db6095d6 202:77c61e2b8c75
167 167
168 return defer.succeed(affiliations) 168 return defer.succeed(affiliations)
169 169
170 170
171 171
172 class PublishedItem(object):
173 """
174 A published item.
175
176 This represent an item as it was published by an entity.
177
178 @ivar element: The DOM representation of the item that was published.
179 @type element: L{Element<twisted.words.xish.domish.Element>}
180 @ivar publisher: The entity that published the item.
181 @type publisher: L{JID<twisted.words.protocols.jabber.jid.JID>}
182 """
183
184 def __init__(self, element, publisher):
185 self.element = element
186 self.publisher = publisher
187
188
189
172 class LeafNodeMixin: 190 class LeafNodeMixin:
173 191
174 nodeType = 'leaf' 192 nodeType = 'leaf'
175 193
176 def __init__(self): 194 def __init__(self):
177 self._items = {} 195 self._items = {}
178 self._itemlist = [] 196 self._itemlist = []
179 197
180 198
181 def storeItems(self, items, publisher): 199 def storeItems(self, items, publisher):
182 for data in items: 200 for element in items:
183 itemIdentifier = data["id"] 201 item = PublishedItem(element, publisher)
184 data = data.toXml() 202 itemIdentifier = element["id"]
185 if isinstance(data, str):
186 data = data.decode('utf-8')
187 item = (data, publisher)
188 if itemIdentifier in self._items: 203 if itemIdentifier in self._items:
189 self._itemlist.remove(self._items[itemIdentifier]) 204 self._itemlist.remove(self._items[itemIdentifier])
190 self._items[itemIdentifier] = item 205 self._items[itemIdentifier] = item
191 self._itemlist.append(item) 206 self._itemlist.append(item)
192 207
209 return defer.succeed(deleted) 224 return defer.succeed(deleted)
210 225
211 226
212 def getItems(self, maxItems=None): 227 def getItems(self, maxItems=None):
213 if maxItems: 228 if maxItems:
214 list = self._itemlist[-maxItems:] 229 itemList = self._itemlist[-maxItems:]
215 else: 230 else:
216 list = self._itemlist 231 itemList = self._itemlist
217 return defer.succeed([item[0] for item in list]) 232 return defer.succeed([item.element for item in itemList])
218 233
219 234
220 def getItemsById(self, itemIdentifiers): 235 def getItemsById(self, itemIdentifiers):
221 items = [] 236 items = []
222 for itemIdentifier in itemIdentifiers: 237 for itemIdentifier in itemIdentifiers:
223 try: 238 try:
224 item = self._items[itemIdentifier] 239 item = self._items[itemIdentifier]
225 except KeyError: 240 except KeyError:
226 pass 241 pass
227 else: 242 else:
228 items.append(item[0]) 243 items.append(item.element)
229 return defer.succeed(items) 244 return defer.succeed(items)
230 245
231 246
232 def purge(self): 247 def purge(self):
233 self._items = {} 248 self._items = {}