~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml.py

  • Committer: Martin Pool
  • Date: 2005-04-15 01:31:21 UTC
  • Revision ID: mbp@sourcefrog.net-20050415013121-b18f1be12a735066
- Doc cleanups from Magnus Therning

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
 
# importing this module is fairly slow because it has to load several ElementTree bits
 
23
 
 
24
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
 
25
__author__ = "Martin Pool <mbp@canonical.com>"
 
26
 
24
27
try:
25
 
    from cElementTree import ElementTree, SubElement, Element
 
28
    from cElementTree import Element, ElementTree, SubElement
26
29
except ImportError:
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))
 
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