Package pyxsd :: Module xsdDataTypes
[hide private]
[frames] | no frames]

Source Code for Module pyxsd.xsdDataTypes

  1  import types 
  2  import base64 
  3   
  4  """ 
  5  This module contains classes of primitive data types found in xml xsd files. 
  6  These check of make sure that the value assigned to an element is of the proper data type. 
  7  Many correspond closely to data types in python, and others are more complex. 
  8   
  9  Every `__init__` method class, which is in every class but XsdDataType, has `val` as its only 
 10  parameter. `val` is the value assigned that is being checked. For example, in the assignment 'Integer(5)', 
 11  'Integer' is the class and '5' the value being checked. 
 12   
 13  This module contains the following classes: 
 14   
 15  - *XsdDataType* 
 16  - *Integer* 
 17  - *PositiveInteger* 
 18  - *NonNegativeInteger* 
 19  - *NegativeInteger* 
 20  - *NonPositiveInteger* 
 21  - *Double* 
 22  - *TypeList* 
 23  - *Boolean* 
 24  - *String* 
 25  - *ID* 
 26  - *IDREF* 
 27  - *Base64Binary* 
 28   
 29  """ 
 30  #============================================================ 
 31  # 
32 -class XsdDataType (object):
33 """ 34 An empty class. Acts as a common base class for all of the other primitive data classes, so 35 it is easier to pick out the classes from the module. Has *object* as a base class. 36 """ 37 pass
38 39 #============================================================ 40 #
41 -class Integer(int, XsdDataType):
42 """ 43 Identical to the integer type in python. Has *int* and *XsdDataType* as base classes. 44 """ 45 46 name = 'Integer' 47
48 - def __init__(self, val):
49 50 int.__init__(self, val)
51 52 #============================================================ 53 #
54 -class PositiveInteger(Integer):
55 """ 56 Integers with values greater than zero. Has *Integer* as a base class. 57 """ 58 name = 'PositiveInteger' 59
60 - def __init__(self, val):
61 62 if val <= 0: 63 64 raise TypeError, "Not a positive Integer" 65 66 Integer.__init__(self, val)
67 68 #============================================================ 69 #
70 -class NonNegativeInteger(Integer):
71 """ 72 Integers with values greater than or equal to zero. Has *Integer* as a base class. 73 """ 74 name = 'NonNegativeInteger' 75
76 - def __init__(self, val):
77 78 if val < 0: 79 80 raise TypeError, "Not a non-negative Integer" 81 82 Integer.__init__(self, val)
83 84 #============================================================ 85 #
86 -class NegativeInteger(Integer):
87 """ 88 Integers with values less than zero. Has *Integer* as a base class. 89 """ 90 name = 'NegativeInteger' 91
92 - def __init__(self, val):
93 94 if val >= 0: 95 96 raise TypeError, "Not a negative Integer" 97 98 Integer.__init__(self, val)
99 100 101 #============================================================ 102 #
103 -class NonPositiveInteger(Integer):
104 """ 105 Integers with values less than or equal to zero. Has *Integer* as a base class. 106 """ 107 name = 'NonPostiveInteger' 108
109 - def __init__(self, val):
110 111 if val > 0: 112 113 raise TypeError, "Not a non-postive Integer" 114 115 Integer.__init__(self, val)
116 117 #============================================================ 118 #
119 -class Double(float, XsdDataType):
120 """ 121 Identical to the float type in python. Has *float* and *XsdDataType* as base classes. 122 """ 123 name = 'Float' #Should this be 'Double' instead? 124
125 - def __init__(self, val):
126 127 float.__init__(self,val)
128 129 #============================================================ 130 #
131 -class TypeList(list, XsdDataType):
132 """ 133 Identical to the list type in python. Has *list* and *XsdDataType* as base classes. 134 """ 135 name = "List" 136
137 - def __init__(self, val):
138 139 list.__init__(self, val)
140 #============================================================ 141 #
142 -class Boolean(Integer):
143 """ 144 Class for Boolean. The boolean type in python is not quite like other types in the language. 145 The Boolean type class in python cannot be used as a base class or have its `__init__` function 146 used as it is with the other classes in the module. In python, the boolean type is really an integer 147 that can either be 0 or 1. Since booleans are normally expressed as 'True' or 'False' in python, the 148 programmer must be sure that True is entered as 1 and False as zero when using this class. The class 149 has defined `__str__` and `__repr__` methods, so that the data is presented in a way that makes more 150 sense in python or xml, depending on the use. `__str__` is for python and `__repr__` for python. Make 151 sure to use the appropiate function when using booleans. Class has *Integer* as a base class. 152 """ 153 name = 'Boolean' 154
155 - def __init__(self, val):
156 157 self.val = val 158 159 if val < 0 or val > 1: 160 161 raise TypeError, "Invalid Boolean Value %i" % val 162 163 Integer.__init__(self, val)
164
165 - def __str__(self):
166 """ 167 Returns 'true' or 'false', depending on the value of 'val', when the str() function is used. Use 168 for xml and xsd files. 169 """ 170 if self.val == 1: 171 return "true" 172 if self.val == 0: 173 return "false"
174
175 - def __repr__(self):
176 """ 177 Returns True or False, depending on the value of 'val', when the repr() function is used. Use 178 for python. 179 """ 180 if val == 1: 181 return True 182 if val == 0: 183 return False
184 185 #============================================================ 186 #
187 -class String(str, XsdDataType):
188 """ 189 Identical to the string type in python. Has *str* and *XsdDataType* as base classes. 190 """ 191 192 name = 'String' 193
194 - def __init__(self, val):
195 196 str.__init__(self, val)
197 198 199 #============================================================ 200 #
201 -class ID(String):
202 """ 203 Used for ID attributes in xml and xsd files. Uses *String* as a base class, and makes no changes to it. 204 """ 205 name = 'ID' 206
207 - def __init__(self, val):
208 209 String.__init__(self, val)
210 211 #============================================================ 212 #
213 -class IDREF(String):
214 """ 215 Used for IDREF attributes in xml and xsd files. Uses *String* as a base class, and makes no changes to it. 216 """ 217 name = 'IDREF' 218
219 - def __init__(self, val):
220 221 String.__init__(self, val)
222 223 #============================================================ 224 #
225 -class Base64Binary(String):
226 """ 227 Used with data encoded into base64. Treats the data as a string, and has *String* as a base class. 228 Tries to see if the the binary is valid by decoding and then reencoding it with the base64 library 229 included with python. This process may not detect errors every time. It should only be able to see 230 if the base64 binary is well-formed when working correctly. 231 """ 232 name = 'Base64Binary' 233
234 - def __init__(self, val):
235 try: 236 valTestDe = base64.decodestring(val) 237 valTestEn = base64.encodestring(valTestDe) 238 239 except: 240 raise TypeError, "Not a valid Base64 Binary" 241 242 243 String.__init__(self, val)
244