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
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
42 """
43 Identical to the integer type in python. Has *int* and *XsdDataType* as base classes.
44 """
45
46 name = 'Integer'
47
51
52
53
55 """
56 Integers with values greater than zero. Has *Integer* as a base class.
57 """
58 name = 'PositiveInteger'
59
61
62 if val <= 0:
63
64 raise TypeError, "Not a positive Integer"
65
66 Integer.__init__(self, val)
67
68
69
71 """
72 Integers with values greater than or equal to zero. Has *Integer* as a base class.
73 """
74 name = 'NonNegativeInteger'
75
77
78 if val < 0:
79
80 raise TypeError, "Not a non-negative Integer"
81
82 Integer.__init__(self, val)
83
84
85
87 """
88 Integers with values less than zero. Has *Integer* as a base class.
89 """
90 name = 'NegativeInteger'
91
93
94 if val >= 0:
95
96 raise TypeError, "Not a negative Integer"
97
98 Integer.__init__(self, val)
99
100
101
102
104 """
105 Integers with values less than or equal to zero. Has *Integer* as a base class.
106 """
107 name = 'NonPostiveInteger'
108
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'
124
128
129
130
132 """
133 Identical to the list type in python. Has *list* and *XsdDataType* as base classes.
134 """
135 name = "List"
136
140
141
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
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
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
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
197
198
199
200
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
210
211
212
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
222
223
224
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
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