42
42
def read_inventory(self, f):
43
43
return self._unpack_inventory(self._read_element(f))
45
def write_revision(self, rev, f):
46
self._write_element(self._pack_revision(rev), f)
48
def read_revision(self, f):
49
return self._unpack_revision(self._read_element(f))
45
51
def _write_element(self, elt, f):
46
52
ElementTree(elt).write(f, 'utf-8')
142
def _pack_revision(self, rev):
143
"""Revision object -> xml tree"""
144
root = Element('revision',
145
committer = rev.committer,
146
timestamp = '%.9f' % rev.timestamp,
147
revision_id = rev.revision_id,
148
inventory_id = rev.inventory_id,
149
inventory_sha1 = rev.inventory_sha1,
152
root.set('timezone', str(rev.timezone))
155
msg = SubElement(root, 'message')
156
msg.text = rev.message
160
pelts = SubElement(root, 'parents')
161
pelts.tail = pelts.text = '\n'
162
for rr in rev.parents:
163
assert isinstance(rr, RevisionReference)
164
p = SubElement(pelts, 'revision_ref')
166
assert rr.revision_id
167
p.set('revision_id', rr.revision_id)
169
p.set('revision_sha1', rr.revision_sha1)
174
def _unpack_revision(self, elt):
175
"""XML Element -> Revision object"""
177
# <changeset> is deprecated...
178
if elt.tag not in ('revision', 'changeset'):
179
raise bzrlib.errors.BzrError("unexpected tag in revision file: %r" % elt)
181
rev = Revision(committer = elt.get('committer'),
182
timestamp = float(elt.get('timestamp')),
183
revision_id = elt.get('revision_id'),
184
inventory_id = elt.get('inventory_id'),
185
inventory_sha1 = elt.get('inventory_sha1')
188
precursor = elt.get('precursor')
189
precursor_sha1 = elt.get('precursor_sha1')
191
pelts = elt.find('parents')
195
assert p.tag == 'revision_ref', \
196
"bad parent node tag %r" % p.tag
197
rev_ref = RevisionReference(p.get('revision_id'),
198
p.get('revision_sha1'))
199
rev.parents.append(rev_ref)
203
prec_parent = rev.parents[0].revision_id
204
assert prec_parent == precursor
206
# revisions written prior to 0.0.5 have a single precursor
207
# give as an attribute
208
rev_ref = RevisionReference(precursor, precursor_sha1)
209
rev.parents.append(rev_ref)
211
v = elt.get('timezone')
212
rev.timezone = v and int(v)
214
rev.message = elt.findtext('message') # text of <message>
136
219
"""singleton instance"""
137
220
serializer_v4 = _Serializer_v4()
144
"""Write object o to file f as XML.
146
o must provide a to_element method.
148
ElementTree(o.to_element()).write(f, 'utf-8')
152
def unpack_xml(cls, f):
153
return cls.from_element(ElementTree().parse(f))