~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml.py

  • Committer: Martin Pool
  • Date: 2005-07-22 22:37:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050722223753-7dced4e32d3ce21d
- add the start of a test for inventory file-id matching

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
1
2
# -*- coding: UTF-8 -*-
2
3
 
3
4
# This program is free software; you can redistribute it and/or modify
19
20
# "XML is like violence: if it doesn't solve your problem, you aren't
20
21
# using enough of it." -- various
21
22
 
22
 
# importing this module is fairly slow because it has to load several
23
 
# ElementTree bits
24
 
 
25
 
from bzrlib.trace import mutter, warning
26
 
 
 
23
# importing this module is fairly slow because it has to load several ElementTree bits
27
24
try:
28
 
    from cElementTree import (ElementTree, SubElement, Element,
29
 
                              XMLTreeBuilder, fromstring, tostring)
30
 
    import elementtree
 
25
    from cElementTree import ElementTree, SubElement, Element
31
26
except ImportError:
32
 
    mutter('WARNING: using slower ElementTree; consider installing cElementTree'
33
 
           " and make sure it's on your PYTHONPATH")
34
 
    from util.elementtree.ElementTree import (ElementTree, SubElement,
35
 
                                              Element, XMLTreeBuilder,
36
 
                                              fromstring, tostring)
37
 
    import util.elementtree as elementtree
38
 
 
39
 
from bzrlib.errors import BzrError
40
 
 
41
 
 
42
 
class Serializer(object):
43
 
    """Abstract object serialize/deserialize"""
44
 
    def write_inventory(self, inv, f):
45
 
        """Write inventory to a file"""
46
 
        elt = self._pack_inventory(inv)
47
 
        self._write_element(elt, f)
48
 
 
49
 
    def write_inventory_to_string(self, inv):
50
 
        return tostring(self._pack_inventory(inv)) + '\n'
51
 
 
52
 
    def read_inventory_from_string(self, xml_string):
53
 
        return self._unpack_inventory(fromstring(xml_string))
54
 
 
55
 
    def read_inventory(self, f):
56
 
        return self._unpack_inventory(self._read_element(f))
57
 
 
58
 
    def write_revision(self, rev, f):
59
 
        self._write_element(self._pack_revision(rev), f)
60
 
 
61
 
    def write_revision_to_string(self, rev):
62
 
        return tostring(self._pack_revision(rev)) + '\n'
63
 
 
64
 
    def read_revision(self, f):
65
 
        return self._unpack_revision(self._read_element(f))
66
 
 
67
 
    def read_revision_from_string(self, xml_string):
68
 
        return self._unpack_revision(fromstring(xml_string))
69
 
 
70
 
    def _write_element(self, elt, f):
71
 
        ElementTree(elt).write(f, 'utf-8')
72
 
        f.write('\n')
73
 
 
74
 
    def _read_element(self, f):
75
 
        return ElementTree().parse(f)
76
 
 
77
 
 
78
 
# performance tuning for elementree's serialiser. This should be
79
 
# sent upstream - RBC 20060523.
80
 
# the functions here are patched into elementtree at runtime.
81
 
import re
82
 
escape_re = re.compile("[&'\"<>]")
83
 
escape_map = {
84
 
    "&":'&amp;',
85
 
    "'":"&apos;", # FIXME: overkill
86
 
    "\"":"&quot;",
87
 
    "<":"&lt;",
88
 
    ">":"&gt;",
89
 
    }
90
 
def _escape_replace(match, map=escape_map):
91
 
    return map[match.group()]
92
 
 
93
 
def _escape_attrib(text, encoding=None, replace=None):
94
 
    # escape attribute value
95
 
    try:
96
 
        if encoding:
97
 
            try:
98
 
                text = elementtree.ElementTree._encode(text, encoding)
99
 
            except UnicodeError:
100
 
                return elementtree.ElementTree._encode_entity(text)
101
 
        if replace is None:
102
 
            return escape_re.sub(_escape_replace, text)
103
 
        else:
104
 
            text = replace(text, "&", "&amp;")
105
 
            text = replace(text, "'", "&apos;") # FIXME: overkill
106
 
            text = replace(text, "\"", "&quot;")
107
 
            text = replace(text, "<", "&lt;")
108
 
            text = replace(text, ">", "&gt;")
109
 
            return text
110
 
    except (TypeError, AttributeError):
111
 
        elementtree.ElementTree._raise_serialization_error(text)
112
 
 
113
 
elementtree.ElementTree._escape_attrib = _escape_attrib
114
 
 
115
 
escape_cdata_re = re.compile("[&<>]")
116
 
escape_cdata_map = {
117
 
    "&":'&amp;',
118
 
    "<":"&lt;",
119
 
    ">":"&gt;",
120
 
    }
121
 
def _escape_cdata_replace(match, map=escape_cdata_map):
122
 
    return map[match.group()]
123
 
 
124
 
def _escape_cdata(text, encoding=None, replace=None):
125
 
    # escape character data
126
 
    try:
127
 
        if encoding:
128
 
            try:
129
 
                text = elementtree.ElementTree._encode(text, encoding)
130
 
            except UnicodeError:
131
 
                return elementtree.ElementTree._encode_entity(text)
132
 
        if replace is None:
133
 
            return escape_cdata_re.sub(_escape_cdata_replace, text)
134
 
        else:
135
 
            text = replace(text, "&", "&amp;")
136
 
            text = replace(text, "<", "&lt;")
137
 
            text = replace(text, ">", "&gt;")
138
 
            return text
139
 
    except (TypeError, AttributeError):
140
 
        elementtree.ElementTree._raise_serialization_error(text)
141
 
 
142
 
elementtree.ElementTree._escape_cdata = _escape_cdata
 
27
    from elementtree.ElementTree import ElementTree, SubElement, Element
 
28
 
 
29
 
 
30
def pack_xml(o, f):
 
31
    """Write object o to file f as XML.
 
32
 
 
33
    o must provide a to_element method.
 
34
    """
 
35
    ElementTree(o.to_element()).write(f, 'utf-8')
 
36
    f.write('\n')
 
37
 
 
38
 
 
39
def unpack_xml(cls, f):
 
40
    return cls.from_element(ElementTree().parse(f))