Browse Source

Bugfixes in lefactory and letype

Deleted the classinstancemethod decorator because of inheritance problems (not sure they were real...)
Add a shortcut method to LeFactory to get the generated LeObject class
+ bugfixes
Yann Weber 9 years ago
parent
commit
052ddd4ce8
3 changed files with 20 additions and 88 deletions
  1. 0
    44
      Lodel/utils/classinstancemethod.py
  2. 5
    1
      leobject/lefactory.py
  3. 15
    43
      leobject/letype.py

+ 0
- 44
Lodel/utils/classinstancemethod.py View File

@@ -1,44 +0,0 @@
1
-#-*- coding: utf-8 -*-
2
-
3
-## @brief Decorator to have methods with the same name callable from Class or from Instance
4
-#
5
-# Usage example :
6
-# @code{.py}
7
-#   class A(object):
8
-#       @classinstancemethod
9
-#       def foo(self, a, b):
10
-#           print("foo() instance call", a ,b)
11
-#       
12
-#       @foo.classmethod
13
-#       def foo(self):
14
-#           print("foo() class call")
15
-#
16
-#       @classinstancemethod
17
-#       def bar(obj):
18
-#           if obj == A:
19
-#               print("bar() class call")
20
-#           else:
21
-#               print("bar() instance call")
22
-#
23
-# a=A()
24
-# A.foo() #foo() class call
25
-# a.foo(1,2) #foo() instance call 1 2
26
-# A.bar() #bar() class call
27
-# a.bar() #bar() instance call
28
-# @endcode
29
-class classinstancemethod(object):
30
-    def __init__(self, func):
31
-        self._func = func
32
-        self._classmethod = None
33
-
34
-    def classmethod(self, func):
35
-        self._classmethod = func
36
-
37
-    def __get__(self, instance, cls):
38
-        if instance is None:
39
-            if self._classmethod is None:
40
-                return self._func.__get__(cls, cls)
41
-            else:
42
-                return self._classmethod.__get__(instance, cls)
43
-        else:
44
-            return self._func.__get__(instance, cls)

+ 5
- 1
leobject/lefactory.py View File

@@ -31,6 +31,10 @@ class LeFactory(object):
31 31
         except AttributeError:
32 32
             return False
33 33
         return res
34
+    
35
+    @classmethod
36
+    def leobject(cls):
37
+        return cls.leobj_from_name('LeObject')
34 38
 
35 39
     ## @brief Convert an EmType or EmClass name in a python class name
36 40
     # @param name str : The name
