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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# Name: attribute.py
# Purpose: Attribute classes
# Author: Roman Rolinsky <rolinsky@femagsoft.com>
# Created: 25.06.2007
# RCS-ID: $Id$
'''
Attribute processing classes.
This module contains some predefined classes which can be used to store and
retrieve XML data into Python objects.
'''
import cPickle
from model import Model
class Attribute:
'''Base class, used for simple attributes, i.e. single attributes
storing data as text strings.'''
@staticmethod
def add(parentNode, attribute, value):
'''Store attribute value in DOM as a text node.
@param attribute: Attribute name.
@param value: Attribute value (Python string).
'''
if attribute == '':
elem = parentNode
else:
elem = Model.dom.createElement(attribute)
parentNode.appendChild(elem)
text = Model.dom.createTextNode(value)
elem.appendChild(text)
@staticmethod
def get(node):
'''Get value (or return a default value) from a DOM C{Element} object.'''
if node is None: return ''
try:
n = node.childNodes[0]
return n.wholeText
except IndexError:
return ''
class ContentAttribute:
'''Content attribute class. Value is a list of strings.'''
@staticmethod
def add(parentNode, attribute, value):
contentElem = Model.dom.createElement(attribute)
parentNode.appendChild(contentElem)
for item in value:
elem = Model.dom.createElement('item')
text = Model.dom.createTextNode(item)
elem.appendChild(text)
contentElem.appendChild(elem)
@staticmethod
def get(node):
if node is None: return []
value = []
for n in node.childNodes:
if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
value.append(Attribute.get(n))
return value
class CheckContentAttribute:
'''CheckList content. Value is a list of tuples (checked, string).'''
@staticmethod
def add(parentNode, attribute, value):
contentElem = Model.dom.createElement(attribute)
parentNode.appendChild(contentElem)
for item in value:
try:
checked, item = item
except:
checked = False
elem = Model.dom.createElement('item')
if checked:
elem.setAttribute('checked', '1')
text = Model.dom.createTextNode(item)
elem.appendChild(text)
contentElem.appendChild(elem)
@staticmethod
def get(node):
if node is None: return []
value = []
for n in node.childNodes:
if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
try:
checked = int(n.getAttribute('checked'))
except:
checked = 0
value.append((checked, str(Attribute.get(n))))
return value
class HelpContentAttribute:
'''RadioBox content. Value is a list of tuples (string, tooltip, help).'''
@staticmethod
def add(parentNode, attribute, value):
contentElem = Model.dom.createElement(attribute)
parentNode.appendChild(contentElem)
for item in value:
try:
item, tooltip, helptext = item
except:
tooltip = helptext = ''
elem = Model.dom.createElement('item')
if tooltip:
elem.setAttribute('tooltip', tooltip)
if helptext:
elem.setAttribute('helptext', helptext)
text = Model.dom.createTextNode(item)
elem.appendChild(text)
contentElem.appendChild(elem)
@staticmethod
def get(node):
if node is None: return []
value = []
for n in node.childNodes:
if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
tooltip = n.getAttribute('tooltip')
helptext = n.getAttribute('helptext')
value.append((str(Attribute.get(n)), str(tooltip), str(helptext)))
return value
class DictAttribute:
'''DictAttribute uses dictionary object for passing data.'''
attributes = []
@classmethod
def add(cls, parentNode, attribute, value):
fontElem = Model.dom.createElement(attribute)
parentNode.appendChild(fontElem)
for a in filter(value.has_key, cls.attributes):
elem = Model.dom.createElement(a)
text = Model.dom.createTextNode(value[a])
elem.appendChild(text)
fontElem.appendChild(elem)
@staticmethod
def get(node):
if node is None: return {}
value = {}
for n in node.childNodes:
if n.nodeType == node.ELEMENT_NODE:
value[n.tagName] = Attribute.get(n)
return value
class FontAttribute(DictAttribute):
attributes = ['size', 'style', 'weight', 'underlined', 'family', 'face', 'encoding',
'sysfont', 'relativesize']
class CodeAttribute(DictAttribute):
attributes = ['events', 'assign_var']
class MultiAttribute:
'''Repeated attribute like growablecols.'''
@staticmethod
def add(parentNode, attribute, value):
for v in value:
elem = Model.dom.createElement(attribute)
parentNode.appendChild(elem)
text = Model.dom.createTextNode(v)
elem.appendChild(text)
@staticmethod
def get(node):
if node is None: return []
tag = node.tagName # remember tag name
value = []
# Look for multiple tags
while node:
if node.nodeType == node.ELEMENT_NODE and node.tagName == tag:
value.append(Attribute.get(node))
node = node.nextSibling
return value
class BitmapAttribute:
'''Bitmap attribute.'''
@staticmethod
def add(parentNode, attribute, value):
if not value[0] and not value[1]: return
if attribute == 'object':
elem = parentNode
else:
elem = Model.dom.createElement(attribute)
parentNode.appendChild(elem)
if value[0]:
elem.setAttribute('stock_id', value[0])
else:
if elem.hasAttribute('stock_id'): elem.removeAttribute('stock_id')
text = Model.dom.createTextNode(value[1])
elem.appendChild(text)
@staticmethod
def get(node):
if node is None: return []
return [node.getAttribute('stock_id'), Attribute.get(node)]
class AttributeAttribute:
'''Attribute as an XML attribute of the element node.'''
@staticmethod
def add(elem, attribute, value):
if value:
elem.setAttribute(attribute, value)
else:
if elem.hasAttribute(attribute): elem.removeAttribute(attribute)
@staticmethod
def getAA(elem, attribute):
return elem.getAttribute(attribute)
class EncodingAttribute(AttributeAttribute):
'''Encoding is a special attribute stored in dom object.'''
@staticmethod
def add(elem, attribute, value):
Model.dom.encoding = value
@staticmethod
def getAA(elem, attribute):
return Model.dom.encoding
class CDATAAttribute(Attribute):
def add(parentNode, attribute, value):
'''value is a dictionary.'''
if value:
elem = Model.dom.createElement(attribute)
parentNode.appendChild(elem)
data = Model.dom.createCDATASection(cPickle.dumps(value))
elem.appendChild(data)
@staticmethod
def get(node):
'''Get XRCED data from a CDATA text node.'''
try:
n = node.childNodes[0]
if n.nodeType == n.CDATA_SECTION_NODE:
return cPickle.loads(n.wholeText.encode())
except IndexError:
pass
class CommentAttribute(AttributeAttribute):
'''Comment is the value of comment object.'''
@staticmethod
def add(node, attribute, value):
node.data = value
@staticmethod
def getAA(node, attribute):
return node.data
|