74
74
def _read_element(self, f):
75
75
return ElementTree().parse(f)
78
class _Serializer_v4(Serializer):
79
"""Version 0.0.4 serializer
81
You should use the serialzer_v4 singleton."""
85
def _pack_inventory(self, inv):
86
"""Convert to XML Element"""
87
e = Element('inventory')
89
if inv.root.file_id not in (None, ROOT_ID):
90
e.set('file_id', inv.root.file_id)
91
for path, ie in inv.iter_entries():
92
e.append(self._pack_entry(ie))
96
def _pack_entry(self, ie):
97
"""Convert InventoryEntry to XML element"""
99
e.set('name', ie.name)
100
e.set('file_id', ie.file_id)
101
e.set('kind', ie.kind)
103
if ie.text_size != None:
104
e.set('text_size', '%d' % ie.text_size)
106
for f in ['text_id', 'text_sha1']:
111
# to be conservative, we don't externalize the root pointers
112
# for now, leaving them as null in the xml form. in a future
113
# version it will be implied by nested elements.
114
if ie.parent_id != ROOT_ID:
115
assert isinstance(ie.parent_id, basestring)
116
e.set('parent_id', ie.parent_id)
123
def _unpack_inventory(self, elt):
124
"""Construct from XML Element
126
assert elt.tag == 'inventory'
127
root_id = elt.get('file_id') or ROOT_ID
128
inv = Inventory(root_id)
130
ie = self._unpack_entry(e)
131
if ie.parent_id == ROOT_ID:
132
ie.parent_id = root_id
137
def _unpack_entry(self, elt):
138
assert elt.tag == 'entry'
140
## original format inventories don't have a parent_id for
141
## nodes in the root directory, but it's cleaner to use one
143
parent_id = elt.get('parent_id')
144
if parent_id == None:
147
ie = InventoryEntry(elt.get('file_id'),
151
ie.text_id = elt.get('text_id')
152
ie.text_sha1 = elt.get('text_sha1')
154
## mutter("read inventoryentry: %r" % (elt.attrib))
156
v = elt.get('text_size')
157
ie.text_size = v and int(v)
162
def _pack_revision(self, rev):
163
"""Revision object -> xml tree"""
164
root = Element('revision',
165
committer = rev.committer,
166
timestamp = '%.9f' % rev.timestamp,
167
revision_id = rev.revision_id,
168
inventory_id = rev.inventory_id,
169
inventory_sha1 = rev.inventory_sha1,
172
root.set('timezone', str(rev.timezone))
175
msg = SubElement(root, 'message')
176
msg.text = rev.message
180
pelts = SubElement(root, 'parents')
181
pelts.tail = pelts.text = '\n'
182
for rr in rev.parents:
183
assert isinstance(rr, RevisionReference)
184
p = SubElement(pelts, 'revision_ref')
186
assert rr.revision_id
187
p.set('revision_id', rr.revision_id)
189
p.set('revision_sha1', rr.revision_sha1)
194
def _unpack_revision(self, elt):
195
"""XML Element -> Revision object"""
197
# <changeset> is deprecated...
198
if elt.tag not in ('revision', 'changeset'):
199
raise BzrError("unexpected tag in revision file: %r" % elt)
201
rev = Revision(committer = elt.get('committer'),
202
timestamp = float(elt.get('timestamp')),
203
revision_id = elt.get('revision_id'),
204
inventory_id = elt.get('inventory_id'),
205
inventory_sha1 = elt.get('inventory_sha1')
208
precursor = elt.get('precursor')
209
precursor_sha1 = elt.get('precursor_sha1')
211
pelts = elt.find('parents')
215
assert p.tag == 'revision_ref', \
216
"bad parent node tag %r" % p.tag
217
rev_ref = RevisionReference(p.get('revision_id'),
218
p.get('revision_sha1'))
219
rev.parents.append(rev_ref)
223
prec_parent = rev.parents[0].revision_id
224
assert prec_parent == precursor
226
# revisions written prior to 0.0.5 have a single precursor
227
# give as an attribute
228
rev_ref = RevisionReference(precursor, precursor_sha1)
229
rev.parents.append(rev_ref)
231
v = elt.get('timezone')
232
rev.timezone = v and int(v)
234
rev.message = elt.findtext('message') # text of <message>
240
"""singleton instance"""
241
serializer_v4 = _Serializer_v4()