1
###############################################################################
3
# Purpose: Provides a base class for managing XML files and data. #
4
# Author: Cody Precord <cprecord@editra.org> #
5
# Copyright: (c) 2011 Cody Precord <staff@editra.org> #
6
# License: wxWindows License #
7
###############################################################################
14
__author__ = "Cody Precord <cprecord@editra.org>"
16
__revision__ = "$Revision: $"
18
#-----------------------------------------------------------------------------#
21
from xml.dom import minidom
22
import extern.dexml as dexml
23
from extern.dexml.fields import *
25
#-----------------------------------------------------------------------------#
27
class EdXml(dexml.Model):
29
def __init__(self, **kwds):
30
super(EdXml, self).__init__(**kwds)
32
Xml = property(lambda self: self.GetXml(),
33
lambda self, xstr: self.parse(xstr))
34
PrettyXml = property(lambda self: self.GetPrettyXml(),
35
lambda self, xstr: self.parse(xstr))
37
def GetPrettyXml(self):
38
"""Get a nicely formatted version of the rendered xml string
43
txt = minidom.parseString(txt).toprettyxml()
44
txt = txt.replace('\t', ' ') # DeTabify
48
"""Get the XML string for this object
54
def Write(self, path):
55
"""Write the xml to a file
57
@return: success (bool)
62
xmlstr = self.PrettyXml
63
if isinstance(xmlstr, types.UnicodeType):
64
xmlstr = xmlstr.encode('utf-8')
65
handle = open(path, 'wb')
68
except (IOError, OSError, UnicodeEncodeError):
74
"""Load this object from a file
75
@param path: path to xml file
81
handle = open(path, 'rb')
82
xmlstr = handle.read()
84
instance = cls.parse(xmlstr)
85
except (IOError, OSError):
90
def LoadString(cls, xmlstr):
91
"""Load an object from an XML string
92
@param cls: Class object
96
instance = cls.parse(xmlstr)