/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
1 by mbp at sourcefrog
import from baz patch-364
23
24
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
25
__author__ = "Martin Pool <mbp@canonical.com>"
26
800 by Martin Pool
Merge John's import-speedup branch:
27
_ElementTree = None
28
def ElementTree(*args, **kwargs):
29
    global _ElementTree
30
    if _ElementTree is None:
31
        try:
32
            from cElementTree import ElementTree
33
        except ImportError:
34
            from elementtree.ElementTree import ElementTree
35
        _ElementTree = ElementTree
36
    return _ElementTree(*args, **kwargs)
37
38
_Element = None
39
def Element(*args, **kwargs):
40
    global _Element
41
    if _Element is None:
42
        try:
43
            from cElementTree import Element
44
        except ImportError:
45
            from elementtree.ElementTree import Element
46
        _Element = Element
47
    return _Element(*args, **kwargs)
48
49
50
_SubElement = None
51
def SubElement(*args, **kwargs):
52
    global _SubElement
53
    if _SubElement is None:
54
        try:
55
            from cElementTree import SubElement
56
        except ImportError:
57
            from elementtree.ElementTree import SubElement
58
        _SubElement = SubElement
59
    return _SubElement(*args, **kwargs)
60
1 by mbp at sourcefrog
import from baz patch-364
61
62
import os, time
63
from trace import mutter
64
65
class XMLMixin:
66
    def to_element(self):
67
        raise Exception("XMLMixin.to_element must be overridden in concrete classes")
68
    
69
    def write_xml(self, f):
70
        ElementTree(self.to_element()).write(f, 'utf-8')
71
        f.write('\n')
72
73
    def read_xml(cls, f):
74
        return cls.from_element(ElementTree().parse(f))
75
76
    read_xml = classmethod(read_xml)
77