Package pyxsd :: Package elementRepresentatives :: Module element
[hide private]
[frames] | no frames]

Source Code for Module pyxsd.elementRepresentatives.element

  1  from elementRepresentative import ElementRepresentative 
  2  from pyxsd.xsdDataTypes    import * 
  3   
  4   
  5  #============================================================ 
  6  # 
7 -class Element(ElementRepresentative):
8 """ 9 The class for the element tag. Subclass of *ElementRepresentative*. 10 The element tag and the attribute tag are the most important in the 11 xml and in the program, so this class contains some machinery that 12 many of the other classes do not have. The element and attribute 13 classes contain descriptor methods. By specifing the `__get__`, 14 `__set__`, and `__delete__` (with `__get__` and `__set__` being the 15 most important). These methods specify how a variable is set and 16 how it retrieved. Any modification of these methods should be made 17 under extreme caution! Both Element and Attribute, primarily 18 Attribute, use these descriptors to add a level oof checking to the 19 program. If some variable is set to some value that does no match 20 the specifications in the schema, an error will be raised. These 21 methods add a powerful layer of functionality, with a small amount 22 of code; however, these functions are almost invisible unless they 23 raise an error, so developers should bear in mind these methods 24 when modifying the program. 25 """
26 - def __init__(self, xsdElement, parent):
27 """ 28 Adds itself to the element list in its parent. 29 See *ElementRepresentative* for documentation. 30 """ 31 32 ElementRepresentative.__init__(self, xsdElement, parent) 33 34 parent.elements.append(self)
35 36 #============================================================ 37 #
38 - def getType(self):
39 """ 40 Returns its type from the class dictionary in *PyXSD*. The 41 instance of *PyXSD* is attached to every element and attribute 42 while the classes for the schema types are being built. Clearly, 43 this function is used after the main ER run. 44 """ 45 if not 'type' in self.__dict__: 46 47 raise "Element.getType() Error: type is not in %s's dictionary." 48 return None 49 50 if self.type in self.pyXSD.classes.keys(): 51 return self.pyXSD.classes[self.type] 52 53 return ElementRepresentative.typeFromName(self.type, self.pyXSD)
54 55 #============================================================ 56 #
57 - def processChildren(self):
58 """ 59 There is a special processChildren() here to handle special types, 60 which can be declared as a child of an element. If an element 61 child can exist that is not a type, then this function will screw 62 it up; however, as far as the developers knew at the time of writing 63 this program, they cannot. 64 65 No parameters. 66 """ 67 children = self.xsdElement.getchildren() 68 69 if len(children) == 0: return None 70 71 for child in children: 72 73 processedChild = ElementRepresentative.factory(child, self) 74 75 self.processedChildren.append(processedChild) 76 77 self.type = processedChild.name 78 79 self.tagAttributes['type'] = self.type 80 81 processedChild.processChildren()
82 83 #============================================================ 84 #
85 - def __str__(self):
86 """ 87 Prints its name in a form that allows for quick identification 88 of an element, without needing a bulky name that does not 89 match the name used. 90 """ 91 return "%s|%s|%s" % (ElementRepresentative.getContainingTypeName(self), \ 92 self.__class__.__name__, self.name)
93 94 #============================================================ Descriptor Protocol 95 #============================================================ 96 #
97 - def __get__(self, obj, mystery=None):
98 """ 99 Gets an element value from the obj's dictionary. Returns 100 it value if it has one, returns the default value if it 101 does not. 102 103 The second argument is called `mystery`. It does not do 104 anything, so it should not really be thought of as a 105 parameter to this method. This variable is needed because of, 106 you guessed it, a mystery. Sometimes, but not every time, 107 the obj is passed in as an instance and it corresponding 108 class. Perhaps it is slightly different, but it does this 109 very odd thing with only some of the attributes/elements. 110 As far as I can tell, the 'mystery' does not cause any 111 problems, but it is a bug, and should be fixed if 112 possible. 113 114 See python documentation for full documentation on 115 descriptors. Some links should be included on the 116 `pyXSD website <http://pyxsd.org>`_. 117 """ 118 119 if self.name in obj.__dict__: 120 return obj.__dict__[self.name] 121 122 default = getattr(self, 'default', None) 123 return default
124 125 #============================================================ 126 #
127 - def __set__(self, obj, value):
128 """ 129 Sets an element's name to the element in the obj's dictionary. 130 If multiple elements exist, sets it to a list. If it is not 131 an element, raises an error. Has code for case when it is dictionary, 132 but no case in which a dictionary would be used. 133 134 See python documentation for full documentation on 135 descriptors. Some links should be included on the 136 `pyXSD website <http://pyxsd.org>`_. 137 """ 138 if not isinstance(value, self.getType()): 139 raise Exception() 140 141 value = obj.__dict__.get(self.name,None) 142 143 if self.isList(): 144 if value == None: 145 obj.__dict__[self.name] = [] 146 obj.__dict__[self.name].append(value) 147 return 148 149 if self.isDict(): 150 if value == None: 151 obj.__dict__[self.name] = {} 152 obj.__dict__[self.name][obj.id] = value 153 return 154 155 obj.__dict__[self.name] = value
156 157 #============================================================ 158 #
159 - def __delete__(self, obj):
160 """ 161 Deletes an entry from the dictionary. 162 163 See python documentation for full documentation on 164 descriptors. Some links should be included on the 165 `pyXSD website <http://pyxsd.org>`_. 166 """ 167 168 del obj.__dict__[self.name]
169 170 #============================================================ 171 #
172 - def isDict (self):
173 """ 174 Returns false. Placeholder function for possible future 175 addition of case where the an element could best be 176 expressed as a dictionary. 177 178 No parameters 179 """ 180 return False
181 182 #============================================================ 183 #
184 - def isList (self):
185 """ 186 Returns true if maxOccurs is greater than one. If it is 187 true, treats all of the elements that are from the schema 188 definition as a list. Otherwise returns false. 189 190 No parameters. 191 """ 192 minOccurs = self.getMinOccurs() 193 194 maxOccurs = self.getMaxOccurs() 195 196 if maxOccurs > 1: 197 198 return True 199 200 return False
201 202 #============================================================ 203 #
204 - def getMinOccurs(self):
205 """ 206 Returns an integer value for the `minOccurs`. If no `minOccurs` 207 has been set, uses default of 1. 208 209 No parameters 210 """ 211 return int(getattr(self, 'minOccurs', 1))
212 213 #============================================================ 214 #
215 - def getMaxOccurs(self):
216 """ 217 Returns an integer value for the `maxOccurs`. If no `maxOccurs` 218 has been set, uses default of 1. If `maxOccurs` is set to 219 'unbounded', returns 99999, since this should cover about 220 every case in which someone would use 'unbounded' 221 222 No parameters 223 """ 224 maxOccurs = getattr(self, 'maxOccurs', 1) 225 if maxOccurs == 'unbounded': 226 return 99999 227 return int(maxOccurs)
228