~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml_serializer.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-29 22:03:03 UTC
  • mfrom: (5416.2.6 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100929220303-cr95h8iwtggco721
(mbp) Add 'break-lock --force'

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# importing this module is fairly slow because it has to load several
23
23
# ElementTree bits
24
24
 
 
25
import re
 
26
 
25
27
from bzrlib.serializer import Serializer
26
 
from bzrlib.trace import mutter, warning
 
28
from bzrlib.trace import mutter
27
29
 
28
30
try:
29
31
    try:
31
33
        from xml.etree.cElementTree import (ElementTree, SubElement, Element,
32
34
            XMLTreeBuilder, fromstring, tostring)
33
35
        import xml.etree as elementtree
 
36
        # Also import ElementTree module so monkey-patching below always works
 
37
        import xml.etree.ElementTree
34
38
    except ImportError:
35
39
        from cElementTree import (ElementTree, SubElement, Element,
36
40
                                  XMLTreeBuilder, fromstring, tostring)
83
87
 
84
88
    def read_inventory(self, f, revision_id=None):
85
89
        try:
86
 
            return self._unpack_inventory(self._read_element(f),
87
 
                revision_id=None)
 
90
            try:
 
91
                return self._unpack_inventory(self._read_element(f),
 
92
                    revision_id=None)
 
93
            finally:
 
94
                f.close()
88
95
        except ParseError, e:
89
96
            raise errors.UnexpectedInventoryFormat(e)
90
97
 
108
115
        return ElementTree().parse(f)
109
116
 
110
117
 
111
 
# performance tuning for elementree's serialiser. This should be
112
 
# sent upstream - RBC 20060523.
113
 
# the functions here are patched into elementtree at runtime.
114
 
import re
115
 
escape_re = re.compile("[&'\"<>]")
116
 
escape_map = {
117
 
    "&":'&amp;',
118
 
    "'":"&apos;", # FIXME: overkill
119
 
    "\"":"&quot;",
120
 
    "<":"&lt;",
121
 
    ">":"&gt;",
122
 
    }
123
 
def _escape_replace(match, map=escape_map):
124
 
    return map[match.group()]
125
 
 
126
 
def _escape_attrib(text, encoding=None, replace=None):
127
 
    # escape attribute value
128
 
    try:
129
 
        if encoding:
130
 
            try:
131
 
                text = elementtree.ElementTree._encode(text, encoding)
132
 
            except UnicodeError:
133
 
                return elementtree.ElementTree._encode_entity(text)
134
 
        if replace is None:
135
 
            return escape_re.sub(_escape_replace, text)
136
 
        else:
137
 
            text = replace(text, "&", "&amp;")
138
 
            text = replace(text, "'", "&apos;") # FIXME: overkill
139
 
            text = replace(text, "\"", "&quot;")
140
 
            text = replace(text, "<", "&lt;")
141
 
            text = replace(text, ">", "&gt;")
142
 
            return text
143
 
    except (TypeError, AttributeError):
144
 
        elementtree.ElementTree._raise_serialization_error(text)
145
 
 
146
 
elementtree.ElementTree._escape_attrib = _escape_attrib
147
 
 
148
 
escape_cdata_re = re.compile("[&<>]")
149
 
escape_cdata_map = {
150
 
    "&":'&amp;',
151
 
    "<":"&lt;",
152
 
    ">":"&gt;",
153
 
    }
154
 
def _escape_cdata_replace(match, map=escape_cdata_map):
155
 
    return map[match.group()]
156
 
 
157
 
def _escape_cdata(text, encoding=None, replace=None):
158
 
    # escape character data
159
 
    try:
160
 
        if encoding:
161
 
            try:
162
 
                text = elementtree.ElementTree._encode(text, encoding)
163
 
            except UnicodeError:
164
 
                return elementtree.ElementTree._encode_entity(text)
165
 
        if replace is None:
166
 
            return escape_cdata_re.sub(_escape_cdata_replace, text)
167
 
        else:
168
 
            text = replace(text, "&", "&amp;")
169
 
            text = replace(text, "<", "&lt;")
170
 
            text = replace(text, ">", "&gt;")
171
 
            return text
172
 
    except (TypeError, AttributeError):
173
 
        elementtree.ElementTree._raise_serialization_error(text)
174
 
 
175
 
elementtree.ElementTree._escape_cdata = _escape_cdata
176
 
 
177
 
 
178
118
def escape_invalid_chars(message):
179
119
    """Escape the XML-invalid characters in a commit message.
180
120