/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-16 20:55:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1942.
  • Revision ID: john@arbash-meinel.com-20060816205523-b8df51cb552b5a7e
[merge] robert's custom XML serializer, and cleanup for benchmarks and iter_entries() differences

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import cStringIO
 
18
 
 
19
from bzrlib import (
 
20
    cache_utf8,
 
21
    inventory,
 
22
    )
 
23
from bzrlib.xml_serializer import SubElement, Element, Serializer
 
24
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
 
25
from bzrlib.revision import Revision
 
26
from bzrlib.errors import BzrError
 
27
 
 
28
 
 
29
class Serializer_v5(Serializer):
 
30
    """Version 5 serializer
 
31
 
 
32
    Packs objects into XML and vice versa.
 
33
    """
 
34
    
 
35
    __slots__ = ['_utf8_re']
 
36
 
 
37
    def __init__(self):
 
38
        self._utf8_re = None
 
39
    
 
40
    def write_inventory_to_string(self, inv):
 
41
        sio = cStringIO.StringIO()
 
42
        self.write_inventory(inv, sio)
 
43
        return sio.getvalue()
 
44
 
 
45
    def write_inventory(self, inv, f):
 
46
        """Write inventory to a file.
 
47
        
 
48
        :param inv: the inventory to write.
 
49
        :param f: the file to write.
 
50
        """
 
51
        output = []
 
52
        self._append_inventory_root(output, inv)
 
53
        entries = inv.iter_entries()
 
54
        root_path, root_ie = entries.next()
 
55
        for path, ie in entries:
 
56
            self._append_entry(output, ie)
 
57
        f.write(''.join(output))
 
58
#        elt = self._pack_inventory(inv)
 
59
#        for child in elt.getchildren():
 
60
#            if isinstance(child, inventory.InventoryDirectory):
 
61
#                print "foo\nbar\n"
 
62
#            print child
 
63
#            ElementTree(child).write(f, 'utf-8')
 
64
        f.write('</inventory>\n')
 
65
 
 
66
    def _append_inventory_root(self, output, inv):
 
67
        """Append the inventory root to output."""
 
68
        output.append('<inventory')
 
69
        if inv.root.file_id not in (None, ROOT_ID):
 
70
            output.append(' file_id="')
 
71
            self._append_utf8_escaped(output, inv.root.file_id)
 
72
        output.append(' format="5"')
 
73
        if inv.revision_id is not None:
 
74
            output.append(' revision_id="')
 
75
            self._append_utf8_escaped(output, inv.revision_id)
 
76
        output.append('>\n')
 
77
        
 
78
    def _append_entry(self, output, ie):
 
79
        """Convert InventoryEntry to XML element and append to output."""
 
80
        # TODO: should just be a plain assertion
 
81
        assert InventoryEntry.versionable_kind(ie.kind), \
 
82
            'unsupported entry kind %s' % ie.kind
 
83
 
 
84
        output.append("<")
 
85
        output.append(ie.kind)
 
86
        if ie.executable:
 
87
            output.append(' executable="yes"')
 
88
        output.append(' file_id="')
 
89
        self._append_utf8_escaped(output, ie.file_id)
 
90
        output.append(' name="')
 
91
        self._append_utf8_escaped(output, ie.name)
 
92
        if ie.parent_id != ROOT_ID:
 
93
            assert isinstance(ie.parent_id, basestring)
 
94
            output.append(' parent_id="')
 
95
            self._append_utf8_escaped(output, ie.parent_id)
 
96
        if ie.revision is not None:
 
97
            output.append(' revision="')
 
98
            self._append_utf8_escaped(output, ie.revision)
 
99
        if ie.symlink_target is not None:
 
100
            output.append(' symlink_target="')
 
101
            self._append_utf8_escaped(output, ie.symlink_target)
 
102
        if ie.text_sha1 is not None:
 
103
            output.append(' text_size="')
 
104
            output.append(ie.text_sha1)
 
105
            output.append('"')
 
106
        if ie.text_size is not None:
 
107
            output.append(' text_size="%d"' % ie.text_size)
 
108
        output.append(" />\n")
 
109
        return
 
110
 
 
111
    def _append_utf8_escaped(self, output, a_string):
 
112
        """Append a_string to output as utf8."""
 
113
        if self._utf8_re is None:
 
114
            import re
 
115
            self._utf8_re = re.compile("[&'\"<>]")
 
116
        # escape attribute value
 
117
        text = a_string.encode('utf8')
 
118
        output.append(self._utf8_re.sub(self._utf8_escape_replace, text))
 
