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

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ed_xml.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: ed_thread.py                                                          #
 
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
###############################################################################
 
8
 
 
9
"""
 
10
XML base class
 
11
 
 
12
"""
 
13
 
 
14
__author__ = "Cody Precord <cprecord@editra.org>"
 
15
__svnid__ = "$Id:  $"
 
16
__revision__ = "$Revision:  $"
 
17
 
 
18
#-----------------------------------------------------------------------------#
 
19
# Imports
 
20
import types
 
21
from xml.dom import minidom
 
22
import extern.dexml as dexml
 
23
from extern.dexml.fields import *
 
24
 
 
25
#-----------------------------------------------------------------------------#
 
26
 
 
27
class EdXml(dexml.Model):
 
28
    """XML base class"""
 
29
    def __init__(self, **kwds):
 
30
        super(EdXml, self).__init__(**kwds)
 
31
 
 
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))
 
36
 
 
37
    def GetPrettyXml(self):
 
38
        """Get a nicely formatted version of the rendered xml string
 
39
        @return: string
 
40
 
 
41
        """
 
42
        txt = self.render()
 
43
        txt = minidom.parseString(txt).toprettyxml()
 
44
        txt = txt.replace('\t', '   ') # DeTabify
 
45
        return txt
 
46
 
 
47
    def GetXml(self):
 
48
        """Get the XML string for this object
 
49
        @return: string
 
50
 
 
51
        """
 
52
        return self.render()
 
53
 
 
54
    def Write(self, path):
 
55
        """Write the xml to a file
 
56
        @param path: string
 
57
        @return: success (bool)
 
58
 
 
59
        """
 
60
        suceeded = True
 
61
        try:
 
62
            xmlstr = self.PrettyXml
 
63
            if isinstance(xmlstr, types.UnicodeType):
 
64
                xmlstr = xmlstr.encode('utf-8')
 
65
            handle = open(path, 'wb')
 
66
            handle.write(xmlstr)
 
67
            handle.close()
 
68
        except (IOError, OSError, UnicodeEncodeError):
 
69
            suceeded = False
 
70
        return suceeded
 
71
 
 
72
    @classmethod
 
73
    def Load(cls, path):
 
74
        """Load this object from a file
 
75
        @param path: path to xml file
 
76
        @return: instance
 
77
 
 
78
        """
 
79
        instance = None
 
80
        try:
 
81
            handle = open(path, 'rb')
 
82
            xmlstr = handle.read()
 
83
            handle.close()
 
84
            instance = cls.parse(xmlstr)
 
85
        except (IOError, OSError):
 
86
            instance = None
 
87
        return instance
 
88
 
 
89
    @classmethod
 
90
    def LoadString(cls, xmlstr):
 
91
        """Load an object from an XML string
 
92
        @param cls: Class object
 
93
        @param xmlstr: string
 
94
 
 
95
        """
 
96
        instance = cls.parse(xmlstr)
 
97
        return instance