Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Data store specific stuff from Database manager #2990

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading