1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.xml_serializer import SubElement, Element, Serializer
18
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
19
19
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
20
20
import bzrlib.inventory as inventory
21
from bzrlib.revision import Revision
21
from bzrlib.revision import Revision
22
22
from bzrlib.errors import BzrError
25
28
class Serializer_v5(Serializer):
26
29
"""Version 5 serializer
33
36
def _pack_inventory(self, inv):
34
37
"""Convert to XML Element"""
35
e = Element('inventory',
38
e = Element('inventory')
38
40
if inv.root.file_id not in (None, ROOT_ID):
39
41
e.set('file_id', inv.root.file_id)
40
if inv.revision_id is not None:
41
e.set('revision_id', inv.revision_id)
42
42
for path, ie in inv.iter_entries():
43
43
e.append(self._pack_entry(ie))
46
47
def _pack_entry(self, ie):
47
48
"""Convert InventoryEntry to XML element"""
48
# TODO: should just be a plain assertion
49
49
if not InventoryEntry.versionable_kind(ie.kind):
50
50
raise AssertionError('unsupported entry kind %s' % ie.kind)
51
51
e = Element(ie.kind)
69
69
if ie.parent_id != ROOT_ID:
70
70
assert isinstance(ie.parent_id, basestring)
71
71
e.set('parent_id', ie.parent_id)
75
78
def _pack_revision(self, rev):
76
79
"""Revision object -> xml tree"""
77
80
root = Element('revision',
79
82
timestamp = '%.9f' % rev.timestamp,
80
83
revision_id = rev.revision_id,
81
84
inventory_sha1 = rev.inventory_sha1,
85
87
root.set('timezone', str(rev.timezone))
87
90
msg = SubElement(root, 'message')
88
91
msg.text = rev.message
91
95
pelts = SubElement(root, 'parents')
92
96
pelts.tail = pelts.text = '\n'
95
99
p = SubElement(pelts, 'revision_ref')
97
101
p.set('revision_id', parent_id)
99
self._pack_revision_properties(rev, root)
103
def _pack_revision_properties(self, rev, under_element):
104
top_elt = SubElement(under_element, 'properties')
105
for prop_name, prop_value in sorted(rev.properties.items()):
106
assert isinstance(prop_name, basestring)
107
assert isinstance(prop_value, basestring)
108
prop_elt = SubElement(top_elt, 'property')
109
prop_elt.set('name', prop_name)
110
prop_elt.text = prop_value
115
106
def _unpack_inventory(self, elt):
116
107
"""Construct from XML Element
118
109
assert elt.tag == 'inventory'
119
110
root_id = elt.get('file_id') or ROOT_ID
120
format = elt.get('format')
121
if format is not None:
123
raise BzrError("invalid format version %r on inventory"
125
revision_id = elt.get('revision_id')
126
inv = Inventory(root_id, revision_id=revision_id)
111
inv = Inventory(root_id)
128
113
ie = self._unpack_entry(e)
129
114
if ie.parent_id == ROOT_ID:
169
154
def _unpack_revision(self, elt):
170
155
"""XML Element -> Revision object"""
171
156
assert elt.tag == 'revision'
172
format = elt.get('format')
173
if format is not None:
175
raise BzrError("invalid format version %r on inventory"
177
158
rev = Revision(committer = elt.get('committer'),
178
159
timestamp = float(elt.get('timestamp')),
179
160
revision_id = elt.get('revision_id'),
180
161
inventory_sha1 = elt.get('inventory_sha1')
182
164
parents = elt.find('parents') or []
183
165
for p in parents:
184
166
assert p.tag == 'revision_ref', \
185
167
"bad parent node tag %r" % p.tag
186
168
rev.parent_ids.append(p.get('revision_id'))
187
self._unpack_revision_properties(elt, rev)
188
170
v = elt.get('timezone')
189
171
rev.timezone = v and int(v)
190
173
rev.message = elt.findtext('message') # text of <message>
194
def _unpack_revision_properties(self, elt, rev):
195
"""Unpack properties onto a revision."""
196
props_elt = elt.find('properties')
197
assert len(rev.properties) == 0
200
for prop_elt in props_elt:
201
assert prop_elt.tag == 'property', \
202
"bad tag under properties list: %r" % prop_elt.tag
203
name = prop_elt.get('name')
204
value = prop_elt.text
205
assert name not in rev.properties, \
206
"repeated property %r" % name
207
rev.properties[name] = value
210
178
serializer_v5 = Serializer_v5()