119
        output.append('"')
 
120
 
 
121
    _utf8_escape_map = {
 
122
        "&":'&amp;',
 
123
        "'":"&apos;", # FIXME: overkill
 
124
        "\"":"&quot;",
 
125
        "<":"&lt;",
 
126
        ">":"&gt;",
 
127
        }
 
128
    def _utf8_escape_replace(self, match, map=_utf8_escape_map):
 
129
        return map[match.group()]
 
130
 
 
131
    def _pack_inventory(self, inv):
 
132
        """Convert to XML Element"""
 
133
        entries = inv.iter_entries()
 
134
        e = Element('inventory',
 
135
                    format='5')
 
136
        e.text = '\n'
 
137
        path, root = entries.next()
 
138
        if root.file_id not in (None, ROOT_ID):
 
139
            e.set('file_id', root.file_id)
 
140
        if inv.revision_id is not None:
 
141
            e.set('revision_id', inv.revision_id)
 
142
        for path, ie in entries:
 
143
            e.append(self._pack_entry(ie))
 
144
        return e
 
145
 
 
146
    def _pack_entry(self, ie):
 
147
        """Convert InventoryEntry to XML element"""
 
148
        # TODO: should just be a plain assertion
 
149
        if not InventoryEntry.versionable_kind(ie.kind):
 
150
            raise AssertionError('unsupported entry kind %s' % ie.kind)
 
151
        e = Element(ie.kind)
 
152
        e.set('name', ie.name)
 
153
        e.set('file_id', ie.file_id)
 
154
 
 
155
        if ie.text_size != None:
 
156
            e.set('text_size', '%d' % ie.text_size)
 
157
 
 
158
        for f in ['text_sha1', 'revision', 'symlink_target']:
 
159
            v = getattr(ie, f)
 
160
            if v != None:
 
161
                e.set(f, v)
 
162
 
 
163
        if ie.executable:
 
164
            e.set('executable', 'yes')
 
165
 
 
166
        # to be conservative, we don't externalize the root pointers
 
167
        # for now, leaving them as null in the xml form.  in a future
 
168
        # version it will be implied by nested elements.
 
169
        if ie.parent_id != ROOT_ID:
 
170
            assert isinstance(ie.parent_id, basestring)
 
171
            e.set('parent_id', ie.parent_id)
 
172
        e.tail = '\n'
 
173
        return e
 
174
 
 
175
    def _pack_revision(self, rev):
 
176
        """Revision object -> xml tree"""
 
177
        root = Element('revision',
 
178
                       committer = rev.committer,
 
179
                       timestamp = '%.9f' % rev.timestamp,
 
180
                       revision_id = rev.revision_id,
 
181
                       inventory_sha1 = rev.inventory_sha1,
 
182
                       format='5',
 
183
                       )
 
184
        if rev.timezone is not None:
 
185
            root.set('timezone', str(rev.timezone))
 
186
        root.text = '\n'
 
187
        msg = SubElement(root, 'message')
 
188
        msg.text = rev.message
 
189
        msg.tail = '\n'
 
190
        if rev.parent_ids:
 
191
            pelts = SubElement(root, 'parents')
 
192
            pelts.tail = pelts.text = '\n'
 
193
            for parent_id in rev.parent_ids:
 
194
                assert isinstance(parent_id, basestring)
 
195
                p = SubElement(pelts, 'revision_ref')
 
196
                p.tail = '\n'
 
197
                p.set('revision_id', parent_id)
 
198
        if rev.properties:
 
199
            self._pack_revision_properties(rev, root)
 
200
        return root
 
201
 
 
202
 
 
203
    def _pack_revision_properties(self, rev, under_element):
 
204
        top_elt = SubElement(under_element, 'properties')
 
205
        for prop_name, prop_value in sorted(rev.properties.items()):
 
206
            assert isinstance(prop_name, basestring) 
 
207
            assert isinstance(prop_value, basestring) 
 
208
            prop_elt = SubElement(top_elt, 'property')
 
209
            prop_elt.set('name', prop_name)
 
210
            prop_elt.text = prop_value
 
211
            prop_elt.tail = '\n'
 
212
        top_elt.tail = '\n'
 
213
 
 
214
 
 
215
    def _unpack_inventory(self, elt):
 
216
        """Construct from XML Element
 
217
        """
 
218
        assert elt.tag == 'inventory'
 
219
        root_id = elt.get('file_id') or ROOT_ID
 
220
        format = elt.get('format')
 
221
        if format is not None:
 
222
            if format != '5':
 
