|
a/Ming/ming/schema.py |
|
b/Ming/ming/schema.py |
|
|
1 |
import types
|
1 |
import logging
|
2 |
import logging
|
2 |
|
3 |
|
3 |
from copy import deepcopy
|
4 |
from copy import deepcopy
|
4 |
from datetime import datetime
|
5 |
from datetime import datetime
|
5 |
from formencode.validators import Invalid
|
6 |
from formencode.validators import Invalid
|
|
... |
|
... |
31 |
def validate(self, d):
|
32 |
def validate(self, d):
|
32 |
'convert/validate an object or raise an Invalid exception'
|
33 |
'convert/validate an object or raise an Invalid exception'
|
33 |
raise NotImplemented, 'validate'
|
34 |
raise NotImplemented, 'validate'
|
34 |
|
35 |
|
35 |
@classmethod
|
36 |
@classmethod
|
36 |
def make(cls, field):
|
37 |
def make(cls, field, *args, **kwargs):
|
37 |
'''Build a SchemaItem from a "shorthand" schema (summarized below)
|
38 |
'''Build a SchemaItem from a "shorthand" schema (summarized below)
|
38 |
|
39 |
|
39 |
int - int or long
|
40 |
int - int or long
|
40 |
str - string or unicode
|
41 |
str - string or unicode
|
41 |
float - float, int, or long
|
42 |
float - float, int, or long
|
|
... |
|
... |
59 |
elif field is None:
|
60 |
elif field is None:
|
60 |
field = Anything()
|
61 |
field = Anything()
|
61 |
elif field in SHORTHAND:
|
62 |
elif field in SHORTHAND:
|
62 |
field = SHORTHAND[field]
|
63 |
field = SHORTHAND[field]
|
63 |
if isinstance(field, type):
|
64 |
if isinstance(field, type):
|
64 |
field = field()
|
65 |
field = field(*args, **kwargs)
|
65 |
return field
|
66 |
return field
|
66 |
|
67 |
|
67 |
class Migrate(SchemaItem):
|
68 |
class Migrate(SchemaItem):
|
68 |
'''Use when migrating from one field type to another
|
69 |
'''Use when migrating from one field type to another
|
69 |
'''
|
70 |
'''
|
|
... |
|
... |
130 |
if self.required:
|
131 |
if self.required:
|
131 |
raise Invalid('Missing field', value, None)
|
132 |
raise Invalid('Missing field', value, None)
|
132 |
else:
|
133 |
else:
|
133 |
if self.if_missing is Missing:
|
134 |
if self.if_missing is Missing:
|
134 |
return self.if_missing
|
135 |
return self.if_missing
|
|
|
136 |
elif isinstance(self.if_missing, (types.FunctionType, types.MethodType)):
|
|
|
137 |
return self.if_missing()
|
135 |
else:
|
138 |
else:
|
136 |
return deepcopy(self.if_missing) # handle mutable defaults
|
139 |
return deepcopy(self.if_missing) # handle mutable defaults
|
137 |
elif value == self.if_missing:
|
140 |
elif value == self.if_missing:
|
138 |
return value
|
141 |
return value
|
139 |
return self._validate(value, **kw)
|
142 |
return self._validate(value, **kw)
|