Module attribute
[hide private]
[frames] | no frames]

Source Code for Module attribute

  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 checked,item in value: 71 elem = Model.dom.createElement('item') 72 if checked: 73 elem.setAttribute('checked', '1') 74 text = Model.dom.createTextNode(item) 75 elem.appendChild(text) 76 contentElem.appendChild(elem)
77 @staticmethod
78 - def get(node):
79 if node is None: return [] 80 value = [] 81 for n in node.childNodes: 82 if n.nodeType == node.ELEMENT_NODE and n.tagName == 'item': 83 checked = bool(n.getAttribute('checked')) 84 value.append((checked, Attribute.get(n))) 85 return value
86
87 -class DictAttribute:
88 '''DictAttribute uses dictionary object for passing data.''' 89 attributes = [] 90 @classmethod
91 - def add(cls, parentNode, attribute, value):
92 fontElem = Model.dom.createElement(attribute) 93 parentNode.appendChild(fontElem) 94 for a in filter(value.has_key, cls.attributes): 95 elem = Model.dom.createElement(a) 96 text = Model.dom.createTextNode(value[a]) 97 elem.appendChild(text) 98 fontElem.appendChild(elem)
99 @staticmethod
100 - def get(node):
101 if node is None: return {} 102 value = {} 103 for n in node.childNodes: 104 if n.nodeType == node.ELEMENT_NODE: 105 value[n.tagName] = Attribute.get(n) 106 return value
107
108 -class FontAttribute(DictAttribute):
109 attributes = ['size', 'style', 'weight', 'underlined', 'family', 'face', 'encoding', 110 'sysfont', 'relativesize']
111
112 -class CodeAttribute(DictAttribute):
113 attributes = ['events', 'assign_var']
114
115 -class MultiAttribute:
116 '''Repeated attribute like growablecols.''' 117 @staticmethod
118 - def add(parentNode, attribute, value):
119 for v in value: 120 elem = Model.dom.createElement(attribute) 121 parentNode.appendChild(elem) 122 text = Model.dom.createTextNode(v) 123 elem.appendChild(text)
124 @staticmethod
125 - def get(node):
126 if node is None: return [] 127 tag = node.tagName # remember tag name 128 value = [] 129 # Look for multiple tags 130 while node: 131 if node.nodeType == node.ELEMENT_NODE and node.tagName == tag: 132 value.append(Attribute.get(node)) 133 node = node.nextSibling 134 return value
135
136 -class BitmapAttribute:
137 '''Bitmap attribute.''' 138 @staticmethod
139 - def add(parentNode, attribute, value):
140 if not value[0] and not value[1]: return 141 if attribute == 'object': 142 elem = parentNode 143 else: 144 elem = Model.dom.createElement(attribute) 145 parentNode.appendChild(elem) 146 if value[0]: 147 elem.setAttribute('stock_id', value[0]) 148 else: 149 if elem.hasAttribute('stock_id'): elem.removeAttribute('stock_id') 150 text = Model.dom.createTextNode(value[1]) 151 elem.appendChild(text)
152 @staticmethod
153 - def get(node):
154 if node is None: return [] 155 return [node.getAttribute('stock_id'), Attribute.get(node)]
156
157 -class AttributeAttribute:
158 '''Attribute as an XML attribute of the element node.''' 159 @staticmethod
160 - def add(elem, attribute, value):
161 if value: 162 elem.setAttribute(attribute, value) 163 else: 164 if elem.hasAttribute(attribute): elem.removeAttribute(attribute)
165 @staticmethod
166 - def getAA(elem, attribute):
167 return elem.getAttribute(attribute)
168
169 -class EncodingAttribute(AttributeAttribute):
170 '''Encoding is a special attribute stored in dom object.''' 171 @staticmethod
172 - def add(elem, attribute, value):
173 Model.dom.encoding = value
174 @staticmethod
175 - def getAA(elem, attribute):
176 return Model.dom.encoding
177
178 -class CDATAAttribute(Attribute):
179 - def add(parentNode, attribute, value):
180 '''value is a dictionary.''' 181 if value: 182 elem = Model.dom.createElement(attribute) 183 parentNode.appendChild(elem) 184 data = Model.dom.createCDATASection(cPickle.dumps(value)) 185 elem.appendChild(data)
186 @staticmethod
187 - def get(node):
188 '''Get XRCED data from a CDATA text node.''' 189 try: 190 n = node.childNodes[0] 191 if n.nodeType == n.CDATA_SECTION_NODE: 192 return cPickle.loads(n.wholeText.encode()) 193 except IndexError: 194 pass
195