~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: Robert Collins
  • Date: 2005-10-06 12:14:01 UTC
  • mfrom: (1393.1.67)
  • Revision ID: robertc@robertcollins.net-20051006121401-ce87bcb93909bbdf
merge martins latest

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
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
 
 
18
from bzrlib.xml import ElementTree, SubElement, Element, Serializer
 
19
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
 
20
import bzrlib.inventory as inventory
 
21
from bzrlib.revision import Revision        
 
22
from bzrlib.errors import BzrError
 
23
 
 
24
 
 
25
 
 
26
 
 
27
 
 
28
class Serializer_v5(Serializer):
 
29
    """Version 5 serializer
 
30
 
 
31
    Packs objects into XML and vice versa.
 
32
    """
 
33
    
 
34
    __slots__ = []
 
35
    
 
36
    def _pack_inventory(self, inv):
 
37
        """Convert to XML Element"""
 
38
        e = Element('inventory',
 
39
                    format='5')
 
40
        e.text = '\n'
 
41
        if inv.root.file_id not in (None, ROOT_ID):
 
42
            e.set('file_id', inv.root.file_id)
 
43
        for path, ie in inv.iter_entries():
 
44
            e.append(self._pack_entry(ie))
 
45
        return e
 
46
 
 
47
 
 
48
    def _pack_entry(self, ie):
 
49
        """Convert InventoryEntry to XML element"""
 
50
        if not InventoryEntry.versionable_kind(ie.kind):
 
51
            raise AssertionError('unsupported entry kind %s' % ie.kind)
 
52
        e = Element(ie.kind)
 
53
        e.set('name', ie.name)
 
54
        e.set('file_id', ie.file_id)
 
55
 
 
56
        if ie.text_size != None:
 
57
            e.set('text_size', '%d' % ie.text_size)
 
58
 
 
59
        for f in ['text_sha1', 'revision', 'symlink_target']:
 
60
            v = getattr(ie, f)
 
61
            if v != None:
 
62
                e.set(f, v)
 
63
 
 
64
        if ie.executable:
 
65
            e.set('executable', 'yes')
 
66
 
 
67
        # to be conservative, we don't externalize the root pointers
 
68
        # for now, leaving them as null in the xml form.  in a future
 
69
        # version it will be implied by nested elements.
 
70
        if ie.parent_id != ROOT_ID:
 
71
            assert isinstance(ie.parent_id, basestring)
 
72
            e.set('parent_id', ie.parent_id)
 
73
 
 
74
        e.tail = '\n'
 
75
 
 
76
        return e
 
77
 
 
78
 
 
79
    def _pack_revision(self, rev):
 
80
        """Revision object -> xml tree"""
 
81
        root = Element('revision',
 
82
                       committer = rev.committer,
 
83
                       timestamp = '%.9f' % rev.timestamp,
 
84
                       revision_id = rev.revision_id,
 
85
                       inventory_sha1 = rev.inventory_sha1,
 
86
                       format='5',
 
87
                       )
 
88
        if rev.timezone:
 
89
            root.set('timezone', str(rev.timezone))
 
90
        root.text = '\n'
 
91
        msg = SubElement(root, 'message')
 
92
        msg.text = rev.message
 
93
        msg.tail = '\n'
 
94
        if rev.parent_ids:
 
95
            pelts = SubElement(root, 'parents')
 
96
            pelts.tail = pelts.text = '\n'
 
97
            for parent_id in rev.parent_ids:
 
98
                assert isinstance(parent_id, basestring)
 
99
                p = SubElement(pelts, 'revision_ref')
 
100
                p.tail = '\n'
 
101
                p.set('revision_id', parent_id)
 
102
        return root
 
103
    
 
104
 
 
105
    def _unpack_inventory(self, elt):
 
106
        """Construct from XML Element
 
107
        """
 
108
        assert elt.tag == 'inventory'
 
109
        root_id = elt.get('file_id') or ROOT_ID
 
110
        format = elt.get('format')
 
111
        if format is not None:
 
112
            if format != '5':
 
113
                raise BzrError("invalid format version %r on inventory"
 
114
                                % format)
 
115
        inv = Inventory(root_id)
 
116
        for e in elt:
 
117
            ie = self._unpack_entry(e)
 
118
            if ie.parent_id == ROOT_ID:
 
119
                ie.parent_id = root_id
 
120
            inv.add(ie)
 
121
        return inv
 
122
 
 
123
 
 
124
    def _unpack_entry(self, elt):
 
125
        kind = elt.tag
 
126
        if not InventoryEntry.versionable_kind(kind):
 
127
            raise AssertionError('unsupported entry kind %s' % kind)
 
128
 
 
129
        parent_id = elt.get('parent_id')
 
130
        if parent_id == None:
 
131
            parent_id = ROOT_ID
 
132
 
 
133
        if kind == 'directory':
 
134
            ie = inventory.InventoryDirectory(elt.get('file_id'),
 
135
                                              elt.get('name'),
 
136
                                              parent_id)
 
137
        elif kind == 'file':
 
138
            ie = inventory.InventoryFile(elt.get('file_id'),
 
139
                                         elt.get('name'),
 
140
                                         parent_id)
 
141
            ie.text_sha1 = elt.get('text_sha1')
 
142
            if elt.get('executable') == 'yes':
 
143
                ie.executable = True
 
144
            v = elt.get('text_size')
 
145
            ie.text_size = v and int(v)
 
146
        elif kind == 'symlink':
 
147
            ie = inventory.InventoryLink(elt.get('file_id'),
 
148
                                         elt.get('name'),
 
149
                                         parent_id)
 
150
            ie.symlink_target = elt.get('symlink_target')
 
151
        else:
 
152
            raise BzrError("unknown kind %r" % kind)
 
153
        ie.revision = elt.get('revision')
 
154
 
 
155
        return ie
 
156
 
 
157
 
 
158
    def _unpack_revision(self, elt):
 
159
        """XML Element -> Revision object"""
 
160
        assert elt.tag == 'revision'
 
161
        format = elt.get('format')
 
162
        if format is not None:
 
163
            if format != '5':
 
164
                raise BzrError("invalid format version %r on inventory"
 
165
                                % format)
 
166
        rev = Revision(committer = elt.get('committer'),
 
167
                       timestamp = float(elt.get('timestamp')),
 
168
                       revision_id = elt.get('revision_id'),
 
169
                       inventory_sha1 = elt.get('inventory_sha1')
 
170
                       )
 
171
        parents = elt.find('parents') or []
 
172
        for p in parents:
 
173
            assert p.tag == 'revision_ref', \
 
174
                   "bad parent node tag %r" % p.tag
 
175
            rev.parent_ids.append(p.get('revision_id'))
 
176
 
 
177
        v = elt.get('timezone')
 
178
        rev.timezone = v and int(v)
 
179
 
 
180
        rev.message = elt.findtext('message') # text of <message>
 
181
        return rev
 
182
 
 
183
 
 
184
serializer_v5 = Serializer_v5()