33
31
from xml.etree.cElementTree import (ElementTree, SubElement, Element,
34
32
XMLTreeBuilder, fromstring, tostring)
35
33
import xml.etree as elementtree
36
# Also import ElementTree module so monkey-patching below always works
37
import xml.etree.ElementTree
38
34
except ImportError:
39
35
from cElementTree import (ElementTree, SubElement, Element,
40
36
XMLTreeBuilder, fromstring, tostring)
56
52
class XMLSerializer(Serializer):
57
53
"""Abstract XML object serialize/deserialize"""
59
squashes_xml_invalid_characters = True
61
55
def read_inventory_from_string(self, xml_string, revision_id=None,
62
entry_cache=None, return_from_cache=False):
63
57
"""Read xml_string into an inventory object.
65
59
:param xml_string: The xml to read.
73
67
:param entry_cache: An optional cache of InventoryEntry objects. If
74
68
supplied we will look up entries via (file_id, revision_id) which
75
69
should map to a valid InventoryEntry (File/Directory/etc) object.
76
:param return_from_cache: Return entries directly from the cache,
77
rather than copying them first. This is only safe if the caller
78
promises not to mutate the returned inventory entries, but it can
79
make some operations significantly faster.
82
72
return self._unpack_inventory(fromstring(xml_string), revision_id,
83
entry_cache=entry_cache,
84
return_from_cache=return_from_cache)
73
entry_cache=entry_cache)
85
74
except ParseError, e:
86
75
raise errors.UnexpectedInventoryFormat(e)
88
77
def read_inventory(self, f, revision_id=None):
91
return self._unpack_inventory(self._read_element(f),
79
return self._unpack_inventory(self._read_element(f),
95
81
except ParseError, e:
96
82
raise errors.UnexpectedInventoryFormat(e)
115
101
return ElementTree().parse(f)
104
# performance tuning for elementree's serialiser. This should be
105
# sent upstream - RBC 20060523.
106
# the functions here are patched into elementtree at runtime.
108
escape_re = re.compile("[&'\"<>]")
111
"'":"'", # FIXME: overkill
116
def _escape_replace(match, map=escape_map):
117
return map[match.group()]
119
def _escape_attrib(text, encoding=None, replace=None):
120
# escape attribute value
124
text = elementtree.ElementTree._encode(text, encoding)
126
return elementtree.ElementTree._encode_entity(text)
128
return escape_re.sub(_escape_replace, text)
130
text = replace(text, "&", "&")
131
text = replace(text, "'", "'") # FIXME: overkill
132
text = replace(text, "\"", """)
133
text = replace(text, "<", "<")
134
text = replace(text, ">", ">")
136
except (TypeError, AttributeError):
137
elementtree.ElementTree._raise_serialization_error(text)
139
elementtree.ElementTree._escape_attrib = _escape_attrib
141
escape_cdata_re = re.compile("[&<>]")
147
def _escape_cdata_replace(match, map=escape_cdata_map):
148
return map[match.group()]
150
def _escape_cdata(text, encoding=None, replace=None):
151
# escape character data
155
text = elementtree.ElementTree._encode(text, encoding)
157
return elementtree.ElementTree._encode_entity(text)
159
return escape_cdata_re.sub(_escape_cdata_replace, text)
161
text = replace(text, "&", "&")
162
text = replace(text, "<", "<")
163
text = replace(text, ">", ">")
165
except (TypeError, AttributeError):
166
elementtree.ElementTree._raise_serialization_error(text)
168
elementtree.ElementTree._escape_cdata = _escape_cdata
118
171
def escape_invalid_chars(message):
119
172
"""Escape the XML-invalid characters in a commit message.
121
174
:param message: Commit message to escape
122
175
:return: tuple with escaped message and number of characters escaped
126
177
# Python strings can include characters that can't be
127
178
# represented in well-formed XML; escape characters that
128
179
# aren't listed in the XML specification