Browse Source

MigrationHandler enhancement (now records Model's hash for each changes). Better beaviour of Model.create_component if MigrationHandler raises

Yann Weber 9 years ago
parent
commit
084b8af6c1
3 changed files with 21 additions and 3 deletions
  1. 4
    1
      EditorialModel/components.py
  2. 5
    1
      EditorialModel/migrationhandler/dummy.py
  3. 12
    1
      EditorialModel/model.py

+ 4
- 1
EditorialModel/components.py View File

@@ -61,10 +61,13 @@ class EmComponent(object):
61 61
 
62 62
     ## @brief Reimplementation for calling the migration handler to register the change
63 63
     def __setattr__(self, attr_name, value):
64
-        if '_inited' in self.__dict__:
64
+        inited = '_inited' in self.__dict__
65
+        if inited:
65 66
             # if fails raise MigrationHandlerChangeError
66 67
             self.model.migration_handler.register_change(self.uid, {attr_name: getattr(self, attr_name) }, {attr_name: value} )
67 68
         super(EmComponent, self).__setattr__(attr_name, value)
69
+        if inited:
70
+            self.model.migration_handler.register_model_state(hash(self.model))
68 71
 
69 72
     ## Check the type of attribute named var_name
70 73
     # @param var_name str : the attribute name

+ 5
- 1
EditorialModel/migrationhandler/dummy.py View File

@@ -36,4 +36,8 @@ class DummyMigrationHandler(object):
36 36
                     print(str_chg)
37 37
             print("##############\n")
38 38
         pass
39
-        
39
+    
40
+    def register_model_state(self, state_hash):
41
+        if self.debug:
42
+            print("New EditorialModel sate registered : '%s'"%state_hash)
43
+

+ 12
- 1
EditorialModel/model.py View File

@@ -147,7 +147,16 @@ class Model(object):
147 147
         em_component.init_ended()
148 148
 
149 149
         #register the creation in migration handler
150
-        self.migration_handler.register_change(em_component.uid, None, em_component.attr_dump)
150
+        try:
151
+            self.migration_handler.register_change(em_component.uid, None, em_component.attr_dump)
152
+        except MigrationHandlerChangeError as e:
153
+            #Revert the creation
154
+            self.components(em_component.__class__).remove(em_component)
155
+            del self._components['uids'][em_component.uid]
156
+            print(self._components)
157
+            raise e
158
+
159
+        self.migration_handler.register_model_state(hash(self))
151 160
 
152 161
         return em_component
153 162
 
@@ -166,6 +175,8 @@ class Model(object):
166 175
         if em_component.delete_check():
167 176
             self._components[self.name_from_emclass(em_component.__class__)].remove(em_component)
168 177
             del self._components['uids'][uid]
178
+        #Register the new EM state
179
+        self.migration_handler.register_model_state(hash(self))
169 180
         return True
170 181
 
171 182
     ## Changes the current backend

Loading…
Cancel
Save