|
@@ -3,17 +3,18 @@
|
3
|
3
|
import types
|
4
|
4
|
import importlib
|
5
|
5
|
|
|
6
|
+
|
6
|
7
|
## @brief Abstract class representing a fieldtype
|
7
|
8
|
class GenericFieldType(object):
|
8
|
|
-
|
|
9
|
+
|
9
|
10
|
## @brief Text describing the fieldtype
|
10
|
11
|
help = 'Generic field type : abstract class for every fieldtype'
|
11
|
12
|
## @brief Allowed type for handled datas
|
12
|
13
|
_allowed_ftype = ['char', 'str', 'int', 'bool', 'datetime', 'text', 'rel2type']
|
13
|
|
-
|
|
14
|
+
|
14
|
15
|
## @brief The basic lowlevel value type
|
15
|
16
|
ftype = None
|
16
|
|
-
|
|
17
|
+
|
17
|
18
|
## @brief Instanciate a new fieldtype
|
18
|
19
|
# @param ftype str : The type of datas handled by this fieldtype
|
19
|
20
|
# @param nullable bool : is None allowed as value ?
|
|
@@ -24,23 +25,23 @@ class GenericFieldType(object):
|
24
|
25
|
# @throw NotImplementedError if called directly
|
25
|
26
|
# @throw AttributeError if bad ftype
|
26
|
27
|
# @throw AttributeError if bad check_function
|
27
|
|
- def __init__(self, ftype, nullable = True, check_function = None, uniq = False, primary=False, **kwargs):
|
|
28
|
+ def __init__(self, ftype, nullable=True, check_function=None, uniq=False, primary=False, **kwargs):
|
28
|
29
|
if self.__class__ == GenericFieldType:
|
29
|
30
|
raise NotImplementedError("Abstract class")
|
30
|
|
-
|
|
31
|
+
|
31
|
32
|
if self.ftype is None:
|
32
|
|
- raise RuntimeError("The class attribute ftype is not properly set by the %s EmFieldType"%self.name)
|
|
33
|
+ raise RuntimeError("The class attribute ftype is not properly set by the %s EmFieldType" % self.name)
|
33
|
34
|
|
34
|
35
|
if ftype not in self._allowed_ftype:
|
35
|
|
- raise AttributeError("Ftype '%s' not known"%ftype)
|
36
|
|
-
|
|
36
|
+ raise AttributeError("Ftype '%s' not known" % ftype)
|
|
37
|
+
|
37
|
38
|
if check_function is None:
|
38
|
39
|
check_function = self.dummy_check
|
39
|
40
|
elif not isinstance(check_function, types.FunctionType):
|
40
|
41
|
raise AttributeError("check_function argument has to be a function")
|
41
|
42
|
|
42
|
43
|
if ftype != self.__class__.ftype:
|
43
|
|
- raise RuntimeError("The ftype is not the same for the instance and the class. Maybe %s reimplement ftype at class level but shouldn't"%self.name)
|
|
44
|
+ raise RuntimeError("The ftype is not the same for the instance and the class. Maybe %s reimplement ftype at class level but shouldn't" % self.name)
|
44
|
45
|
|
45
|
46
|
self.ftype = ftype
|
46
|
47
|
self.check_function = check_function
|
|
@@ -50,16 +51,16 @@ class GenericFieldType(object):
|
50
|
51
|
if 'default' in kwargs:
|
51
|
52
|
self.check_or_raise(kwargs['default'])
|
52
|
53
|
self.default = kwargs['default']
|
53
|
|
- del(kwargs['default'])
|
|
54
|
+ del kwargs['default']
|
54
|
55
|
|
55
|
|
- for argname,argvalue in kwargs.items():
|
|
56
|
+ for argname, argvalue in kwargs.items():
|
56
|
57
|
setattr(self, argname, argvalue)
|
57
|
|
-
|
|
58
|
+
|
58
|
59
|
## @return A fieldtype name from an instance
|
59
|
60
|
@property
|
60
|
61
|
def name(self):
|
61
|
62
|
return self.__module__.split('.')[-1]
|
62
|
|
-
|
|
63
|
+
|
63
|
64
|
## @brief Check if a value is correct
|
64
|
65
|
# @param value * : The value
|
65
|
66
|
# @throw TypeError if not valid
|
|
@@ -80,17 +81,15 @@ class GenericFieldType(object):
|
80
|
81
|
# @return a string representing a python module name
|
81
|
82
|
@staticmethod
|
82
|
83
|
def module_name(fieldtype_name):
|
83
|
|
- return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
|
|
84
|
+ return 'EditorialModel.fieldtypes.%s' % (fieldtype_name)
|
84
|
85
|
|
85
|
86
|
## @brief __hash__ implementation for fieldtypes
|
86
|
87
|
def __hash__(self):
|
87
|
|
- hash_dats = [ self.__class__.__module__ ]
|
|
88
|
+ hash_dats = [self.__class__.__module__]
|
88
|
89
|
for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
|
89
|
90
|
hash_dats.append((kdic, getattr(self, kdic)))
|
90
|
91
|
return hash(tuple(hash_dats))
|
91
|
92
|
|
92
|
|
-
|
93
|
|
-
|
94
|
93
|
## @brief Transform a value into a valid python representation according to the fieldtype
|
95
|
94
|
# @param value ? : The value to cast
|
96
|
95
|
# @param kwargs dict : optionnal cast arguments
|
|
@@ -99,9 +98,9 @@ class GenericFieldType(object):
|
99
|
98
|
# @throw TypeError if the cast is not possible
|
100
|
99
|
def cast(self, value, **kwargs):
|
101
|
100
|
if len(kwargs) > 0:
|
102
|
|
- raise AttributeError("No optionnal argument allowed for %s cast method"%self.__class__.__name__)
|
|
101
|
+ raise AttributeError("No optionnal argument allowed for %s cast method" % self.__class__.__name__)
|
103
|
102
|
return value
|
104
|
|
-
|
|
103
|
+
|
105
|
104
|
## @brief Check if a value is correct
|
106
|
105
|
# @param value * : The value to check
|
107
|
106
|
# @return True if valid else False
|
|
@@ -120,6 +119,6 @@ class GenericFieldType(object):
|
120
|
119
|
raise TypeError("Not nullable field")
|
121
|
120
|
self.check_function(value)
|
122
|
121
|
|
|
122
|
+
|
123
|
123
|
class FieldTypeError(Exception):
|
124
|
124
|
pass
|
125
|
|
-
|