223
                raise BzrError("invalid format version %r on inventory"
 
224
                                % format)
 
225
        revision_id = elt.get('revision_id')
 
226
        if revision_id is not None:
 
227
            revision_id = cache_utf8.get_cached_unicode(revision_id)
 
228
        inv = Inventory(root_id, revision_id=revision_id)
 
229
        for e in elt:
 
230
            ie = self._unpack_entry(e)
 
231
            if ie.parent_id == ROOT_ID:
 
232
                ie.parent_id = root_id
 
233
            inv.add(ie)
 
234
        return inv
 
235
 
 
236
 
 
237
    def _unpack_entry(self, elt):
 
238
        kind = elt.tag
 
239
        if not InventoryEntry.versionable_kind(kind):
 
240
            raise AssertionError('unsupported entry kind %s' % kind)
 
241
 
 
242
        get_cached = cache_utf8.get_cached_unicode
 
243
 
 
244
        parent_id = elt.get('parent_id')
 
245
        if parent_id == None:
 
246
            parent_id = ROOT_ID
 
247
        parent_id = get_cached(parent_id)
 
248
        file_id = get_cached(elt.get('file_id'))
 
249
 
 
250
        if kind == 'directory':
 
251
            ie = inventory.InventoryDirectory(file_id,
 
252
                                              elt.get('name'),
 
253
                                              parent_id)
 
254
        elif kind == 'file':
 
255
            ie = inventory.InventoryFile(file_id,
 
256
                                         elt.get('name'),
 
257
                                         parent_id)
 
258
            ie.text_sha1 = elt.get('text_sha1')
 
259
            if elt.get('executable') == 'yes':
 
260
                ie.executable = True
 
261
            v = elt.get('text_size')
 
262
            ie.text_size = v and int(v)
 
263
        elif kind == 'symlink':
 
264
            ie = inventory.InventoryLink(file_id,
 
265
                                         elt.get('name'),
 
266
                                         parent_id)
 
267
            ie.symlink_target = elt.get('symlink_target')
 
268
        else:
 
269
            raise BzrError("unknown kind %r" % kind)
 
270
        revision = elt.get('revision')
 
271
        if revision is not None:
 
272
            revision = get_cached(revision)
 
273
        ie.revision = revision
 
274
 
 
275
        return ie
 
276
 
 
277
 
 
278
    def _unpack_revision(self, elt):
 
279
        """XML Element -> Revision object"""
 
280
        assert elt.tag == 'revision'
 
281
        format = elt.get('format')
 
282
        if format is not None:
 
283
            if format != '5':
 
284
                raise BzrError("invalid format version %r on inventory"
 
285
                                % format)
 
286
        get_cached = cache_utf8.get_cached_unicode
 
287
        rev = Revision(committer = elt.get('committer'),
 
288
                       timestamp = float(elt.get('timestamp')),
 
289
                       revision_id = get_cached(elt.get('revision_id')),
 
290
                       inventory_sha1 = elt.get('inventory_sha1')
 
291
                       )
 
292
        parents = elt.find('parents') or []
 
293
        for p in parents:
 
294
            assert p.tag == 'revision_ref', \
 
295
                   "bad parent node tag %r" % p.tag
 
296
            rev.parent_ids.append(get_cached(p.get('revision_id')))
 
297
        self._unpack_revision_properties(elt, rev)
 
298
        v = elt.get('timezone')
 
299
        if v is None:
 
300
            rev.timezone = 0
 
301
        else:
 
302
            rev.timezone = int(v)
 
303
        rev.message = elt.findtext('message') # text of <message>
 
304
        return rev
 
305
 
 
306
 
 
307
    def _unpack_revision_properties(self, elt, rev):
 
308
        """Unpack properties onto a revision."""
 
309
        props_elt = elt.find('properties')
 
310
        assert len(rev.properties) == 0
 
311
        if not props_elt:
 
312
            return
 
313
        for prop_elt in props_elt:
 
314
            assert prop_elt.tag == 'property', \
 
315
                "bad tag under properties list: %r" % prop_elt.tag
 
316
            name = prop_elt.get('name')
 
317
            value = prop_elt.text
 
318
            # If a property had an empty value ('') cElementTree reads
 
319
            # that back as None, convert it back to '', so that all
 
320
            # properties have string values
 
321
            if value is None:
 
322
                value = ''
 
323
            assert name not in rev.properties, \
 
324
                "repeated property %r" % name
 
325
            rev.properties[name] = value
 
326
 
 
327
 
 
328
serializer_v5 = Serializer_v5()