~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml.py

  • Committer: Martin Pool
  • Date: 2005-09-05 09:27:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050905092711-f9f5bded3fd82605
- more disentangling of xml storage format from objects

- remove pack_xml and unpack_xml function in favor of 
  serializer object

- test unpacking canned revision xml

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    from util.elementtree.ElementTree import ElementTree, SubElement, Element
30
30
 
31
31
from bzrlib.inventory import ROOT_ID, Inventory, InventoryEntry
32
 
        
 
32
from bzrlib.revision import Revision, RevisionReference        
33
33
 
34
34
 
35
35
class Serializer(object):
42
42
    def read_inventory(self, f):
43
43
        return self._unpack_inventory(self._read_element(f))
44
44
 
 
45
    def write_revision(self, rev, f):
 
46
        self._write_element(self._pack_revision(rev), f)
 
47
 
 
48
    def read_revision(self, f):
 
49
        return self._unpack_revision(self._read_element(f))
 
50
 
45
51
    def _write_element(self, elt, f):
46
52
        ElementTree(elt).write(f, 'utf-8')
47
53
        f.write('\n')
133
139
        return ie
134
140
 
135
141
 
 
142
    def _pack_revision(self, rev):
 
143
        """Revision object -> xml tree"""
 
144
        root = Element('revision',
 
145
                       committer = rev.committer,
 
146
                       timestamp = '%.9f' % rev.timestamp,
 
147
                       revision_id = rev.revision_id,
 
148
                       inventory_id = rev.inventory_id,
 
149
                       inventory_sha1 = rev.inventory_sha1,
 
150
                       )
 
151
        if rev.timezone:
 
152
            root.set('timezone', str(rev.timezone))
 
153
        root.text = '\n'
 
154
 
 
155
        msg = SubElement(root, 'message')
 
156
        msg.text = rev.message
 
157
        msg.tail = '\n'
 
158
 
 
159
        if rev.parents:
 
160
            pelts = SubElement(root, 'parents')
 
161
            pelts.tail = pelts.text = '\n'
 
162
            for rr in rev.parents:
 
163
                assert isinstance(rr, RevisionReference)
 
164
                p = SubElement(pelts, 'revision_ref')
 
165
                p.tail = '\n'
 
166
                assert rr.revision_id
 
167
                p.set('revision_id', rr.revision_id)
 
168
                if rr.revision_sha1:
 
169
                    p.set('revision_sha1', rr.revision_sha1)
 
170
 
 
171
        return root
 
172
 
 
173
    
 
174
    def _unpack_revision(self, elt):
 
175
        """XML Element -> Revision object"""
 
176
        
 
177
        # <changeset> is deprecated...
 
178
        if elt.tag not in ('revision', 'changeset'):
 
179
            raise bzrlib.errors.BzrError("unexpected tag in revision file: %r" % elt)
 
180
 
 
181
        rev = Revision(committer = elt.get('committer'),
 
182
                       timestamp = float(elt.get('timestamp')),
 
183
                       revision_id = elt.get('revision_id'),
 
184
                       inventory_id = elt.get('inventory_id'),
 
185
                       inventory_sha1 = elt.get('inventory_sha1')
 
186
                       )
 
187
 
 
188
        precursor = elt.get('precursor')
 
189
        precursor_sha1 = elt.get('precursor_sha1')
 
190
 
 
191
        pelts = elt.find('parents')
 
192
 
 
193
        if pelts:
 
194
            for p in pelts:
 
195
                assert p.tag == 'revision_ref', \
 
196
                       "bad parent node tag %r" % p.tag
 
197
                rev_ref = RevisionReference(p.get('revision_id'),
 
198
                                            p.get('revision_sha1'))
 
199
                rev.parents.append(rev_ref)
 
200
 
 
201
            if precursor:
 
202
                # must be consistent
 
203
                prec_parent = rev.parents[0].revision_id
 
204
                assert prec_parent == precursor
 
205
        elif precursor:
 
206
            # revisions written prior to 0.0.5 have a single precursor
 
207
            # give as an attribute
 
208
            rev_ref = RevisionReference(precursor, precursor_sha1)
 
209
            rev.parents.append(rev_ref)
 
210
 
 
211
        v = elt.get('timezone')
 
212
        rev.timezone = v and int(v)
 
213
 
 
214
        rev.message = elt.findtext('message') # text of <message>
 
215
        return rev
 
216
 
 
217
 
 
218
 
136
219
"""singleton instance"""
137
220
serializer_v4 = _Serializer_v4()
138
 
 
139
 
 
140
 
 
141
 
 
142
 
 
143
 
def pack_xml(o, f):
144
 
    """Write object o to file f as XML.
145
 
 
146
 
    o must provide a to_element method.
147
 
    """
148
 
    ElementTree(o.to_element()).write(f, 'utf-8')
149
 
    f.write('\n')
150
 
 
151
 
 
152
 
def unpack_xml(cls, f):
153
 
    return cls.from_element(ElementTree().parse(f))