/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 18:38:57 UTC
  • mfrom: (1934 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1937.
  • Revision ID: john@arbash-meinel.com-20060816183857-7307edffa7098bd2
[merge] bzr.dev 1934

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
1
3
# This program is free software; you can redistribute it and/or modify
2
4
# it under the terms of the GNU General Public License as published by
3
5
# the Free Software Foundation; either version 2 of the License, or
4
6
# (at your option) any later version.
5
 
 
 
7
#
6
8
# This program is distributed in the hope that it will be useful,
7
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9
11
# GNU General Public License for more details.
10
 
 
 
12
#
11
13
# You should have received a copy of the GNU General Public License
12
14
# along with this program; if not, write to the Free Software
13
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
16
 
15
17
 
16
 
from bzrlib.xml_serializer import ElementTree, SubElement, Element, Serializer
 
18
from bzrlib import (
 
19
    cache_utf8,
 
20
    )
 
21
from bzrlib.xml_serializer import SubElement, Element, Serializer
17
22
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
18
23
import bzrlib.inventory as inventory
19
 
from bzrlib.revision import Revision        
 
24
from bzrlib.revision import Revision
20
25
from bzrlib.errors import BzrError
21
26
 
22
27
 
30
35
    
31
36
    def _pack_inventory(self, inv):
32
37
        """Convert to XML Element"""
 
38
        entries = inv.iter_entries()
33
39
        e = Element('inventory',
34
40
                    format='5')
35
41
        e.text = '\n'
36
 
        if inv.root.file_id not in (None, ROOT_ID):
37
 
            e.set('file_id', inv.root.file_id)
 
42
        path, root = entries.next()
 
43
        if root.file_id not in (None, ROOT_ID):
 
44
            e.set('file_id', root.file_id)
38
45
        if inv.revision_id is not None:
39
46
            e.set('revision_id', inv.revision_id)
40
 
        for path, ie in inv.iter_entries():
 
47
        for path, ie in entries:
41
48
            e.append(self._pack_entry(ie))
42
49
        return e
43
50
 
44
 
 
45
51
    def _pack_entry(self, ie):
46
52
        """Convert InventoryEntry to XML element"""
 
53
        # TODO: should just be a plain assertion
47
54
        if not InventoryEntry.versionable_kind(ie.kind):
48
55
            raise AssertionError('unsupported entry kind %s' % ie.kind)
49
56
        e = Element(ie.kind)
67
74
        if ie.parent_id != ROOT_ID:
68
75
            assert isinstance(ie.parent_id, basestring)
69
76
            e.set('parent_id', ie.parent_id)
70
 
 
71
77
        e.tail = '\n'
72
 
 
73
78
        return e
74
79
 
75
 
 
76
80
    def _pack_revision(self, rev):
77
81
        """Revision object -> xml tree"""
78
82
        root = Element('revision',
82
86
                       inventory_sha1 = rev.inventory_sha1,
83
87
                       format='5',
84
88
                       )
85
 
        if rev.timezone:
 
89
        if rev.timezone is not None:
86
90
            root.set('timezone', str(rev.timezone))
87
91
        root.text = '\n'
88
92
        msg = SubElement(root, 'message')
124
128
                raise BzrError("invalid format version %r on inventory"
125
129
                                % format)
126
130
        revision_id = elt.get('revision_id')
 
131
        if revision_id is not None:
 
132
            revision_id = cache_utf8.get_cached_unicode(revision_id)
127
133
        inv = Inventory(root_id, revision_id=revision_id)
128
134
        for e in elt:
129
135
            ie = self._unpack_entry(e)
138
144
        if not InventoryEntry.versionable_kind(kind):
139
145
            raise AssertionError('unsupported entry kind %s' % kind)
140
146
 
 
147
        get_cached = cache_utf8.get_cached_unicode
 
148
 
141
149
        parent_id = elt.get('parent_id')
142
150
        if parent_id == None:
143
151
            parent_id = ROOT_ID
 
152
        parent_id = get_cached(parent_id)
 
153
        file_id = get_cached(elt.get('file_id'))
144
154
 
145
155
        if kind == 'directory':
146
 
            ie = inventory.InventoryDirectory(elt.get('file_id'),
 
156
            ie = inventory.InventoryDirectory(file_id,
147
157
                                              elt.get('name'),
148
158
                                              parent_id)
149
159
        elif kind == 'file':
150
 
            ie = inventory.InventoryFile(elt.get('file_id'),
 
160
            ie = inventory.InventoryFile(file_id,
151
161
                                         elt.get('name'),
152
162
                                         parent_id)
153
163
            ie.text_sha1 = elt.get('text_sha1')
156
166
            v = elt.get('text_size')
157
167
            ie.text_size = v and int(v)
158
168
        elif kind == 'symlink':
159
 
            ie = inventory.InventoryLink(elt.get('file_id'),
 
169
            ie = inventory.InventoryLink(file_id,
160
170
                                         elt.get('name'),
161
171
                                         parent_id)
162
172
            ie.symlink_target = elt.get('symlink_target')
163
173
        else:
164
174
            raise BzrError("unknown kind %r" % kind)
165
 
        ie.revision = elt.get('revision')
 
175
        revision = elt.get('revision')
 
176
        if revision is not None:
 
177
            revision = get_cached(revision)
 
178
        ie.revision = revision
166
179
 
167
180
        return ie
168
181
 
175
188
            if format != '5':
176
189
                raise BzrError("invalid format version %r on inventory"
177
190
                                % format)
 
191
        get_cached = cache_utf8.get_cached_unicode
178
192
        rev = Revision(committer = elt.get('committer'),
179
193
                       timestamp = float(elt.get('timestamp')),
180
 
                       revision_id = elt.get('revision_id'),
 
194
                       revision_id = get_cached(elt.get('revision_id')),
181
195
                       inventory_sha1 = elt.get('inventory_sha1')
182
196
                       )
183
197
        parents = elt.find('parents') or []
184
198
        for p in parents:
185
199
            assert p.tag == 'revision_ref', \
186
200
                   "bad parent node tag %r" % p.tag
187
 
            rev.parent_ids.append(p.get('revision_id'))
 
201
            rev.parent_ids.append(get_cached(p.get('revision_id')))
188
202
        self._unpack_revision_properties(elt, rev)
189
203
        v = elt.get('timezone')
190
 
        rev.timezone = v and int(v)
 
204
        if v is None:
 
205
            rev.timezone = 0
 
206
        else:
 
207
            rev.timezone = int(v)
191
208
        rev.message = elt.findtext('message') # text of <message>
192
209
        return rev
193
210
 
200
217
            return
201
218
        for prop_elt in props_elt:
202
219
            assert prop_elt.tag == 'property', \
203
 
                "bad tag under properties list: %r" % p.tag
 
220
                "bad tag under properties list: %r" % prop_elt.tag
204
221
            name = prop_elt.get('name')
205
222
            value = prop_elt.text
 
223
            # If a property had an empty value ('') cElementTree reads
 
224
            # that back as None, convert it back to '', so that all
 
225
            # properties have string values
 
226
            if value is None:
 
227
                value = ''
206
228
            assert name not in rev.properties, \
207
 
                "repeated property %r" % p.name
 
229
                "repeated property %r" % name
208
230
            rev.properties[name] = value
209
231
 
210
232