2
# Purpose: Attribute classes
3
# Author: Roman Rolinsky <rolinsky@femagsoft.com>
8
Attribute processing classes.
10
This module contains some predefined classes which can be used to store and
11
retrieve XML data into Python objects.
15
from model import Model
18
'''Base class, used for simple attributes, i.e. single attributes
19
storing data as text strings.'''
21
def add(parentNode, attribute, value):
22
'''Store attribute value in DOM as a text node.
24
@param attribute: Attribute name.
25
@param value: Attribute value (Python string).
30
elem = Model.dom.createElement(attribute)
31
parentNode.appendChild(elem)
32
text = Model.dom.createTextNode(value)
33
elem.appendChild(text)
36
'''Get value (or return a default value) from a DOM C{Element} object.'''
37
if node is None: return ''
39
n = node.childNodes[0]
44
class ContentAttribute:
45
'''Content attribute class. Value is a list of strings.'''
47
def add(parentNode, attribute, value):
48
contentElem = Model.dom.createElement(attribute)
49
parentNode.appendChild(contentElem)
51
elem = Model.dom.createElement('item')
52
text = Model.dom.createTextNode(item)
53
elem.appendChild(text)
54
contentElem.appendChild(elem)
57
if node is None: return []
59
for n in node.childNodes:
60
if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
61
value.append(Attribute.get(n))
64
class CheckContentAttribute:
65
'''CheckList content. Value is a list of tuples (checked, string).'''
67
def add(parentNode, attribute, value):
68
contentElem = Model.dom.createElement(attribute)
69
parentNode.appendChild(contentElem)
75
elem = Model.dom.createElement('item')
77
elem.setAttribute('checked', '1')
78
text = Model.dom.createTextNode(item)
79
elem.appendChild(text)
80
contentElem.appendChild(elem)
83
if node is None: return []
85
for n in node.childNodes:
86
if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item':
88
checked = int(n.getAttribute('checked'))
91
value.append((checked, str(Attribute.get(n))))
94
class HelpContentAttribute:
95
'''RadioBox content. Value is a list of tuples (string, tooltip, help).'''
97
def add(parentNode, attribute, value):
98
contentElem = Model.dom.createElement(attribute)
99
parentNode.appendChild(contentElem)
102
item, tooltip, helptext = item
104
tooltip = helptext = ''
105
elem = Model.dom.createElement('item')
107
elem.setAttribute('tooltip', tooltip)
109
elem.setAttribute('helptext', helptext)
110
text = Model.dom.createTextNode(item)
111
elem.appendChild(text)
112
contentElem.appendChild(elem)
115
if node is None: return []
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)))
126
'''DictAttribute uses dictionary object for passing data.'''
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)
139
if node is None: return {}
141
for n in node.childNodes:
142
if n.nodeType == node.ELEMENT_NODE:
143
value[n.tagName] = Attribute.get(n)
146
class FontAttribute(DictAttribute):
147
attributes = ['size', 'style', 'weight', 'underlined', 'family', 'face', 'encoding',
148
'sysfont', 'relativesize']
150
class CodeAttribute(DictAttribute):
151
attributes = ['events', 'assign_var']
153
class MultiAttribute:
154
'''Repeated attribute like growablecols.'''
156
def add(parentNode, attribute, value):
158
elem = Model.dom.createElement(attribute)
159
parentNode.appendChild(elem)
160
text = Model.dom.createTextNode(v)
161
elem.appendChild(text)
164
if node is None: return []
165
tag = node.tagName # remember tag name
167
# Look for multiple tags
169
if node.nodeType == node.ELEMENT_NODE and node.tagName == tag:
170
value.append(Attribute.get(node))
171
node = node.nextSibling
174
class BitmapAttribute:
175
'''Bitmap attribute.'''
177
def add(parentNode, attribute, value):
178
if not value[0] and not value[1]: return
179
if attribute == 'object':
182
elem = Model.dom.createElement(attribute)
183
parentNode.appendChild(elem)
185
elem.setAttribute('stock_id', value[0])
187
if elem.hasAttribute('stock_id'): elem.removeAttribute('stock_id')
188
text = Model.dom.createTextNode(value[1])
189
elem.appendChild(text)
192
if node is None: return []
193
return [node.getAttribute('stock_id'), Attribute.get(node)]
195
class AttributeAttribute:
196
'''Attribute as an XML attribute of the element node.'''
198
def add(elem, attribute, value):
200
elem.setAttribute(attribute, value)
202
if elem.hasAttribute(attribute): elem.removeAttribute(attribute)
204
def getAA(elem, attribute):
205
return elem.getAttribute(attribute)
207
class EncodingAttribute(AttributeAttribute):
208
'''Encoding is a special attribute stored in dom object.'''
210
def add(elem, attribute, value):
211
Model.dom.encoding = value
213
def getAA(elem, attribute):
214
return Model.dom.encoding
216
class CDATAAttribute(Attribute):
217
def add(parentNode, attribute, value):
218
'''value is a dictionary.'''
220
elem = Model.dom.createElement(attribute)
221
parentNode.appendChild(elem)
222
data = Model.dom.createCDATASection(cPickle.dumps(value))
223
elem.appendChild(data)
226
'''Get XRCED data from a CDATA text node.'''
228
n = node.childNodes[0]
229
if n.nodeType == n.CDATA_SECTION_NODE:
230
return cPickle.loads(n.wholeText.encode())
234
class CommentAttribute(AttributeAttribute):
235
'''Comment is the value of comment object.'''
237
def add(node, attribute, value):
240
def getAA(node, attribute):