Skip to content

Commit

Permalink
Remove Data store specific stuff from Database manager (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen authored Oct 28, 2024
2 parents be4cfcf + 2349f82 commit b301489
Showing 1 changed file with 8 additions and 44 deletions.
52 changes: 8 additions & 44 deletions spinetoolbox/spine_db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class SpineDBManager(QObject):
str: item type, such as "object_class"
dict: mapping DatabaseMapping to list of updated dict-items.
"""
database_clean_changed = Signal(object, bool)
"""Emitted whenever database becomes clean or dirty.
Args:
object: database mapping
bool: True if database has become clean, False if it became dirty
"""

def __init__(self, settings, parent, synchronous=False):
"""
Expand All @@ -116,7 +123,6 @@ def __init__(self, settings, parent, synchronous=False):
self._connect_signals()
self._cmd_id = 0
self._synchronous = synchronous
self.data_stores = {}
self._validated_values = {"parameter_definition": {}, "parameter_value": {}}
self._parameter_type_validator = ParameterTypeValidator(self)
self._parameter_type_validator.validated.connect(self._parameter_value_validated)
Expand Down Expand Up @@ -406,6 +412,7 @@ def _do_get_db_map(self, url, **kwargs):
self._validated_values["parameter_definition"][id(db_map)] = {}
self._validated_values["parameter_value"][id(db_map)] = {}
stack = self.undo_stack[db_map] = AgedUndoStack(self)
stack.cleanChanged.connect(lambda clean: self.database_clean_changed.emit(db_map, clean))
self.undo_action[db_map] = stack.createUndoAction(self)
self.redo_action[db_map] = stack.createRedoAction(self)
return db_map
Expand Down Expand Up @@ -435,19 +442,13 @@ def register_listener(self, listener, *db_maps):
listener (object)
*db_maps
"""
self.update_data_store_db_maps()
for db_map in db_maps:
self.add_db_map_listener(db_map, listener)
stack = self.undo_stack[db_map]
try:
stack.canRedoChanged.connect(listener.update_undo_redo_actions)
stack.canUndoChanged.connect(listener.update_undo_redo_actions)
stack.cleanChanged.connect(listener.update_commit_enabled)
stores = self.data_stores.get(db_map)
if not stores:
continue
for store in stores:
self.undo_stack[db_map].cleanChanged.connect(store.notify_about_dirtiness)
except AttributeError:
pass

Expand Down Expand Up @@ -476,12 +477,6 @@ def unregister_listener(self, listener, *db_maps, dirty_db_maps=None, commit_dir
# for that db map is closed. This way the dirtiness state of a data store that is
# not open in a db editor can still be affected.
continue
stores = self.data_stores.get(db_map)
if not stores:
continue
for store in stores:
self.undo_stack[db_map].cleanChanged.disconnect(store.notify_about_dirtiness)
store.notify_about_dirtiness(True)
except AttributeError:
pass
if dirty_db_maps:
Expand All @@ -496,44 +491,13 @@ def unregister_listener(self, listener, *db_maps, dirty_db_maps=None, commit_dir
self.undo_stack[db_map].canRedoChanged.connect(listener.update_undo_redo_actions)
self.undo_stack[db_map].canUndoChanged.connect(listener.update_undo_redo_actions)
self.undo_stack[db_map].cleanChanged.connect(listener.update_commit_enabled)
stores = self.data_stores.get(db_map)
if not stores:
continue
for store in stores:
self.undo_stack[db_map].cleanChanged.connect(store.notify_about_dirtiness)
except AttributeError:
pass
for db_map in db_maps:
if not self.db_map_listeners(db_map):
self.close_session(db_map.db_url)
return failed_db_maps

def add_data_store_db_map(self, db_map, store):
"""Adds a Data Store instance under a db_map into memory"""
if db_map in self.data_stores:
self.data_stores[db_map].append(store)
else:
self.data_stores[db_map] = [store]

def remove_data_store_db_map(self, db_map, store):
"""Removes a Data Store instance from memory"""
if self.data_stores.get(db_map):
self.data_stores[db_map].remove(store)

def update_data_store_db_maps(self):
"""Updates the db maps of the dict that maps db_maps to Data Stores"""
new_data_stores = {}
old_stores = []
for store in self.data_stores.values():
old_stores.extend(store)
for store in old_stores:
db_map = store.get_db_map_for_ds()
if db_map in new_data_stores:
new_data_stores[db_map].append(store)
else:
new_data_stores[db_map] = [store]
self.data_stores = new_data_stores

def is_dirty(self, db_map):
"""Returns True if mapping has pending changes.
Expand Down

0 comments on commit b301489

Please sign in to comment.