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
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
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
entries = inv.iter_entries()
36
38
e = Element('inventory',
39
path, root = entries.next()
40
if root.file_id not in (None, ROOT_ID):
41
e.set('file_id', root.file_id)
42
if inv.revision_id is not None:
43
e.set('revision_id', inv.revision_id)
44
for path, ie in entries:
41
if inv.root.file_id not in (None, ROOT_ID):
42
e.set('file_id', inv.root.file_id)
43
for path, ie in inv.iter_entries():
45
44
e.append(self._pack_entry(ie))
48
48
def _pack_entry(self, ie):
49
49
"""Convert InventoryEntry to XML element"""
50
# TODO: should just be a plain assertion
51
50
if not InventoryEntry.versionable_kind(ie.kind):
52
51
raise AssertionError('unsupported entry kind %s' % ie.kind)
53
52
e = Element(ie.kind)
71
70
if ie.parent_id != ROOT_ID:
72
71
assert isinstance(ie.parent_id, basestring)
73
72
e.set('parent_id', ie.parent_id)
77
79
def _pack_revision(self, rev):
78
80
"""Revision object -> xml tree"""
79
81
root = Element('revision',
97
99
p = SubElement(pelts, 'revision_ref')
99
101
p.set('revision_id', parent_id)
101
self._pack_revision_properties(rev, root)
105
def _pack_revision_properties(self, rev, under_element):
106
top_elt = SubElement(under_element, 'properties')
107
for prop_name, prop_value in sorted(rev.properties.items()):
108
assert isinstance(prop_name, basestring)
109
assert isinstance(prop_value, basestring)
110
prop_elt = SubElement(top_elt, 'property')
111
prop_elt.set('name', prop_name)
112
prop_elt.text = prop_value
117
105
def _unpack_inventory(self, elt):
118
106
"""Construct from XML Element
124
112
if format != '5':
125
113
raise BzrError("invalid format version %r on inventory"
127
revision_id = elt.get('revision_id')
128
inv = Inventory(root_id, revision_id=revision_id)
115
inv = Inventory(root_id)
130
117
ie = self._unpack_entry(e)
131
118
if ie.parent_id == ROOT_ID:
186
173
assert p.tag == 'revision_ref', \
187
174
"bad parent node tag %r" % p.tag
188
175
rev.parent_ids.append(p.get('revision_id'))
189
self._unpack_revision_properties(elt, rev)
190
177
v = elt.get('timezone')
191
178
rev.timezone = v and int(v)
192
180
rev.message = elt.findtext('message') # text of <message>
196
def _unpack_revision_properties(self, elt, rev):
197
"""Unpack properties onto a revision."""
198
props_elt = elt.find('properties')
199
assert len(rev.properties) == 0
202
for prop_elt in props_elt:
203
assert prop_elt.tag == 'property', \
204
"bad tag under properties list: %r" % prop_elt.tag
205
name = prop_elt.get('name')
206
value = prop_elt.text
207
# If a property had an empty value ('') cElementTree reads
208
# that back as None, convert it back to '', so that all
209
# properties have string values
212
assert name not in rev.properties, \
213
"repeated property %r" % name
214
rev.properties[name] = value
217
184
serializer_v5 = Serializer_v5()