From 084b8af6c1db6f8cf0465ffeb550c43473d65ef5 Mon Sep 17 00:00:00 2001 From: Yann Date: Fri, 24 Jul 2015 17:21:50 +0200 Subject: [PATCH] MigrationHandler enhancement (now records Model's hash for each changes). Better beaviour of Model.create_component if MigrationHandler raises --- EditorialModel/components.py | 5 ++++- EditorialModel/migrationhandler/dummy.py | 6 +++++- EditorialModel/model.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/EditorialModel/components.py b/EditorialModel/components.py index 0d4445a..560deac 100644 --- a/EditorialModel/components.py +++ b/EditorialModel/components.py @@ -61,10 +61,13 @@ class EmComponent(object): ## @brief Reimplementation for calling the migration handler to register the change def __setattr__(self, attr_name, value): - if '_inited' in self.__dict__: + inited = '_inited' in self.__dict__ + if inited: # if fails raise MigrationHandlerChangeError self.model.migration_handler.register_change(self.uid, {attr_name: getattr(self, attr_name) }, {attr_name: value} ) super(EmComponent, self).__setattr__(attr_name, value) + if inited: + self.model.migration_handler.register_model_state(hash(self.model)) ## Check the type of attribute named var_name # @param var_name str : the attribute name diff --git a/EditorialModel/migrationhandler/dummy.py b/EditorialModel/migrationhandler/dummy.py index 57fdbf8..60cfd87 100644 --- a/EditorialModel/migrationhandler/dummy.py +++ b/EditorialModel/migrationhandler/dummy.py @@ -36,4 +36,8 @@ class DummyMigrationHandler(object): print(str_chg) print("##############\n") pass - + + def register_model_state(self, state_hash): + if self.debug: + print("New EditorialModel sate registered : '%s'"%state_hash) + diff --git a/EditorialModel/model.py b/EditorialModel/model.py index 271006a..59525da 100644 --- a/EditorialModel/model.py +++ b/EditorialModel/model.py @@ -147,7 +147,16 @@ class Model(object): em_component.init_ended() #register the creation in migration handler - self.migration_handler.register_change(em_component.uid, None, em_component.attr_dump) + try: + self.migration_handler.register_change(em_component.uid, None, em_component.attr_dump) + except MigrationHandlerChangeError as e: + #Revert the creation + self.components(em_component.__class__).remove(em_component) + del self._components['uids'][em_component.uid] + print(self._components) + raise e + + self.migration_handler.register_model_state(hash(self)) return em_component @@ -166,6 +175,8 @@ class Model(object): if em_component.delete_check(): self._components[self.name_from_emclass(em_component.__class__)].remove(em_component) del self._components['uids'][uid] + #Register the new EM state + self.migration_handler.register_model_state(hash(self)) return True ## Changes the current backend