20
20
# "XML is like violence: if it doesn't solve your problem, you aren't
21
21
# using enough of it." -- various
24
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
25
__author__ = "Martin Pool <mbp@canonical.com>"
23
# importing this module is fairly slow because it has to load several
28
from cElementTree import Element, ElementTree, SubElement
27
from util.cElementTree import ElementTree, SubElement, Element
29
28
except ImportError:
30
from elementtree.ElementTree import Element, ElementTree, SubElement
33
from trace import mutter
37
raise Exception("XMLMixin.to_element must be overridden in concrete classes")
39
def write_xml(self, f):
40
ElementTree(self.to_element()).write(f, 'utf-8')
29
from util.elementtree.ElementTree import ElementTree, SubElement, Element
31
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
32
from bzrlib.revision import Revision, RevisionReference
35
class Serializer(object):
36
"""Abstract object serialize/deserialize"""
37
def write_inventory(self, inv, f):
38
"""Write inventory to a file"""
39
elt = self._pack_inventory(inv)
40
self._write_element(elt, f)
42
def read_inventory(self, f):
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))
51
def _write_element(self, elt, f):
52
ElementTree(elt).write(f, 'utf-8')
44
return cls.from_element(ElementTree().parse(f))
46
read_xml = classmethod(read_xml)
55
def _read_element(self, f):
56
return ElementTree().parse(f)
60
class _Serializer_v4(Serializer):
61
"""Version 0.0.4 serializer"""
65
def _pack_inventory(self, inv):
66
"""Convert to XML Element"""
67
e = Element('inventory')
69
if inv.root.file_id not in (None, ROOT_ID):
70
e.set('file_id', inv.root.file_id)
71
for path, ie in inv.iter_entries():
72
e.append(self._pack_entry(ie))
76
def _pack_entry(self, ie):
77
"""Convert InventoryEntry to XML element"""
79
e.set('name', ie.name)
80
e.set('file_id', ie.file_id)
81
e.set('kind', ie.kind)
83
if ie.text_size != None:
84
e.set('text_size', '%d' % ie.text_size)
86
for f in ['text_id', 'text_sha1']:
91
# to be conservative, we don't externalize the root pointers
92
# for now, leaving them as null in the xml form. in a future
93
# version it will be implied by nested elements.
94
if ie.parent_id != ROOT_ID:
95
assert isinstance(ie.parent_id, basestring)
96
e.set('parent_id', ie.parent_id)
103
def _unpack_inventory(self, elt):
104
"""Construct from XML Element
106
assert elt.tag == 'inventory'
107
root_id = elt.get('file_id') or ROOT_ID
108
inv = Inventory(root_id)
110
ie = self._unpack_entry(e)
111
if ie.parent_id == ROOT_ID:
112
ie.parent_id = root_id
117
def _unpack_entry(self, elt):
118
assert elt.tag == 'entry'
120
## original format inventories don't have a parent_id for
121
## nodes in the root directory, but it's cleaner to use one
123
parent_id = elt.get('parent_id')
124
if parent_id == None:
127
ie = InventoryEntry(elt.get('file_id'),
131
ie.text_id = elt.get('text_id')
132
ie.text_sha1 = elt.get('text_sha1')
134
## mutter("read inventoryentry: %r" % (elt.attrib))
136
v = elt.get('text_size')
137
ie.text_size = v and int(v)
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>
219
"""singleton instance"""
220
serializer_v4 = _Serializer_v4()