~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: Alexander Belchenko
  • Date: 2006-08-17 12:14:57 UTC
  • mto: (1711.2.128 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1939.
  • Revision ID: bialix@ukr.net-20060817121457-44e8499df346c8bb
New target to produce html docs to upload on server

- 'make htmldocs' translate docs to html 
  and copy documentation to folder html_docs
  (can be tuned by variable HTMLDIR inside Makefile)
- 'make clean-docs' remove generated html files
- provide consistency in make targets
- added dependencies for bzr_man.txt

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
 
 
18
from bzrlib import (
 
19
    cache_utf8,
 
20
    )
 
21
from bzrlib.xml_serializer import SubElement, Element, Serializer
 
22
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
 
23
import bzrlib.inventory as inventory
 
24
from bzrlib.revision import Revision
 
25
from bzrlib.errors import BzrError
 
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
        entries = inv.iter_entries()
 
39
        e = Element('inventory',
 
40
                    format='5')
 
41
        e.text = '\n'
 
42
        path, root = entries.next()
 
43
        if root.file_id not in (None, ROOT_ID):
 
44
            e.set('file_id', root.file_id)
 
45
        if inv.revision_id is not None:
 
46
            e.set('revision_id', inv.revision_id)
 
47
        for path, ie in entries:
 
48
            e.append(self._pack_entry(ie))
 
49
        return e
 
50
 
 
51
    def _pack_entry(self, ie):
 
52
        """Convert InventoryEntry to XML element"""
 
53
        # TODO: should just be a plain assertion
 
54
        if not InventoryEntry.versionable_kind(ie.kind):
 
55
            raise AssertionError('unsupported entry kind %s' % ie.kind)
 
56
        e = Element(ie.kind)
 
57
        e.set('name', ie.name)
 
58
        e.set('file_id', ie.file_id)
 
59
 
 
60
        if ie.text_size != None:
 
61
            e.set('text_size', '%d' % ie.text_size)
 
62
 
 
63
        for f in ['text_sha1', 'revision', 'symlink_target']:
 
64
            v = getattr(ie, f)
 
65
            if v != None:
 
66
                e.set(f, v)
 
67
 
 
68
        if ie.executable:
 
69
            e.set('executable', 'yes')
 
70
 
 
71
        # to be conservative, we don't externalize the root pointers
 
72
        # for now, leaving them as null in the xml form.  in a future
 
73
        # version it will be implied by nested elements.
 
74
        if ie.parent_id != ROOT_ID:
 
75
            assert isinstance(ie.parent_id, basestring)
 
76
            e.set('parent_id', ie.parent_id)
 
77
        e.tail = '\n'
 
78
        return e
 
79
 
 
80
    def _pack_revision(self, rev):
 
81
        """Revision object -> xml tree"""
 
82
        root = Element('revision',
 
83
                       committer = rev.committer,
 
84
                       timestamp = '%.9f' % rev.timestamp,
 
85
                       revision_id = rev.revision_id,
 
86
                       inventory_sha1 = rev.inventory_sha1,
 
87
                       format='5',
 
88
                       )
 
89
        if rev.timezone:
 
90
            root.set('timezone', str(rev.timezone))
 
91
        root.text = '\n'
 
92
        msg = SubElement(root, 'message')
 
93
        msg.text = rev.message
 
94
        msg.tail = '\n'
 
95
        if rev.parent_ids:
 
96
            pelts = SubElement(root, 'parents')
 
97
            pelts.tail = pelts.text = '\n'
 
98
            for parent_id in rev.parent_ids:
 
99
                assert isinstance(parent_id, basestring)
 
100
                p = SubElement(pelts, 'revision_ref')
 
101
                p.tail = '\n'
 
102
                p.set('revision_id', parent_id)
 
103
        if rev.properties:
 
104
            self._pack_revision_properties(rev, root)
 
105
        return root
 
106
 
 
107
 
 
108
    def _pack_revision_properties(self, rev, under_element):
 
109
        top_elt = SubElement(under_element, 'properties')
 
110
        for prop_name, prop_value in sorted(rev.properties.items()):
 
111
            assert isinstance(prop_name, basestring) 
 
112
            assert isinstance(prop_value, basestring) 
 
113
            prop_elt = SubElement(top_elt, 'property')
 
114
            prop_elt.set('name', prop_name)
 
115
            prop_elt.text = prop_value
 
116
            prop_elt.tail = '\n'
 
117
        top_elt.tail = '\n'
 
118
 
 
119
 
 
120
    def _unpack_inventory(self, elt):
 
121
        """Construct from XML Element
 
122
        """
 
123
        assert elt.tag == 'inventory'
 
124
        root_id = elt.get('file_id') or ROOT_ID
 
125
        format = elt.get('format')
 
126
        if format is not None:
 
127
            if format != '5':
 
128
                raise BzrError("invalid format version %r on inventory"
 
129
                                % format)
 
130
        revision_id = elt.get('revision_id')
 
131
        if revision_id is not None:
 
132
            revision_id = cache_utf8.get_cached_unicode(revision_id)
 
133
        inv = Inventory(root_id, revision_id=revision_id)
 
134
        for e in elt:
 
135
            ie = self._unpack_entry(e)
 
136
            if ie.parent_id == ROOT_ID:
 
137
                ie.parent_id = root_id
 
138
            inv.add(ie)
 
139
        return inv
 
140
 
 
141
 
 
142
    def _unpack_entry(self, elt):
 
143
        kind = elt.tag
 
144
        if not InventoryEntry.versionable_kind(kind):
 
145
            raise AssertionError('unsupported entry kind %s' % kind)
 
146
 
 
147
        get_cached = cache_utf8.get_cached_unicode
 
148
 
 
149
        parent_id = elt.get('parent_id')
 
150
        if parent_id == None:
 
151
            parent_id = ROOT_ID
 
152
        parent_id = get_cached(parent_id)
 
153
        file_id = get_cached(elt.get('file_id'))
 
154
 
 
155
        if kind == 'directory':
 
156
            ie = inventory.InventoryDirectory(file_id,
 
157
                                              elt.get('name'),
 
158
                                              parent_id)
 
159
        elif kind == 'file':
 
160
            ie = inventory.InventoryFile(file_id,
 
161
                                         elt.get('name'),
 
162
                                         parent_id)
 
163
            ie.text_sha1 = elt.get('text_sha1')
 
164
            if elt.get('executable') == 'yes':
 
165
                ie.executable = True
 
166
            v = elt.get('text_size')
 
167
            ie.text_size = v and int(v)
 
168
        elif kind == 'symlink':
 
169
            ie = inventory.InventoryLink(file_id,
 
170
                                         elt.get('name'),
 
171
                                         parent_id)
 
172
            ie.symlink_target = elt.get('symlink_target')
 
173
        else:
 
174
            raise BzrError("unknown kind %r" % kind)
 
175
        revision = elt.get('revision')
 
176
        if revision is not None:
 
177
            revision = get_cached(revision)
 
178
        ie.revision = revision
 
179
 
 
180
        return ie
 
181
 
 
182
 
 
183
    def _unpack_revision(self, elt):
 
184
        """XML Element -> Revision object"""
 
185
        assert elt.tag == 'revision'
 
186
        format = elt.get('format')
 
187
        if format is not None:
 
188
            if format != '5':
 
189
                raise BzrError("invalid format version %r on inventory"
 
190
                                % format)
 
191
        get_cached = cache_utf8.get_cached_unicode
 
192
        rev = Revision(committer = elt.get('committer'),
 
193
                       timestamp = float(elt.get('timestamp')),
 
194
                       revision_id = get_cached(elt.get('revision_id')),
 
195
                       inventory_sha1 = elt.get('inventory_sha1')
 
196
                       )
 
197
        parents = elt.find('parents') or []
 
198
        for p in parents:
 
199
            assert p.tag == 'revision_ref', \
 
200
                   "bad parent node tag %r" % p.tag
 
201
            rev.parent_ids.append(get_cached(p.get('revision_id')))
 
202
        self._unpack_revision_properties(elt, rev)
 
203
        v = elt.get('timezone')
 
204
        rev.timezone = v and int(v)
 
205
        rev.message = elt.findtext('message') # text of <message>
 
206
        return rev
 
207
 
 
208
 
 
209
    def _unpack_revision_properties(self, elt, rev):
 
210
        """Unpack properties onto a revision."""
 
211
        props_elt = elt.find('properties')
 
212
        assert len(rev.properties) == 0
 
213
        if not props_elt:
 
214
            return
 
215
        for prop_elt in props_elt:
 
216
            assert prop_elt.tag == 'property', \
 
217
                "bad tag under properties list: %r" % prop_elt.tag
 
218
            name = prop_elt.get('name')
 
219
            value = prop_elt.text
 
220
            # If a property had an empty value ('') cElementTree reads
 
221
            # that back as None, convert it back to '', so that all
 
222
            # properties have string values
 
223
            if value is None:
 
224
                value = ''
 
225
            assert name not in rev.properties, \
 
226
                "repeated property %r" % name
 
227
            rev.properties[name] = value
 
228
 
 
229
 
 
230
serializer_v5 = Serializer_v5()