|
@@ -1,6 +1,8 @@
|
1
|
1
|
#-*- coding: utf-8 -*-
|
2
|
2
|
|
|
3
|
+from Lodel.utils.classinstancemethod import classinstancemethod
|
3
|
4
|
from leobject.leclass import LeClass
|
|
5
|
+from leobject.leobject import LeObjectError
|
4
|
6
|
|
5
|
7
|
## @brief Represent an EmType data instance
|
6
|
8
|
# @note Is not a derivated class of LeClass because the concrete class will be a derivated class from LeClass
|
|
@@ -25,6 +27,14 @@ class LeType(object):
|
25
|
27
|
raise AttributeError("No such field '%s' for %s"%(name, self.__class__.__name__)
|
26
|
28
|
setattr(self, name, value)
|
27
|
29
|
|
|
30
|
+ @classinstancemethod
|
|
31
|
+ def delete(self):
|
|
32
|
+ pass
|
|
33
|
+
|
|
34
|
+ @delete.classmethod
|
|
35
|
+ def delete(cls, uid):
|
|
36
|
+ pass
|
|
37
|
+
|
28
|
38
|
## @brief Insert a new LeType in the datasource
|
29
|
39
|
# @param **datas : A dict containing the datas
|
30
|
40
|
# @return The lodel id of the new LeType or False
|
|
@@ -32,13 +42,6 @@ class LeType(object):
|
32
|
42
|
# @throw InvalidArgumentError if invalid argument
|
33
|
43
|
@classmethod
|
34
|
44
|
def insert(cls, **datas):
|
35
|
|
- #check datas
|
36
|
|
- autom_fields = ['lodel_id', 'typename', 'classname' ]
|
37
|
|
- for forbiden in autom_fields:
|
38
|
|
- if forbiden in datas:
|
39
|
|
- raise AttributeError("Not allowed to give explicitly '%s' to insert method"%(forbiden))
|
40
|
|
- self.check_datas(datas)
|
41
|
|
-
|
42
|
45
|
super(LeType, self).insert(typename=self.__class__.__name__, classname=self._leclass.__name__, **datas)
|
43
|
46
|
pass
|
44
|
47
|
|
|
@@ -63,14 +66,34 @@ class LeType(object):
|
63
|
66
|
|
64
|
67
|
## @brief Check that datas are valid for this type
|
65
|
68
|
# @param datas dict : key == field name value are field values
|
66
|
|
- # @throw If the datas are not valids
|
|
69
|
+ # @param complete bool : if True expect that datas provide values for all non internal fields
|
|
70
|
+ # @throw TypeError If the datas are not valids
|
|
71
|
+ # @throw AttributeError if datas provides values for automatic fields
|
|
72
|
+ # @throw AttributeError if datas provides values for fields that doesn't exists
|
67
|
73
|
@classmethod
|
68
|
|
- def check_datas(cls, datas):
|
|
74
|
+ def check_datas_or_raise(cls, datas, complete = False):
|
|
75
|
+ autom_fields = [f.name for f in cls._fieldtypes if f.internal]
|
69
|
76
|
for dname, dval in datas.items():
|
|
77
|
+ if dname in autom_fields:
|
|
78
|
+ raise AttributeError("The field '%s' is internal"%(dname)
|
70
|
79
|
if dname not in cls._fields:
|
71
|
80
|
raise AttributeError("No such field '%s' for %s"%(dname, self.__class__.__name__)
|
72
|
|
- cls._fields[dname].check_or_raise(dval)
|
73
|
|
-
|
|
81
|
+ cls._fieldtypess[dname].check_or_raise(dval)
|
|
82
|
+
|
|
83
|
+ fields = [f.name for f in cls._fieldtypes if not f.internal]
|
|
84
|
+ if complete and len(datas) < len(fields):
|
|
85
|
+ raise LeObjectError("The argument complete was True but some fields are missing : %s"%(set(fields) - set(datas.keys())))
|
|
86
|
+
|
|
87
|
+ ## @brief Check that datas are valid for this type
|
|
88
|
+ # @param datas dict : key == field name value are field values
|
|
89
|
+ # @param complete bool : if True expect that datas provide values for all non internal fields
|
|
90
|
+ # @return True if check pass else False
|
|
91
|
+ def check_datas(cls, datas, complete = False):
|
|
92
|
+ try:
|
|
93
|
+ cls.check_datas_or_raise(datas, complete)
|
|
94
|
+ except (TypeError, AttributeError, LeObjectError):
|
|
95
|
+ return False
|
|
96
|
+ return True
|
74
|
97
|
|
75
|
98
|
## @brief Implements the automatic checks of attributes
|
76
|
99
|
# @note Run data check from fieldtypes if we try to modify an field attribute of the LeType
|
|
@@ -80,7 +103,7 @@ class LeType(object):
|
80
|
103
|
if name in self._fields.keys():
|
81
|
104
|
self._fields[name].check_or_raise(value)
|
82
|
105
|
return super(LeType, self).__setattr__(name, value)
|
83
|
|
-
|
|
106
|
+
|
84
|
107
|
## @brief Delete the LeType
|
85
|
108
|
# @return True if deleted False if not
|
86
|
109
|
def delete(self):
|