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

Source Code for Module pyxsd.elementRepresentatives.attribute

  1  from elementRepresentative import ElementRepresentative 
  2  from pyxsd.xsdDataTypes    import * 
  3   
  4  #============================================================ 
  5  # 
6 -class Attribute(ElementRepresentative):
7 """ 8 The class for the attribute tag. Subclass of *ElementRepresentative*. 9 The element tag and the attribute tag are the most important in the 10 xml and in the program, so this class contains some machinery that 11 many of the other classes do not have. The element and attribute 12 classes contain descriptor methods. By specifing the `__get__`, 13 `__set__`, and `__delete__` (with `__get__` and `__set__` being the 14 most important). These methods specify how a variable is set and 15 how it retrieved. Any modification of these methods should be made 16 under extreme caution! Both Element and Attribute, primarily 17 Attribute, use these descriptors to add a level oof checking to the 18 program. If some variable is set to some value that does no match 19 the specifications in the schema, an error will be raised. These 20 methods add a powerful layer of functionality, with a small amount 21 of code; however, these functions are almost invisible unless they 22 raise an error, so developers should bear in mind these methods 23 when modifying the program. 24 """
25 - def __init__(self, xsdElement, parent):
26 """ 27 Adds itself to the attribute dictionary in its containing 28 type. See *ElementRepresentative* for documentation. 29 """ 30 ElementRepresentative.__init__(self, xsdElement, parent) 31 32 self.getContainingType().attributes[self.name] = self
33 34 #============================================================ 35 #
36 - def __str__(self):
37 """ 38 Prints its name in a form that allows for quick identification 39 of an attribute, without needing a bulky name that does not 40 match the name used. 41 """ 42 return "%s|%s|%s" % (self.getContainingTypeName(), 43 self.__class__.__name__, 44 ElementRepresentative.getName(self))
45 46 47 #============================================================ 48 #
49 - def processChildren(self):
50 """ 51 There is a special processChildren() here to handle special types, 52 which can be declared as a child of an attribute. If an attribute 53 child can exist that is not a type, then this function will screw 54 it up; however, as far as the developers knew at the time of writing 55 this program, they cannot. 56 57 No parameters. 58 """ 59 children = self.xsdElement.getchildren() 60 61 if len(children) == 0: return None 62 63 for child in children: 64 65 processedChild = ElementRepresentative.factory(child, self) 66 67 self.processedChildren.append(processedChild) 68 69 self.type = processedChild.name 70 71 self.tagAttributes['type'] = self.type 72 73 processedChild.processChildren()
74 75 76 #============================================================ 77 #
78 - def getType(self):
79 """ 80 Returns its type from the class dictionary in *PyXSD*. The 81 instance of *PyXSD* is attached to every element and attribute 82 while the classes for the schema types are being built. Clearly, 83 this function is used after the main ER run. 84 """ 85 if not 'type' in self.__dict__: 86 87 raise "Attribute.getType() Error: type is not in %s's dictionary." 88 return None 89 90 if self.type in self.pyXSD.classes.keys(): 91 return self.pyXSD.classes[self.type] 92 93 return ElementRepresentative.typeFromName(self.type, self.pyXSD)
94 95 96 97 #============================================================ Descriptor Protocol 98 #============================================================ 99 #
100 - def __get__(self, obj, mystery=None):
101 """ 102 Gets an attribute value from the obj's dictionary. Returns 103 it value if it has one, returns the default value if it 104 does not. 105 106 The second argument is called `mystery`. It does not do 107 anything, so it should not really be thought of as a 108 parameter to this method. This variable is needed because of, 109 you guessed it, a mystery. Sometimes, but not every time, 110 the obj is passed in as an instance and it corresponding 111 class. Perhaps it is slightly different, but it does this 112 very odd thing with only some of the attributes/elements. 113 As far as I can tell, the 'mystery' does not cause any 114 problems, but it is a bug, and should be fixed if 115 possible. 116 117 See python documentation for full documentation on 118 descriptors. Some links should be included on the 119 `pyXSD website <http://pyxsd.org>`_. 120 """ 121 122 if self.name in obj.__dict__: 123 return obj.__dict__[self.name] 124 125 default = getattr(self, 'default', None) 126 return default
127 128 #============================================================ 129 #
130 - def __set__(self, obj, value):
131 """ 132 Sets values to attributes. Converts text Boolean values to 133 binary values (integers of 0 and 1). 134 135 See python documentation for full documentation on 136 descriptors. Some links should be included on the 137 `pyXSD website <http://pyxsd.org>`_. 138 """ 139 if issubclass(self.getType(), XsdDataType): 140 if self.getType() == Boolean: 141 if isinstance(value, basestring): 142 143 if value == 'true' or value == 'True': 144 145 value = 1 146 147 elif value =='False' or value =='false': 148 149 value = 0 150 try: 151 value = self.getType()(value) 152 except Exception, e: 153 print 154 print "Parser Error: One of your attributes is invalid." 155 print "The program's error message is as follows:" 156 print " %s" % e 157 print "The program will attempt to continue, but may experience errors." 158 print 159 elif not isinstance(obj, self.getType()): 160 161 raise Exception() 162 163 obj.__dict__[self.name] = value
164 165 #============================================================ 166 #
167 - def __delete__(self, obj):
168 """ 169 Deletes an entry from the dictionary. 170 171 See python documentation for full documentation on 172 descriptors. Some links should be included on the 173 `pyXSD website <http://pyxsd.org>`_. 174 """ 175 del obj.__dict__[self.name]
176 177 #============================================================ 178 #
179 - def getUse(self):
180 """ 181 Returns the 'use' value, which says if the attribute is required 182 or the default optional. 183 """ 184 if not 'use' in self.__dict__.keys(): 185 self.use = 'optional' 186 return self.use
187