1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# Copyright (C) 2005, 2006 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""XML externalization support."""
# "XML is like violence: if it doesn't solve your problem, you aren't
# using enough of it." -- various
# importing this module is fairly slow because it has to load several
# ElementTree bits
from bzrlib import registry
from bzrlib.trace import mutter, warning
try:
try:
# it's in this package in python2.5
from xml.etree.cElementTree import (ElementTree, SubElement, Element,
XMLTreeBuilder, fromstring, tostring)
import xml.etree as elementtree
except ImportError:
from cElementTree import (ElementTree, SubElement, Element,
XMLTreeBuilder, fromstring, tostring)
import elementtree
ParseError = SyntaxError
except ImportError:
mutter('WARNING: using slower ElementTree; consider installing cElementTree'
" and make sure it's on your PYTHONPATH")
# this copy is shipped with bzr
from util.elementtree.ElementTree import (ElementTree, SubElement,
Element, XMLTreeBuilder,
fromstring, tostring)
import util.elementtree as elementtree
from xml.parsers.expat import ExpatError as ParseError
from bzrlib import errors
class Serializer(object):
"""Abstract object serialize/deserialize"""
def write_inventory(self, inv, f):
"""Write inventory to a file"""
raise NotImplementedError(self.write_inventory)
def write_inventory_to_string(self, inv):
raise NotImplementedError(self.write_inventory_to_string)
def read_inventory_from_string(self, xml_string):
try:
return self._unpack_inventory(fromstring(xml_string))
except ParseError, e:
raise errors.UnexpectedInventoryFormat(e)
def read_inventory(self, f):
try:
return self._unpack_inventory(self._read_element(f))
except ParseError, e:
raise errors.UnexpectedInventoryFormat(e)
def write_revision(self, rev, f):
self._write_element(self._pack_revision(rev), f)
def write_revision_to_string(self, rev):
return tostring(self._pack_revision(rev)) + '\n'
def read_revision(self, f):
return self._unpack_revision(self._read_element(f))
def read_revision_from_string(self, xml_string):
return self._unpack_revision(fromstring(xml_string))
def _write_element(self, elt, f):
ElementTree(elt).write(f, 'utf-8')
f.write('\n')
def _read_element(self, f):
return ElementTree().parse(f)
# performance tuning for elementree's serialiser. This should be
# sent upstream - RBC 20060523.
# the functions here are patched into elementtree at runtime.
import re
escape_re = re.compile("[&'\"<>]")
escape_map = {
"&":'&',
"'":"'", # FIXME: overkill
"\"":""",
"<":"<",
">":">",
}
def _escape_replace(match, map=escape_map):
return map[match.group()]
def _escape_attrib(text, encoding=None, replace=None):
# escape attribute value
try:
if encoding:
try:
text = elementtree.ElementTree._encode(text, encoding)
except UnicodeError:
return elementtree.ElementTree._encode_entity(text)
if replace is None:
return escape_re.sub(_escape_replace, text)
else:
text = replace(text, "&", "&")
text = replace(text, "'", "'") # FIXME: overkill
text = replace(text, "\"", """)
text = replace(text, "<", "<")
text = replace(text, ">", ">")
return text
except (TypeError, AttributeError):
elementtree.ElementTree._raise_serialization_error(text)
elementtree.ElementTree._escape_attrib = _escape_attrib
escape_cdata_re = re.compile("[&<>]")
escape_cdata_map = {
"&":'&',
"<":"<",
">":">",
}
def _escape_cdata_replace(match, map=escape_cdata_map):
return map[match.group()]
def _escape_cdata(text, encoding=None, replace=None):
# escape character data
try:
if encoding:
try:
text = elementtree.ElementTree._encode(text, encoding)
except UnicodeError:
return elementtree.ElementTree._encode_entity(text)
if replace is None:
return escape_cdata_re.sub(_escape_cdata_replace, text)
else:
text = replace(text, "&", "&")
text = replace(text, "<", "<")
text = replace(text, ">", ">")
return text
except (TypeError, AttributeError):
elementtree.ElementTree._raise_serialization_error(text)
elementtree.ElementTree._escape_cdata = _escape_cdata
class SerializerRegistry(registry.Registry):
"""Registry for serializer objects"""
format_registry = SerializerRegistry()
format_registry.register_lazy('4', 'bzrlib.xml4', 'serializer_v4')
format_registry.register_lazy('5', 'bzrlib.xml5', 'serializer_v5')
format_registry.register_lazy('6', 'bzrlib.xml6', 'serializer_v6')
format_registry.register_lazy('7', 'bzrlib.xml7', 'serializer_v7')
|