222
class _Serializer_v5(Serializer):
223
"""Version 5 serializer
225
Packs objects into XML and vice versa.
227
You should use the serialzer_v5 singleton."""
231
def _pack_inventory(self, inv):
232
"""Convert to XML Element"""
233
e = Element('inventory')
235
if inv.root.file_id not in (None, ROOT_ID):
236
e.set('file_id', inv.root.file_id)
237
for path, ie in inv.iter_entries():
238
e.append(self._pack_entry(ie))
242
def _pack_entry(self, ie):
243
"""Convert InventoryEntry to XML element"""
244
assert ie.kind == 'directory' or ie.kind == 'file'
246
e.set('name', ie.name)
247
e.set('file_id', ie.file_id)
249
if ie.text_size != None:
250
e.set('text_size', '%d' % ie.text_size)
252
for f in ['text_version', 'text_sha1', 'entry_version']:
257
# to be conservative, we don't externalize the root pointers
258
# for now, leaving them as null in the xml form. in a future
259
# version it will be implied by nested elements.
260
if ie.parent_id != ROOT_ID:
261
assert isinstance(ie.parent_id, basestring)
262
e.set('parent_id', ie.parent_id)
269
def _pack_revision(self, rev):
270
"""Revision object -> xml tree"""
271
root = Element('revision',
272
committer = rev.committer,
273
timestamp = '%.9f' % rev.timestamp,
274
revision_id = rev.revision_id,
275
inventory_id = rev.inventory_id,
276
inventory_sha1 = rev.inventory_sha1,
279
root.set('timezone', str(rev.timezone))
282
msg = SubElement(root, 'message')
283
msg.text = rev.message
287
pelts = SubElement(root, 'parents')
288
pelts.tail = pelts.text = '\n'
289
for rr in rev.parents:
290
assert isinstance(rr, RevisionReference)
291
p = SubElement(pelts, 'revision_ref')
293
assert rr.revision_id
294
p.set('revision_id', rr.revision_id)
300
def _unpack_inventory(self, elt):
301
"""Construct from XML Element
303
assert elt.tag == 'inventory'
304
root_id = elt.get('file_id') or ROOT_ID
305
inv = Inventory(root_id)
307
ie = self._unpack_entry(e)
308
if ie.parent_id == ROOT_ID:
309
ie.parent_id = root_id
314
def _unpack_entry(self, elt):
316
assert kind == 'directory' or kind == 'file'
318
parent_id = elt.get('parent_id')
319
if parent_id == None:
322
ie = InventoryEntry(elt.get('file_id'),
326
ie.text_version = elt.get('text_version')
327
ie.entry_version = elt.get('entry_version')
328
ie.text_sha1 = elt.get('text_sha1')
329
v = elt.get('text_size')
330
ie.text_size = v and int(v)
335
def _unpack_revision(self, elt):
336
"""XML Element -> Revision object"""
337
assert elt.tag == 'revision'
339
rev = Revision(committer = elt.get('committer'),
340
timestamp = float(elt.get('timestamp')),
341
revision_id = elt.get('revision_id'),
342
inventory_id = elt.get('inventory_id'),
343
inventory_sha1 = elt.get('inventory_sha1')
346
for p in elt.find('parents'):
347
assert p.tag == 'revision_ref', \
348
"bad parent node tag %r" % p.tag
349
rev_ref = RevisionReference(p.get('revision_id'))
350
rev.parents.append(rev_ref)
352
v = elt.get('timezone')
353
rev.timezone = v and int(v)
355
rev.message = elt.findtext('message') # text of <message>
360
222
"""singleton instance"""
361
223
serializer_v4 = _Serializer_v4()
363
serializer_v5 = _Serializer_v5()