~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml.py

  • Committer: Robert Collins
  • Date: 2005-08-25 07:48:27 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050825074826-dfdf6cecb8020b93
unbreak weavebench

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# "XML is like violence: if it doesn't solve your problem, you aren't
21
21
# using enough of it." -- various
22
22
 
23
 
 
24
 
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
25
 
__author__ = "Martin Pool <mbp@canonical.com>"
26
 
 
 
23
# importing this module is fairly slow because it has to load several ElementTree bits
27
24
try:
28
 
    from cElementTree import Element, ElementTree, SubElement
 
25
    from util.cElementTree import ElementTree, SubElement, Element
29
26
except ImportError:
30
 
    from elementtree.ElementTree import Element, ElementTree, SubElement
31
 
 
32
 
import os, time
33
 
from trace import mutter
34
 
 
35
 
class XMLMixin:
36
 
    def to_element(self):
37
 
        raise Exception("XMLMixin.to_element must be overridden in concrete classes")
38
 
    
39
 
    def write_xml(self, f):
40
 
        ElementTree(self.to_element()).write(f, 'utf-8')
41
 
        f.write('\n')
42
 
 
43
 
    def read_xml(cls, f):
44
 
        return cls.from_element(ElementTree().parse(f))
45
 
 
46
 
    read_xml = classmethod(read_xml)
47
 
 
48
 
 
49
 
 
 
27
    from util.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))