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

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/XRCed/attribute.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
# Name:         attribute.py
 
2
# Purpose:      Attribute classes
 
3
# Author:       Roman Rolinsky <rolinsky@femagsoft.com>
 
4
# Created:      25.06.2007
 
5
# RCS-ID:       $Id$
 
6
 
 
7
'''
 
8
Attribute processing classes.
 
9
 
 
10
This module contains some predefined classes which can be used to store and
 
11
retrieve XML data into Python objects.
 
12
'''
 
13
 
 
14
import cPickle
 
15
from model import Model
 
16
 
 
17
class Attribute:
 
18
    '''Base class, used for simple attributes, i.e. single attributes
 
19
    storing data as text strings.'''
 
20
    @staticmethod
 
21
    def add(parentNode, attribute, value):
 
22
        '''Store attribute value in DOM as a text node.
 
23
 
 
24
        @param attribute: Attribute name.
 
25
        @param value: Attribute value (Python string).
 
26
        '''
 
27
        if attribute == '':
 
28
            elem = parentNode
 
29
        else:
 
30
            elem = Model.dom.createElement(attribute)
 
31
            parentNode.appendChild(elem)
 
32
        text = Model.dom.createTextNode(value)
 
33
        elem.appendChild(text)
 
34
    @staticmethod
 
35
    def get(node):
 
36
        '''Get value (or return a default value) from a DOM C{Element} object.'''
 
37
        if node is None: return ''
 
38
        try:
 
39
            n = node.childNodes[0]
 
40
            return n.wholeText
 
41
        except IndexError:
 
42
            return ''
 
43
 
 
44
class ContentAttribute:
 
45
    '''Content attribute class. Value is a list of strings.'''
 
46
    @staticmethod
 
47
    def add(parentNode, attribute, value):
 
48
        contentElem = Model.dom.createElement(attribute)
 
49
        parentNode.appendChild(contentElem)
 
50
        for item in value:
 
51
            elem = Model.dom.createElement('item')
 
52
            text = Model.dom.createTextNode(item)
 
53
            elem.appendChild(text)
 
54
            contentElem.appendChild(elem)
 
55
    @staticmethod
 
56
    def get(node):
 
57
        if node is None: return []
 
58
        value = []
 
59
        for n in node.childNodes:
 
60
            if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
 
61
                value.append(Attribute.get(n))
 
62
        return value
 
63
 
 
64
class CheckContentAttribute:
 
65
    '''CheckList content. Value is a list of tuples (checked, string).'''
 
66
    @staticmethod
 
67
    def add(parentNode, attribute, value):
 
68
        contentElem = Model.dom.createElement(attribute)
 
69
        parentNode.appendChild(contentElem)
 
70
        for item in value:
 
71
            try:
 
72
                checked, item = item
 
73
            except:
 
74
                checked = False
 
75
            elem = Model.dom.createElement('item')
 
76
            if checked:
 
77
                elem.setAttribute('checked', '1')
 
78
            text = Model.dom.createTextNode(item)
 
79
            elem.appendChild(text)
 
80
            contentElem.appendChild(elem)
 
81
    @staticmethod
 
82
    def get(node):
 
83
        if node is None: return []
 
84
        value = []
 
85
        for n in node.childNodes:
 
86
            if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
 
87
                try:
 
88
                    checked = int(n.getAttribute('checked'))
 
89
                except:
 
90
                    checked = 0
 
91
                value.append((checked, str(Attribute.get(n))))
 
92
        return value
 
93
 
 
94
class HelpContentAttribute:
 
95
    '''RadioBox content.  Value is a list of tuples (string, tooltip, help).'''
 
96
    @staticmethod
 
97
    def add(parentNode, attribute, value):
 
98
        contentElem = Model.dom.createElement(attribute)
 
99
        parentNode.appendChild(contentElem)
 
100
        for item in value:
 
101
            try:
 
102
                item, tooltip, helptext = item
 
103
            except:
 
104
                tooltip = helptext = ''
 
105
            elem = Model.dom.createElement('item')
 
106
            if tooltip:
 
107
                elem.setAttribute('tooltip', tooltip)
 
108
            if helptext:
 
109
                elem.setAttribute('helptext', helptext)
 
110
            text = Model.dom.createTextNode(item)
 
111
            elem.appendChild(text)
 
112
            contentElem.appendChild(elem)
 
113
    @staticmethod
 
114
    def get(node):
 
115
        if node is None: return []
 
116
        value = []
 
117
        for n in node.childNodes:
 
118
            if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
 
119
                tooltip = n.getAttribute('tooltip')
 
120
                helptext = n.getAttribute('helptext')
 
121
                value.append((str(Attribute.get(n)), str(tooltip), str(helptext)))
 
122
        return value
 
123
    
 
124
 
 
125
class DictAttribute:
 
126
    '''DictAttribute uses dictionary object for passing data.'''
 
