~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-31 16:12:57 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060731161257-91a231523255332c
new official bzr.ico

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# -*- coding: UTF-8 -*-
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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:
29
 
    try:
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
34
 
    except ImportError:
35
 
        from cElementTree import (ElementTree, SubElement, Element,
36
 
                                  XMLTreeBuilder, fromstring, tostring)
37
 
        import elementtree
38
 
    ParseError = SyntaxError
 
28
    from cElementTree import (ElementTree, SubElement, Element,
 
29
                              XMLTreeBuilder, fromstring, tostring)
 
30
    import elementtree
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
48
38
 
49
 
from bzrlib import errors
 
39
from bzrlib.errors import BzrError
50
40
 
51
41
 
52
42
class Serializer(object):
60
50
        return tostring(self._pack_inventory(inv)) + '\n'
61
51
 
62
52
    def read_inventory_from_string(self, xml_string):
63
 
        try:
64
 
            return self._unpack_inventory(fromstring(xml_string))
65
 
        except ParseError, e:
66
 
            raise errors.UnexpectedInventoryFormat(e)
 
53
        return self._unpack_inventory(fromstring(xml_string))
67
54
 
68
55
    def read_inventory(self, f):
69
 
        try:
70
 
            return self._unpack_inventory(self._read_element(f))
71
 
        except ParseError, e:
72
 
            raise errors.UnexpectedInventoryFormat(e)
 
56
        return self._unpack_inventory(self._read_element(f))
73
57
 
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)
157
141
 
158
142
elementtree.ElementTree._escape_cdata = _escape_cdata
159
 
 
160
 
 
161
 
class SerializerRegistry(registry.Registry):
162
 
    """Registry for serializer objects"""
163
 
 
164
 
 
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')