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

Source Code for Module pyxsd.elementRepresentatives.xsdType

  1  import sys 
  2  from elementRepresentative import ElementRepresentative 
  3   
  4  #============================================================ 
  5  # 
6 -class XsdType(ElementRepresentative):
7 """ 8 This class is the base class for *SimpleType* and *ComplexType* 9 Subclass of *ElementRepresentative*. In this class, the classes 10 for all of the types are generated. 11 """
12 - def __init__(self, xsdElement, parent):
13 """ 14 The `__init__` for this class' subclasses. 15 Creates a blank list for enumerations. 16 Creates a blank dictionary for attributes. 17 See *ElementRepresentative* for documentation. 18 """ 19 self.enumerations = [] 20 21 self.attributes = {} 22 23 ElementRepresentative.__init__(self, xsdElement, parent)
24 25 26 #============================================================ 27 #
28 - def getContainingTypeName(self):
29 """ 30 Since all types are containing types, this method returns 31 its own name. 32 33 No parameters. 34 """ 35 if self.name == None: 36 37 self.name = self.getName() 38 39 return self.name
40 41 #============================================================ 42 #
43 - def getContainingType(self):
44 """ 45 All types are containing types, so this works for all of the 46 XSD types. 47 48 No parameters. 49 """ 50 return self
51 52 #============================================================ 53 #
54 - def getName(self):
55 """ 56 Mostly normal getName(), except it includes a means to make 57 a type name if the type is the child of an element or 58 some other tag. That name should look like this: 59 60 `elementName`|`type tag type` 61 62 No parameters 63 """ 64 name = ElementRepresentative.getName(self) 65 66 if not name == None: return name 67 68 # We are implicitly defined in an element 69 element = self.parent 70 71 name = '%s|%s' % (element.name, self.tagType) 72 73 element.typeName = name 74 75 return name
76 77 #============================================================ 78 #
79 - def containsSchemaBase(self, bases):
80 """ 81 Returns true if *SchemaBase* is in the bases list, false 82 if it is not. Used by getBaseList() 83 84 Parameters: 85 86 - `bases`- the list of bases for the class that will be created 87 88 """ 89 for base in bases: 90 91 if issubclass(base,SchemaBase): 92 return True 93 94 return False
95 #============================================================ 96 #
97 - def getBaseList(self, pyXSD):
98 """ 99 Creates blank list for the base classes. It goes through 100 the list of super classes to be added (all type classes), 101 and adds them. Adds SchemaBase, if it is not already added. 102 Returns the list as a tuple, since the type factory must 103 have the bases stored in a tuple, not a list. 104 105 Parameters: 106 107 - `pyXSD`- The *PyXSD* instance. 108 109 """ 110 baseList = [] 111 112 for superClassName in self.superClassNames: 113 baseList.append(ElementRepresentative.typeFromName \ 114 (superClassName, pyXSD)) 115 116 if not self.containsSchemaBase(baseList): 117 118 baseList.append(SchemaBase) 119 120 return tuple(baseList)
121 122 #============================================================ 123 #
124 - def getElements(self):
125 """ 126 Returns a blank list. Subclasses uses this function to 127 return elements, but this function is called elsewhere 128 on all of the types. 129 130 No parameters. 131 """ 132 return []
133 134 #============================================================ 135 #
136 - def clsFor(self, pyXSD):
137 """ 138 Produces a class for a schema type. In this class, because this function 139 is only makes classes for tag types that are subclasses of *XsdType*. 140 Adds functions to the class dictionary to get elements and attributes 141 later on. Calls getBaseList() to generate the list of bases. SchemaBase 142 is in every base list, which will come into play after the class generation. 143 Adds the name and the doc string to the dictionary. Adds the instance of 144 *PyXSD* to all attributes, elements, and the class dictionary, so it can 145 be accessed later on. In future versions, hopefully this operation will 146 be done in a metaclass. 147 148 Parameters: 149 150 - `pyXSD`- The *PyXSD* instance. 151 """ 152 153 bases = self.getBaseList(pyXSD) 154 155 #arrange base so that it always inherits from SchemaBase (SchemaBase inherits from object) 156 clsDict = dict([('pyXSD', pyXSD), ('name', self.name), \ 157 ('__doc__', self.__doc__)]) 158 159 _elementNames_ = [] 160 161 for element in self.getElements(): 162 element.pyXSD = pyXSD 163 _elementNames_.append(element.name) 164 clsDict[element.name] = element 165 166 clsDict['_elementNames_'] = _elementNames_ 167 168 def _getElements(cls): 169 elements = [] 170 for elemName in cls._elementNames_: 171 element = cls.__class__.__dict__[elemName] 172 elements.append(element) 173 return elements
174 175 clsDict['_getElements'] = _getElements 176 177 _attributeNames_ = self.attributes.keys() 178 179 clsDict['_attributeNames_'] = _attributeNames_ 180 181 def _getAttributes(cls): 182 attrs = [] 183 for attrName in cls._attributeNames_: 184 attr = cls.__dict__[attrName] 185 attrs.append(attr) 186 return attrs 187 188 clsDict['_getAttributes'] = _getAttributes 189 for attr in self.attributes.values(): 190 attr.pyXSD = pyXSD 191 clsDict.update(self.attributes) 192 193 try: 194 cls = type(self.name, bases, clsDict) 195 196 except Exception, e: 197 print e 198 self.describe() 199 print "baseList %s" % repr(self.superClassNames) 200 print "baseList %s" % repr(baseList) 201 raise 202 return None 203 204 return cls 205 206 #====================================================================== 207 # 208 from pyxsd.schemaBase import SchemaBase 209