~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# importing this module is fairly slow because it has to load several
23
23
# ElementTree bits
24
24
 
25
 
from bzrlib import registry
26
25
from bzrlib.trace import mutter, warning
27
26
 
28
27
try:
34
33
    except ImportError:
35
34
        from cElementTree import (ElementTree, SubElement, Element,
36
35
                                  XMLTreeBuilder, fromstring, tostring)
37
 
        import elementtree.ElementTree
 
36
        import elementtree
38
37
    ParseError = SyntaxError
39
38
except ImportError:
40
39
    mutter('WARNING: using slower ElementTree; consider installing cElementTree'
51
50
 
52
51
class Serializer(object):
53
52
    """Abstract object serialize/deserialize"""
54
 
 
55
53
    def write_inventory(self, inv, f):
56
54
        """Write inventory to a file"""
57
 
        raise NotImplementedError(self.write_inventory)
 
55
        elt = self._pack_inventory(inv)
 
56
        self._write_element(elt, f)
58
57
 
59
58
    def write_inventory_to_string(self, inv):
60
 
        raise NotImplementedError(self.write_inventory_to_string)
61
 
 
62
 
    def read_inventory_from_string(self, xml_string, revision_id=None):
63
 
        """Read xml_string into an inventory object.
64
 
 
65
 
        :param xml_string: The xml to read.
66
 
        :param revision_id: If not-None, the expected revision id of the
67
 
            inventory. Some serialisers use this to set the results' root
68
 
            revision. This should be supplied for deserialising all
69
 
            from-repository inventories so that xml5 inventories that were
70
 
            serialised without a revision identifier can be given the right
71
 
            revision id (but not for working tree inventories where users can
72
 
            edit the data without triggering checksum errors or anything).
73
 
        """
 
59
        return tostring(self._pack_inventory(inv)) + '\n'
 
60
 
 
61
    def read_inventory_from_string(self, xml_string):
74
62
        try:
75
 
            return self._unpack_inventory(fromstring(xml_string), revision_id)
 
63
            return self._unpack_inventory(fromstring(xml_string))
76
64
        except ParseError, e:
77
65
            raise errors.UnexpectedInventoryFormat(e)
78
66
 
79
 
    def read_inventory(self, f, revision_id=None):
 
67
    def read_inventory(self, f):
80
68
        try:
81
 
            return self._unpack_inventory(self._read_element(f),
82
 
                revision_id=None)
 
69
            return self._unpack_inventory(self._read_element(f))
83
70
        except ParseError, e:
84
71
            raise errors.UnexpectedInventoryFormat(e)
85
72
 
168
155
        elementtree.ElementTree._raise_serialization_error(text)
169
156
 
170
157
elementtree.ElementTree._escape_cdata = _escape_cdata
171
 
 
172
 
 
173
 
class SerializerRegistry(registry.Registry):
174
 
    """Registry for serializer objects"""
175
 
 
176
 
 
177
 
format_registry = SerializerRegistry()
178
 
format_registry.register_lazy('4', 'bzrlib.xml4', 'serializer_v4')
179
 
format_registry.register_lazy('5', 'bzrlib.xml5', 'serializer_v5')
180
 
format_registry.register_lazy('6', 'bzrlib.xml6', 'serializer_v6')
181
 
format_registry.register_lazy('7', 'bzrlib.xml7', 'serializer_v7')
182
 
format_registry.register_lazy('8', 'bzrlib.xml8', 'serializer_v8')