22
22
# importing this module is fairly slow because it has to load several
25
from bzrlib import registry
26
25
from bzrlib.trace import mutter, warning
30
# it's in this package in python2.5
31
from xml.etree.cElementTree import (ElementTree, SubElement, Element,
32
XMLTreeBuilder, fromstring, tostring)
33
import xml.etree as elementtree
35
from cElementTree import (ElementTree, SubElement, Element,
36
XMLTreeBuilder, fromstring, tostring)
38
ParseError = SyntaxError
28
from cElementTree import (ElementTree, SubElement, Element,
29
XMLTreeBuilder, fromstring, tostring)
39
31
except ImportError:
40
32
mutter('WARNING: using slower ElementTree; consider installing cElementTree'
41
33
" and make sure it's on your PYTHONPATH")
42
# this copy is shipped with bzr
43
34
from util.elementtree.ElementTree import (ElementTree, SubElement,
44
35
Element, XMLTreeBuilder,
45
36
fromstring, tostring)
46
37
import util.elementtree as elementtree
47
from xml.parsers.expat import ExpatError as ParseError
49
from bzrlib import errors
39
from bzrlib.errors import BzrError
52
42
class Serializer(object):
60
50
return tostring(self._pack_inventory(inv)) + '\n'
62
52
def read_inventory_from_string(self, xml_string):
64
return self._unpack_inventory(fromstring(xml_string))
66
raise errors.UnexpectedInventoryFormat(e)
53
return self._unpack_inventory(fromstring(xml_string))
68
55
def read_inventory(self, f):
70
return self._unpack_inventory(self._read_element(f))
72
raise errors.UnexpectedInventoryFormat(e)
56
return self._unpack_inventory(self._read_element(f))
74
58
def write_revision(self, rev, f):
75
59
self._write_element(self._pack_revision(rev), f)
156
140
elementtree.ElementTree._raise_serialization_error(text)
158
142
elementtree.ElementTree._escape_cdata = _escape_cdata
161
class SerializerRegistry(registry.Registry):
162
"""Registry for serializer objects"""
165
format_registry = SerializerRegistry()
166
format_registry.register_lazy('4', 'bzrlib.xml4', 'serializer_v4')
167
format_registry.register_lazy('5', 'bzrlib.xml5', 'serializer_v5')
168
format_registry.register_lazy('6', 'bzrlib.xml6', 'serializer_v6')
169
format_registry.register_lazy('7', 'bzrlib.xml7', 'serializer_v7')