/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: Michael Ellerman
  • Date: 2006-03-09 00:24:48 UTC
  • mto: (1610.1.8 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: michael@ellerman.id.au-20060309002448-70cce15e3d605130
Make the "ignore line" in the commit message editor the "right" width, so
that if you make your message that wide it won't wrap in bzr log output.
Just as a visual aid.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is free software; you can redistribute it and/or modify
 
2
# it under the terms of the GNU General Public License as published by
 
3
# the Free Software Foundation; either version 2 of the License, or
 
4
# (at your option) any later version.
 
5
 
 
6
# This program is distributed in the hope that it will be useful,
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
# GNU General Public License for more details.
 
10
 
 
11
# You should have received a copy of the GNU General Public License
 
12
# along with this program; if not, write to the Free Software
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
 
 
15
 
 
16
from bzrlib.xml_serializer import ElementTree, SubElement, Element, Serializer
 
17
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
 
18
import bzrlib.inventory as inventory
 
19
from bzrlib.revision import Revision        
 
20
from bzrlib.errors import BzrError
 
21
 
 
22
 
 
23
class Serializer_v5(Serializer):
 
24
    """Version 5 serializer
 
25
 
 
26
    Packs objects into XML and vice versa.
 
27
    """
 
28
    
 
29
    __slots__ = []
 
30
    
 
31
    def _pack_inventory(self, inv):
 
32
        """Convert to XML Element"""
 
33
        e = Element('inventory',
 
34
                    format='5')
 
35
        e.text = '\n'
 
36
        if inv.root.file_id not in (None, ROOT_ID):
 
37
            e.set('file_id', inv.root.file_id)
 
38
        for path, ie in inv.iter_entries():
 
39
            e.append(self._pack_entry(ie))
 
40
        return e
 
41
 
 
42
 
 
43
    def _pack_entry(self, ie):
 
44
        """Convert InventoryEntry to XML element"""
 
45
        if not InventoryEntry.versionable_kind(ie.kind):
 
46
            raise AssertionError('unsupported entry kind %s' % ie.kind)
 
47
        e = Element(ie.kind)
 
48
        e.set('name', ie.name)
 
49
        e.set('file_id', ie.file_id)
 
50
 
 
51
        if ie.text_size != None:
 
52
            e.set('text_size', '%d' % ie.text_size)
 
53
 
 
54
        for f in ['text_sha1', 'revision', 'symlink_target']:
 
55
            v = getattr(ie, f)
 
56
            if v != None:
 
57
                e.set(f, v)
 
58
 
 
59
        if ie.executable:
 
60
            e.set('executable', 'yes')
 
61
 
 
62
        # to be conservative, we don't externalize the root pointers
 
63
        # for now, leaving them as null in the xml form.  in a future
 
64
        # version it will be implied by nested elements.
 
65
        if ie.parent_id != ROOT_ID:
 
66
            assert isinstance(ie.parent_id, basestring)
 
67
            e.set('parent_id', ie.parent_id)
 
68
 
 
69
        e.tail = '\n'
 
70
 
 
71
        return e
 
72
 
 
73
 
 
74
    def _pack_revision(self, rev):
 
75
        """Revision object -> xml tree"""
 
76
        root = Element('revision',
 
77
                       committer = rev.committer,
 
78
                       timestamp = '%.9f' % rev.timestamp,
 
79
                       revision_id = rev.revision_id,
 
80
                       inventory_sha1 = rev.inventory_sha1,
 
81
                       format='5',
 
82
                       )
 
83
        if rev.timezone:
 
84
            root.set('timezone', str(rev.timezone))
 
85
        root.text = '\n'
 
86
        msg = SubElement(root, 'message')
 
87
        msg.text = rev.message
 
88
        msg.tail = '\n'
 
89
        if rev.parent_ids:
 
90
            pelts = SubElement(root, 'parents')
 
91
            pelts.tail = pelts.text = '\n'
 
92
            for parent_id in rev.parent_ids:
 
93
                assert isinstance(parent_id, basestring)
 
94
                p = SubElement(pelts, 'revision_ref')
 
95
                p.tail = '\n'
 
96
                p.set('revision_id', parent_id)
 
97
        if rev.properties:
 
98
            self._pack_revision_properties(rev, root)
 
99
        return root
 
100
 
 
101
 
 
102
    def _pack_revision_properties(self, rev, under_element):
 
103
        top_elt = SubElement(under_element, 'properties')
 
104
        for prop_name, prop_value in sorted(rev.properties.items()):
 
105
            assert isinstance(prop_name, basestring) 
 
106
            assert isinstance(prop_value, basestring) 
 
107
            prop_elt = SubElement(top_elt, 'property')
 
108
            prop_elt.set('name', prop_name)
 
109
            prop_elt.text = prop_value
 
110
            prop_elt.tail = '\n'
 
111
        top_elt.tail = '\n'
 
112
 
 
113
 
 
114
    def _unpack_inventory(self, elt):
 
115
        """Construct from XML Element
 
116
        """
 
117
        assert elt.tag == 'inventory'
 
118
        root_id = elt.get('file_id') or ROOT_ID
 
119
        format = elt.get('format')
 
120
        if format is not None:
 
121
            if format != '5':
 
122
                raise BzrError("invalid format version %r on inventory"
 
123
                                % format)
 
124
        inv = Inventory(root_id)
 
125
        for e in elt:
 
126
            ie = self._unpack_entry(e)
 
127
            if ie.parent_id == ROOT_ID:
 
128
                ie.parent_id = root_id
 
129
            inv.add(ie)
 
130
        return inv
 
131
 
 
132
 
 
133
    def _unpack_entry(self, elt):
 
134
        kind = elt.tag
 
135
        if not InventoryEntry.versionable_kind(kind):
 
136
            raise AssertionError('unsupported entry kind %s' % kind)
 
137
 
 
138
        parent_id = elt.get('parent_id')
 
139
        if parent_id == None:
 
140
            parent_id = ROOT_ID
 
141
 
 
142
        if kind == 'directory':
 
143
            ie = inventory.InventoryDirectory(elt.get('file_id'),
 
144
                                              elt.get('name'),
 
145
                                              parent_id)
 
146
        elif kind == 'file':
 
147
            ie = inventory.InventoryFile(elt.get('file_id'),
 
148
                                         elt.get('name'),
 
149
                                         parent_id)
 
150
            ie.text_sha1 = elt.get('text_sha1')
 
151
            if elt.get('executable') == 'yes':
 
152
                ie.executable = True
 
153
            v = elt.get('text_size')
 
154
            ie.text_size = v and int(v)
 
155
        elif kind == 'symlink':
 
156
            ie = inventory.InventoryLink(elt.get('file_id'),
 
157
                                         elt.get('name'),
 
158
                                         parent_id)
 
159
            ie.symlink_target = elt.get('symlink_target')
 
160
        else:
 
161
            raise BzrError("unknown kind %r" % kind)
 
162
        ie.revision = elt.get('revision')
 
163
 
 
164
        return ie
 
165
 
 
166
 
 
167
    def _unpack_revision(self, elt):
 
168
        """XML Element -> Revision object"""
 
169
        assert elt.tag == 'revision'
 
170
        format = elt.get('format')
 
171
        if format is not None:
 
172
            if format != '5':
 
173
                raise BzrError("invalid format version %r on inventory"
 
174
                                % format)
 
175
        rev = Revision(committer = elt.get('committer'),
 
176
                       timestamp = float(elt.get('timestamp')),
 
177
                       revision_id = elt.get('revision_id'),
 
178
                       inventory_sha1 = elt.get('inventory_sha1')
 
179
                       )
 
180
        parents = elt.find('parents') or []
 
181
        for p in parents:
 
182
            assert p.tag == 'revision_ref', \
 
183
                   "bad parent node tag %r" % p.tag
 
184
            rev.parent_ids.append(p.get('revision_id'))
 
185
        self._unpack_revision_properties(elt, rev)
 
186
        v = elt.get('timezone')
 
187
        rev.timezone = v and int(v)
 
188
        rev.message = elt.findtext('message') # text of <message>
 
189
        return rev
 
190
 
 
191
 
 
192
    def _unpack_revision_properties(self, elt, rev):
 
193
        """Unpack properties onto a revision."""
 
194
        props_elt = elt.find('properties')
 
195
        assert len(rev.properties) == 0
 
196
        if not props_elt:
 
197
            return
 
198
        for prop_elt in props_elt:
 
199
            assert prop_elt.tag == 'property', \
 
200
                "bad tag under properties list: %r" % p.tag
 
201
            name = prop_elt.get('name')
 
202
            value = prop_elt.text
 
203
            assert name not in rev.properties, \
 
204
                "repeated property %r" % p.name
 
205
            rev.properties[name] = value
 
206
 
 
207
 
 
208
serializer_v5 = Serializer_v5()