127
    attributes = []
 
128
    @classmethod
 
129
    def add(cls, parentNode, attribute, value):
 
130
        fontElem = Model.dom.createElement(attribute)
 
131
        parentNode.appendChild(fontElem)
 
132
        for a in filter(value.has_key, cls.attributes):
 
133
            elem = Model.dom.createElement(a)
 
134
            text = Model.dom.createTextNode(value[a])
 
135
            elem.appendChild(text)
 
136
            fontElem.appendChild(elem)
 
137
    @staticmethod
 
138
    def get(node):
 
139
        if node is None: return {}
 
140
        value = {}
 
141
        for n in node.childNodes:
 
142
            if n.nodeType == node.ELEMENT_NODE:
 
143
                value[n.tagName] = Attribute.get(n)
 
144
        return value
 
145
 
 
146
class FontAttribute(DictAttribute):
 
147
    attributes = ['size', 'style', 'weight', 'underlined', 'family', 'face', 'encoding', 
 
148
                  'sysfont', 'relativesize']
 
149
 
 
150
class CodeAttribute(DictAttribute):
 
151
    attributes = ['events', 'assign_var']
 
152
 
 
153
class MultiAttribute:
 
154
    '''Repeated attribute like growablecols.'''
 
155
    @staticmethod
 
156
    def add(parentNode, attribute, value):
 
157
        for v in value:
 
158
            elem = Model.dom.createElement(attribute)
 
159
            parentNode.appendChild(elem)
 
160
            text = Model.dom.createTextNode(v)
 
161
            elem.appendChild(text)
 
162
    @staticmethod
 
163
    def get(node):
 
164
        if node is None: return []
 
165
        tag = node.tagName  # remember tag name
 
166
        value = []
 
167
        # Look for multiple tags
 
168
        while node:
 
169
            if node.nodeType == node.ELEMENT_NODE and node.tagName == tag:
 
170
                value.append(Attribute.get(node))
 
171
            node = node.nextSibling
 
172
        return value
 
173
 
 
174
class BitmapAttribute:
 
175
    '''Bitmap attribute.'''
 
176
    @staticmethod
 
177
    def add(parentNode, attribute, value):
 
178
        if not value[0] and not value[1]: return
 
179
        if attribute == 'object':
 
180
            elem = parentNode
 
181
        else:
 
182
            elem = Model.dom.createElement(attribute)
 
183
            parentNode.appendChild(elem)
 
184
        if value[0]:
 
185
            elem.setAttribute('stock_id', value[0])
 
186
        else:
 
187
            if elem.hasAttribute('stock_id'): elem.removeAttribute('stock_id')
 
188
            text = Model.dom.createTextNode(value[1])
 
189
            elem.appendChild(text)
 
190
    @staticmethod
 
191
    def get(node):
 
192
        if node is None: return []
 
193
        return [node.getAttribute('stock_id'), Attribute.get(node)]
 
194
            
 
195
class AttributeAttribute:
 
196
    '''Attribute as an XML attribute of the element node.'''
 
197
    @staticmethod
 
198
    def add(elem, attribute, value):
 
199
        if value:
 
200
            elem.setAttribute(attribute, value)
 
201
        else:
 
202
            if elem.hasAttribute(attribute): elem.removeAttribute(attribute)
 
203
    @staticmethod
 
204
    def getAA(elem, attribute):
 
205
        return elem.getAttribute(attribute)
 
206
 
 
207
class EncodingAttribute(AttributeAttribute):
 
208
    '''Encoding is a special attribute stored in dom object.'''
 
209
    @staticmethod
 
210
    def add(elem, attribute, value):
 
211
        Model.dom.encoding = value
 
212
    @staticmethod
 
213
    def getAA(elem, attribute):
 
214
        return Model.dom.encoding
 
215
            
 
216
class CDATAAttribute(Attribute):
 
217
    def add(parentNode, attribute, value):
 
218
        '''value is a dictionary.'''
 
219
        if value:
 
220
            elem = Model.dom.createElement(attribute)
 
221
            parentNode.appendChild(elem)
 
222
            data = Model.dom.createCDATASection(cPickle.dumps(value))
 
223
            elem.appendChild(data)
 
224
    @staticmethod
 
225
    def get(node):
 
226
        '''Get XRCED data from a CDATA text node.'''
 
227
        try:
 
228
            n = node.childNodes[0]
 
229
            if n.nodeType == n.CDATA_SECTION_NODE:
 
230
                return cPickle.loads(n.wholeText.encode())
 
231
        except IndexError:
 
232
            pass
 
233
    
 
234
class CommentAttribute(AttributeAttribute):
 
235
    '''Comment is the value of comment object.'''
 
236
    @staticmethod
 
237
    def add(node, attribute, value):
 
238
        node.data = value
 
239
    @staticmethod
 
240
    def getAA(node, attribute):
 
241
        return node.data
 
242