~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
###############################################################################
# Name: ed_thread.py                                                          #
# Purpose: Provides a base class for managing XML files and data.             #
# Author: Cody Precord <cprecord@editra.org>                                  #
# Copyright: (c) 2011 Cody Precord <staff@editra.org>                         #
# License: wxWindows License                                                  #
###############################################################################

"""
XML base class

"""

__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id:  $"
__revision__ = "$Revision:  $"

#-----------------------------------------------------------------------------#
# Imports
import types
from xml.dom import minidom
import extern.dexml as dexml
from extern.dexml.fields import *

#-----------------------------------------------------------------------------#

class EdXml(dexml.Model):
    """XML base class"""
    def __init__(self, **kwds):
        super(EdXml, self).__init__(**kwds)

    Xml = property(lambda self: self.GetXml(),
                   lambda self, xstr: self.parse(xstr))
    PrettyXml = property(lambda self: self.GetPrettyXml(),
                         lambda self, xstr: self.parse(xstr))

    def GetPrettyXml(self):
        """Get a nicely formatted version of the rendered xml string
        @return: string

        """
        txt = self.render()
        txt = minidom.parseString(txt).toprettyxml()
        txt = txt.replace('\t', '   ') # DeTabify
        return txt

    def GetXml(self):
        """Get the XML string for this object
        @return: string

        """
        return self.render()

    def Write(self, path):
        """Write the xml to a file
        @param path: string
        @return: success (bool)

        """
        suceeded = True
        try:
            xmlstr = self.PrettyXml
            if isinstance(xmlstr, types.UnicodeType):
                xmlstr = xmlstr.encode('utf-8')
            handle = open(path, 'wb')
            handle.write(xmlstr)
            handle.close()
        except (IOError, OSError, UnicodeEncodeError):
            suceeded = False
        return suceeded

    @classmethod
    def Load(cls, path):
        """Load this object from a file
        @param path: path to xml file
        @return: instance

        """
        instance = None
        try:
            handle = open(path, 'rb')
            xmlstr = handle.read()
            handle.close()
            instance = cls.parse(xmlstr)
        except (IOError, OSError):
            instance = None
        return instance

    @classmethod
    def LoadString(cls, xmlstr):
        """Load an object from an XML string
        @param cls: Class object
        @param xmlstr: string

        """
        instance = cls.parse(xmlstr)
        return instance