~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: Aaron Bentley
  • Date: 2006-08-08 06:47:16 UTC
  • mto: (1910.2.43 format-bumps)
  • mto: This revision was merged to the branch mainline in revision 1922.
  • Revision ID: aaron.bentley@utoronto.ca-20060808064716-75bc465292c2708f
Ensure root entry always has a revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
 
18
from bzrlib.xml_serializer import SubElement, Element, Serializer
19
19
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
20
20
import bzrlib.inventory as inventory
21
 
from bzrlib.revision import Revision        
 
21
from bzrlib.revision import Revision
22
22
from bzrlib.errors import BzrError
23
23
 
24
24
 
32
32
    
33
33
    def _pack_inventory(self, inv):
34
34
        """Convert to XML Element"""
 
35
        entries = inv.iter_entries()
35
36
        e = Element('inventory',
36
37
                    format='5')
37
38
        e.text = '\n'
38
 
        if inv.root.file_id not in (None, ROOT_ID):
39
 
            e.set('file_id', inv.root.file_id)
40
 
        for path, ie in inv.iter_entries():
 
39
        path, root = entries.next()
 
40
        if root.file_id not in (None, ROOT_ID):
 
41
            e.set('file_id', root.file_id)
 
42
        if inv.revision_id is not None:
 
43
            e.set('revision_id', inv.revision_id)
 
44
        for path, ie in entries:
41
45
            e.append(self._pack_entry(ie))
42
46
        return e
43
47
 
44
 
 
45
48
    def _pack_entry(self, ie):
46
49
        """Convert InventoryEntry to XML element"""
 
50
        # TODO: should just be a plain assertion
47
51
        if not InventoryEntry.versionable_kind(ie.kind):
48
52
            raise AssertionError('unsupported entry kind %s' % ie.kind)
49
53
        e = Element(ie.kind)
67
71
        if ie.parent_id != ROOT_ID:
68
72
            assert isinstance(ie.parent_id, basestring)
69
73
            e.set('parent_id', ie.parent_id)
70
 
 
71
74
        e.tail = '\n'
72
 
 
73
75
        return e
74
76
 
75
 
 
76
77
    def _pack_revision(self, rev):
77
78
        """Revision object -> xml tree"""
78
79
        root = Element('revision',
123
124
            if format != '5':
124
125
                raise BzrError("invalid format version %r on inventory"
125
126
                                % format)
126
 
        inv = Inventory(root_id)
 
127
        revision_id = elt.get('revision_id')
 
128
        inv = Inventory(root_id, revision_id=revision_id)
127
129
        for e in elt:
128
130
            ie = self._unpack_entry(e)
129
131
            if ie.parent_id == ROOT_ID:
199
201
            return
200
202
        for prop_elt in props_elt:
201
203
            assert prop_elt.tag == 'property', \
202
 
                "bad tag under properties list: %r" % p.tag
 
204
                "bad tag under properties list: %r" % prop_elt.tag
203
205
            name = prop_elt.get('name')
204
206
            value = prop_elt.text
 
207
            # If a property had an empty value ('') cElementTree reads
 
208
            # that back as None, convert it back to '', so that all
 
209
            # properties have string values
 
210
            if value is None:
 
211
                value = ''
205
212
            assert name not in rev.properties, \
206
 
                "repeated property %r" % p.name
 
213
                "repeated property %r" % name
207
214
            rev.properties[name] = value
208
215
 
209
216