15
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
16
from bzrlib.xml_serializer import ElementTree, SubElement, Element, Serializer
19
17
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
20
18
import bzrlib.inventory as inventory
21
19
from bzrlib.revision import Revision
22
20
from bzrlib.errors import BzrError
28
23
class Serializer_v5(Serializer):
29
24
"""Version 5 serializer
36
31
def _pack_inventory(self, inv):
37
32
"""Convert to XML Element"""
38
e = Element('inventory')
33
e = Element('inventory',
40
36
if inv.root.file_id not in (None, ROOT_ID):
41
37
e.set('file_id', inv.root.file_id)
82
78
timestamp = '%.9f' % rev.timestamp,
83
79
revision_id = rev.revision_id,
84
80
inventory_sha1 = rev.inventory_sha1,
87
84
root.set('timezone', str(rev.timezone))
90
86
msg = SubElement(root, 'message')
91
87
msg.text = rev.message
95
90
pelts = SubElement(root, 'parents')
96
91
pelts.tail = pelts.text = '\n'
99
94
p = SubElement(pelts, 'revision_ref')
101
96
p.set('revision_id', parent_id)
98
self._pack_revision_properties(rev, root)
102
def _pack_revision_properties(self, rev, under_element):
103
top_elt = SubElement(under_element, 'properties')
104
for prop_name, prop_value in sorted(rev.properties.items()):
105
assert isinstance(prop_name, basestring)
106
assert isinstance(prop_value, basestring)
107
prop_elt = SubElement(top_elt, 'property')
108
prop_elt.set('name', prop_name)
109
prop_elt.text = prop_value
106
114
def _unpack_inventory(self, elt):
107
115
"""Construct from XML Element
109
117
assert elt.tag == 'inventory'
110
118
root_id = elt.get('file_id') or ROOT_ID
119
format = elt.get('format')
120
if format is not None:
122
raise BzrError("invalid format version %r on inventory"
111
124
inv = Inventory(root_id)
113
126
ie = self._unpack_entry(e)
154
167
def _unpack_revision(self, elt):
155
168
"""XML Element -> Revision object"""
156
169
assert elt.tag == 'revision'
170
format = elt.get('format')
171
if format is not None:
173
raise BzrError("invalid format version %r on inventory"
158
175
rev = Revision(committer = elt.get('committer'),
159
176
timestamp = float(elt.get('timestamp')),
160
177
revision_id = elt.get('revision_id'),
161
178
inventory_sha1 = elt.get('inventory_sha1')
164
180
parents = elt.find('parents') or []
165
181
for p in parents:
166
182
assert p.tag == 'revision_ref', \
167
183
"bad parent node tag %r" % p.tag
168
184
rev.parent_ids.append(p.get('revision_id'))
185
self._unpack_revision_properties(elt, rev)
170
186
v = elt.get('timezone')
171
187
rev.timezone = v and int(v)
173
188
rev.message = elt.findtext('message') # text of <message>
192
def _unpack_revision_properties(self, elt, rev):
193
"""Unpack properties onto a revision."""
194
props_elt = elt.find('properties')
195
assert len(rev.properties) == 0
198
for prop_elt in props_elt:
199
assert prop_elt.tag == 'property', \
200
"bad tag under properties list: %r" % p.tag
201
name = prop_elt.get('name')
202
value = prop_elt.text
203
assert name not in rev.properties, \
204
"repeated property %r" % p.name
205
rev.properties[name] = value
178
208
serializer_v5 = Serializer_v5()