@@ -174,7 +178,7 @@ class {name}(LeObject,LeClass):
174 178
            result += """
175 179
 ## @brief EmType {name} LeType child class
176 180
 # @see leobject::letype::LeType
177
-class {name}({leclass},LeType):
181
+class {name}(LeType, {leclass}):
178 182
     _type_id = {uid}
179 183
 """.format(
180 184
     name = LeFactory.name2classname(emtype.name),

+ 15
- 43
leobject/letype.py View File

@@ -13,11 +13,10 @@
13 13
 import leobject
14 14
 from leobject.leclass import LeClass
15 15
 from leobject.leobject import LeObjectError
16
-from Lodel.utils.classinstancemethod import classinstancemethod
17 16
 
18 17
 ## @brief Represent an EmType data instance
19 18
 # @note Is not a derivated class of LeClass because the concrete class will be a derivated class from LeClass
20
-class LeType(leobject.leobject._LeObject):
19
+class LeType(object):
21 20
     
22 21
     ## @brief Stores selected fields with key = name
23 22
     _fields = list()
@@ -64,24 +63,20 @@ class LeType(leobject.leobject._LeObject):
64 63
     ## @brief Get a fieldname:value dict
65 64
     # @return A dict with field name as key and the field value as value
66 65
     @property
67
-    def datas(self, full=False):
66
+    def datas(self):
68 67
         return { fname: getattr(self, fname) for fname in self._fields if hasattr(self,fname) }
69 68
     
70 69
     ## @brief Get all the datas for this LeType
71 70
     # @return a dict with fieldname as key and field value as value
72 71
     # @warning Can represent a performance issue
73
-    @property
74 72
     def all_datas(self):
75 73
         self.populate()
76 74
         return self.datas
77
-        
78
-            
79
-
75
+    
80 76
     ## @brief Delete the LeType from Db
81 77
     # @note equivalent to LeType::delete(filters=['lodel_id = %s'%self.lodel_id)
82
-    @classinstancemethod
83
-    def delete(self):
84
-        return self.__class__.delete([('lodel_id','=',self.lodel_id)])
78
+    def db_delete(self):
79
+        return self.delete([('lodel_id','=',repr(self.lodel_id))])
85 80
     
86 81
     ## @brief Delete a LeType from the datasource
87 82
     # @param filters list : list of filters (see @ref leobject_filters)
@@ -89,27 +84,23 @@ class LeType(leobject.leobject._LeObject):
89 84
     # @return True if deleted False if not existing
90 85
     # @throw InvalidArgumentError if invalid parameters
91 86
     # @throw Leo exception if the lodel_id identify an object from another type
92
-    @delete.classmethod
87
+    @classmethod
93 88
     def delete(cls, filters):
94
-        return super(LeType, cls).delete(letype, leclass, filters)
89
+        return leobject.lefactory.LeFactory.leobject().delete(cls, filters)
95 90
         
96
-
97
-    @classinstancemethod
98 91
     ## @brief Update a LeType in db
99
-    def update(self):
100
-        return self.__class__.update(filters=[('lodel_id', '=', self.lodel_id)], datas = self.datas)
92
+    def db_update(self):
93
+        return self.update(filters=[('lodel_id', '=', repr(self.lodel_id))], datas = self.datas)
101 94
         
102
-
103
-    @update.classmethod
95
+    @classmethod
104 96
     ## @brief Update some LeType in db
105 97
     # @param datas : keys are lodel_id and value are dict of fieldname => value
106 98
     # @param filters list : list of filters (see @ref leobject_filters)
107 99
     # @param cls
108 100
     # return bool
109 101
     def update(cls, filters, datas):
110
-        return super(LeType, cls).update(letype = cls, leclass = cls._leclass, filters = filters, datas = datas)
102
+        return leobject.lefactory.LeFactory.leobject().update(letype = cls, filters = filters, datas = datas)
111 103
         
112
-
113 104
     ## @brief Insert a new LeType in the datasource
114 105
     # @param **datas list : A list of dict containing the datas
115 106
     # @return The lodel id of the new LeType or False
@@ -122,14 +113,15 @@ class LeType(leobject.leobject._LeObject):
122 113
     ## @brief Check that datas are valid for this type
123 114
     # @param datas dict : key == field name value are field values
124 115
     # @param complete bool : if True expect that datas provide values for all non internal fields
116
+    # @param allow_internal bool : if True don't raise an error if a field is internal
125 117
     # @throw TypeError If the datas are not valids
126 118
     # @throw AttributeError if datas provides values for automatic fields
127 119
     # @throw AttributeError if datas provides values for fields that doesn't exists
128 120
     @classmethod
129
-    def check_datas_or_raise(cls, datas, complete = False):
121
+    def check_datas_or_raise(cls, datas, complete = False, allow_internal = True):
130 122
         autom_fields = [f for f, ft in cls._fieldtypes.items() if hasattr(ft,'internal') and ft.internal]
131 123
         for dname, dval in datas.items():
132
-            if dname in autom_fields:
124
+            if not allow_internal and dname in autom_fields:
133 125
                 raise AttributeError("The field '%s' is internal"%(dname))
134 126
             if dname not in cls._fields:
135 127
                 raise AttributeError("No such field '%s' for %s"%(dname, self.__class__.__name__))
@@ -138,6 +130,7 @@ class LeType(leobject.leobject._LeObject):
138 130
         fields = [f for f, ft in cls._fieldtypes.items() if not hasattr(ft,'internal') or not ft.internal]
139 131
         if complete and len(datas) < len(fields):
140 132
             raise LeObjectError("The argument complete was True but some fields are missing : %s"%(set(fields) - set(datas.keys())))
133
+        
141 134
     
142 135
     ## @brief Check that datas are valid for this type
143 136
     # @param datas dict : key == field name value are field values
@@ -160,24 +153,3 @@ class LeType(leobject.leobject._LeObject):
160 153
             self._fieldtypes[name].check_or_raise(value)
161 154
         return super(LeType, self).__setattr__(name, value)
162 155
     
163
-    ## @brief Fetch superiors
164
-    # @param nature str : The relation nature
165
-    # @return if no nature given return a dict with nature as key and arrays of LeObject as value. Else return an array of LeObject
166
-    def superiors(self, nature = None):
167
-        pass
168
-
169
-    ## @brief Fetch subordinates
170
-    # @param nature str : The relation nature
171
-    # @return if no nature given return a dict with nature as key and arrays of LeObject as value. Else return an array of LeObject
172
-    def subordinates(self, nature = None):
173
-        pass
174
-
175
-    ## @brief Add a superior
176
-    # @param nature str : The raltion nature
177
-    # @param leo LeObject : The superior
178
-    # @return True if done False if already done
179
-    # @throw A Leo exception if trying to link with an invalid leo
180
-    # @throw InvalidArgumentError if invalid argument
181
-    def set_superior(self, nature, leo):
182
-        pass
183
-    

Loading…
Cancel
Save