1# Copyright (C) 2009, 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17"""Inventory/revision serialization."""
18
19from .. import errors, registry
20
21
22class BadInventoryFormat(errors.BzrError):
23
24    _fmt = "Root class for inventory serialization errors"
25
26
27class UnexpectedInventoryFormat(BadInventoryFormat):
28
29    _fmt = "The inventory was not in the expected format:\n %(msg)s"
30
31    def __init__(self, msg):
32        BadInventoryFormat.__init__(self, msg=msg)
33
34
35class UnsupportedInventoryKind(errors.BzrError):
36
37    _fmt = """Unsupported entry kind %(kind)s"""
38
39    def __init__(self, kind):
40        self.kind = kind
41
42
43class Serializer(object):
44    """Inventory and revision serialization/deserialization."""
45
46    squashes_xml_invalid_characters = False
47
48    def write_inventory(self, inv, f):
49        """Write inventory to a file.
50
51        Note: this is a *whole inventory* operation, and should only be used
52        sparingly, as it does not scale well with large trees.
53        """
54        raise NotImplementedError(self.write_inventory)
55
56    def write_inventory_to_chunks(self, inv):
57        """Produce a simple bytestring chunk representation of an inventory.
58
59        Note: this is a *whole inventory* operation, and should only be used
60        sparingly, as it does not scale well with large trees.
61
62        The requirement for the contents of the string is that it can be passed
63        to read_inventory_from_lines and the result is an identical inventory
64        in memory.
65        """
66        raise NotImplementedError(self.write_inventory_to_chunks)
67
68    def write_inventory_to_lines(self, inv):
69        """Produce a simple lines representation of an inventory.
70
71        Note: this is a *whole inventory* operation, and should only be used
72        sparingly, as it does not scale well with large trees.
73
74        The requirement for the contents of the string is that it can be passed
75        to read_inventory_from_lines and the result is an identical inventory
76        in memory.
77        """
78        raise NotImplementedError(self.write_inventory_to_lines)
79
80    def read_inventory_from_lines(self, lines, revision_id=None,
81                                  entry_cache=None, return_from_cache=False):
82        """Read bytestring chunks into an inventory object.
83
84        :param lines: The serialized inventory to read.
85        :param revision_id: If not-None, the expected revision id of the
86            inventory. Some serialisers use this to set the results' root
87            revision. This should be supplied for deserialising all
88            from-repository inventories so that xml5 inventories that were
89            serialised without a revision identifier can be given the right
90            revision id (but not for working tree inventories where users can
91            edit the data without triggering checksum errors or anything).
92        :param entry_cache: An optional cache of InventoryEntry objects. If
93            supplied we will look up entries via (file_id, revision_id) which
94            should map to a valid InventoryEntry (File/Directory/etc) object.
95        :param return_from_cache: Return entries directly from the cache,
96            rather than copying them first. This is only safe if the caller
97            promises not to mutate the returned inventory entries, but it can
98            make some operations significantly faster.
99        """
100        raise NotImplementedError(self.read_inventory_from_lines)
101
102    def read_inventory(self, f, revision_id=None):
103        """See read_inventory_from_lines."""
104        raise NotImplementedError(self.read_inventory)
105
106    def write_revision_to_string(self, rev):
107        raise NotImplementedError(self.write_revision_to_string)
108
109    def write_revision_to_lines(self, rev):
110        raise NotImplementedError(self.write_revision_to_lines)
111
112    def read_revision(self, f):
113        raise NotImplementedError(self.read_revision)
114
115    def read_revision_from_string(self, xml_string):
116        raise NotImplementedError(self.read_revision_from_string)
117
118
119class SerializerRegistry(registry.Registry):
120    """Registry for serializer objects"""
121
122
123format_registry = SerializerRegistry()
124format_registry.register_lazy('5', 'breezy.bzr.xml5', 'serializer_v5')
125format_registry.register_lazy('6', 'breezy.bzr.xml6', 'serializer_v6')
126format_registry.register_lazy('7', 'breezy.bzr.xml7', 'serializer_v7')
127format_registry.register_lazy('8', 'breezy.bzr.xml8', 'serializer_v8')
128format_registry.register_lazy('9', 'breezy.bzr.chk_serializer',
129                              'chk_serializer_255_bigpage')
130format_registry.register_lazy('10', 'breezy.bzr.chk_serializer',
131                              'chk_bencode_serializer')
132