~bzr-pqm/bzr/bzr.dev

1 by mbp at sourcefrog
import from baz patch-364
1
#! /usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""XML externalization support."""
19
48 by Martin Pool
witty comment
20
# "XML is like violence: if it doesn't solve your problem, you aren't
21
# using enough of it." -- various
22
802 by Martin Pool
- Remove XMLMixin class in favour of simple pack_xml, unpack_xml functions
23
# importing this module is fairly slow because it has to load several ElementTree bits
24
try:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
25
    from util.cElementTree import ElementTree, SubElement, Element
802 by Martin Pool
- Remove XMLMixin class in favour of simple pack_xml, unpack_xml functions
26
except ImportError:
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
27
    from util.elementtree.ElementTree import ElementTree, SubElement, Element
802 by Martin Pool
- Remove XMLMixin class in favour of simple pack_xml, unpack_xml functions
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))