spinetoolbox.spine_db_manager

The SpineDBManager class

authors:
  1. Vennström (VTT) and M. Marin (KTH)
date:

2.10.2019

Module Contents

Classes

SpineDBManager Class to manage DBs within a project.

Functions

do_create_new_spine_database(url) Creates a new spine database at the given url.
spinetoolbox.spine_db_manager.do_create_new_spine_database(url)[source]

Creates a new spine database at the given url.

class spinetoolbox.spine_db_manager.SpineDBManager(settings, logger, project)[source]

Bases: PySide2.QtCore.QObject

Class to manage DBs within a project.

TODO: Expand description, how it works, the cache, the signals, etc.

Initializes the instance.

Parameters:
  • settings (QSettings) – Toolbox settings
  • logger (LoggingInterface) – a general, non-database-specific logger
  • project (SpineToolboxProject) –
database_created[source]
session_refreshed[source]
session_committed[source]
session_rolled_back[source]
scenarios_added[source]
alternatives_added[source]
object_classes_added[source]
objects_added[source]
relationship_classes_added[source]
relationships_added[source]
entity_groups_added[source]
parameter_definitions_added[source]
parameter_values_added[source]
parameter_value_lists_added[source]
parameter_tags_added[source]
scenarios_removed[source]
alternatives_removed[source]
object_classes_removed[source]
objects_removed[source]
relationship_classes_removed[source]
relationships_removed[source]
entity_groups_removed[source]
parameter_definitions_removed[source]
parameter_values_removed[source]
parameter_value_lists_removed[source]
parameter_tags_removed[source]
scenarios_updated[source]
alternatives_updated[source]
object_classes_updated[source]
objects_updated[source]
relationship_classes_updated[source]
relationships_updated[source]
parameter_definitions_updated[source]
parameter_values_updated[source]
parameter_value_lists_updated[source]
parameter_tags_updated[source]
parameter_definition_tags_set[source]
items_removed_from_cache[source]
_scenario_alternatives_added[source]
_scenario_alternatives_updated[source]
_scenario_alternatives_removed[source]
_parameter_definition_tags_added[source]
_parameter_definition_tags_removed[source]
_GROUP_SEP = ǀ[source]
db_maps[source]
db_editors[source]
create_new_spine_database(self, url)[source]
close_session(self, url)[source]

Pops any db map on the given url and closes its connection.

Parameters:url (str) –
close_all_sessions(self)[source]

Closes connections to all database mappings.

show_data_store_form(self, db_url_codenames, logger)[source]

Creates a new SpineDBEditor and shows it.

Parameters:
  • db_url_codenames (dict) – Mapping db urls to codenames.
  • logger (LoggingInterface) – Where to log SpineDBAPIError
get_db_map(self, url, logger, upgrade=False, codename=None)[source]

Returns a DiffDatabaseMapping instance from url if possible, None otherwise. If needed, asks the user to upgrade to the latest db version.

Parameters:
  • url (str, URL) –
  • logger (LoggingInterface) – Where to log SpineDBAPIError
  • upgrade (bool, optional) –
  • codename (str, NoneType, optional) –
Returns:

DiffDatabaseMapping, NoneType

_do_get_db_map(self, url, upgrade, codename)[source]

Returns a memorized DiffDatabaseMapping instance from url. Called by get_db_map.

Parameters:
  • url (str, URL) –
  • upgrade (bool, optional) –
  • codename (str, NoneType, optional) –
Returns:

DiffDatabaseMapping

register_listener(self, ds_form, *db_maps)[source]

Register given ds_form as listener for all given db_map’s signals.

Parameters:
unregister_listener(self, ds_form, db_map)[source]

Unregisters given ds_form from given db_map signals.

Parameters:
set_logger_for_db_map(self, logger, db_map)[source]
unset_logger_for_db_map(self, db_map)[source]
fetch_db_maps_for_listener(self, listener, *db_maps)[source]

Fetches given db_map for given listener.

Parameters:
  • listener (SpineDBEditor) –
  • *db_maps – database maps to fetch
_clean_up_fetcher(self, fetcher)[source]

Cleans up things after fetcher has finished working.

Parameters:fetcher (SpineDBFetcher) – the fetcher to clean up
_stop_fetchers(self)[source]

Quits all fetchers and deletes them.

refresh_session(self, *db_maps)[source]
commit_session(self, *db_maps, rollback_if_no_msg=False, cookie=None)[source]

Commits the current session.

Parameters:
  • *db_maps – database maps to commit
  • rollback_if_no_msg (bool) – if True, the commit will be rolled back if no commit message is provided
  • cookie (object, optional) – a free form identifier which will be forwarded to session_committed signal
static _get_commit_msg(db_map)[source]
rollback_session(self, *db_maps)[source]
static _get_rollback_confirmation(db_map)[source]
_commit_db_map_session(self, db_map)[source]
_rollback_db_map_session(self, db_map)[source]
ok_to_close(self, db_map)[source]

Prompts the user to commit or rollback changes to given database map.

Returns:True if successfully committed or rolled back, False otherwise
Return type:bool
connect_signals(self)[source]

Connects signals.

error_msg(self, db_map_error_log)[source]
cache_items(self, item_type, db_map_data)[source]

Caches data for a given type. It works for both insert and update operations.

Parameters:
  • item_type (str) –
  • db_map_data (dict) – lists of dictionary items keyed by DiffDatabaseMapping
update_icons(self, db_map_data)[source]

Runs when object classes are added or updated. Setups icons for those classes. :param item_type: :type item_type: str :param db_map_data: lists of dictionary items keyed by DiffDatabaseMapping :type db_map_data: dict

entity_class_icon(self, db_map, entity_type, entity_class_id, for_group=False)[source]

Returns an appropriate icon for a given entity_class.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • entity_type (str) – either ‘object_class’ or ‘relationship_class’
  • entity_class_id (int) –
Returns:

QIcon

get_item(self, db_map, item_type, id_)[source]

Returns the item of the given type in the given db map that has the given id, or an empty dict if not found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • id (int) –
Returns:

dict

_pop_item(self, db_map, item_type, id_)[source]
get_item_by_field(self, db_map, item_type, field, value)[source]

Returns the first item of the given type in the given db map that has the given value for the given field Returns an empty dictionary if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • field (str) –
  • value
Returns:

dict

get_items_by_field(self, db_map, item_type, field, value)[source]

Returns all items of the given type in the given db map that have the given value for the given field. Returns an empty list if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
  • field (str) –
  • value
Returns:

list

get_items(self, db_map, item_type)[source]

Returns all the items of the given type in the given db map, or an empty list if none found.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) –
Returns:

list

get_field(self, db_map, item_type, id_, field)[source]
static _display_data(parsed_data)[source]

Returns the value’s database representation formatted for Qt.DisplayRole.

static _tool_tip_data(parsed_data)[source]

Returns the value’s database representation formatted for Qt.ToolTipRole.

get_value(self, db_map, item_type, id_, role=Qt.DisplayRole)[source]

Returns the value or default value of a parameter.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) – either “parameter_definition” or “parameter_value”
  • id (int) – The parameter_value or definition id
  • role (int, optional) –
static parse_value(db_value)[source]
format_value(self, parsed_value, role=Qt.DisplayRole)[source]

Formats the given value for the given role.

Parameters:
  • parsed_value (object) – A python object as returned by spinedb_api.from_database
  • role (int, optional) –
get_value_indexes(self, db_map, item_type, id_)[source]

Returns the value or default value indexes of a parameter.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) – either “parameter_definition” or “parameter_value”
  • id (int) – The parameter_value or definition id
get_value_index(self, db_map, item_type, id_, index, role=Qt.DisplayRole)[source]

Returns the value or default value of a parameter for a given index.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • item_type (str) – either “parameter_definition” or “parameter_value”
  • id (int) – The parameter_value or definition id
  • index – The index to retrieve
  • role (int, optional) –
_split_and_parse_value_list(self, item)[source]
get_value_list_item(self, db_map, id_, index, role=Qt.DisplayRole)[source]

Returns one value item of a parameter_value_list.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • id (int) – The parameter_value_list id
  • index (int) – The value item index
  • role (int, optional) –
get_parameter_value_list(self, db_map, id_, role=Qt.DisplayRole)[source]

Returns a parameter_value_list formatted for the given role.

Parameters:
  • db_map (DiffDatabaseMapping) –
  • id (int) – The parameter_value_list id
  • role (int, optional) –
static get_db_items(query, key=lambda x: x['id'])[source]
static _make_query(db_map, sq_name, ids=())[source]
get_alternatives(self, db_map, ids=())[source]

Returns alternatives from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_scenarios(self, db_map, ids=())[source]

Returns scenarios from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_scenario_alternatives(self, db_map, ids=())[source]

Returns scenario alternatives from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_object_classes(self, db_map, ids=())[source]

Returns object classes from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_objects(self, db_map, ids=())[source]

Returns objects from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_relationship_classes(self, db_map, ids=())[source]

Returns relationship classes from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_relationships(self, db_map, ids=())[source]

Returns relationships from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_entity_groups(self, db_map, ids=())[source]

Returns entity groups from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_object_parameter_definitions(self, db_map, ids=())[source]

Returns object parameter definitions from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_relationship_parameter_definitions(self, db_map, ids=())[source]

Returns relationship parameter definitions from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_definitions(self, db_map, ids=())[source]

Returns both object and relationship parameter definitions.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_definition_tags(self, db_map, ids=())[source]

Returns parameter definition tags.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_object_parameter_values(self, db_map, ids=())[source]

Returns object parameter values from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_relationship_parameter_values(self, db_map, ids=())[source]

Returns relationship parameter values from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_values(self, db_map, ids=())[source]

Returns both object and relationship parameter values.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_value_lists(self, db_map, ids=())[source]

Returns parameter_value lists from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
get_parameter_tags(self, db_map, ids=())[source]

Get parameter tags from database.

Parameters:db_map (DiffDatabaseMapping) –
Returns:dictionary items
Return type:list
import_data(self, db_map_data, command_text='Import data')[source]

Imports the given data into given db maps using the dedicated import functions from spinedb_api. Condenses all in a single command for undo/redo.

Parameters:
  • db_map_data (dict(DiffDatabaseMapping, list)) – Maps dbs to data to be passed as keyword arguments to get_data_for_import
  • command_text (str, optional) – What to call the command that condenses the operation.
add_or_update_items(self, db_map_data, method_name, get_method_name, signal_name)[source]

Adds or updates items in db.

Parameters:
  • db_map_data (dict) – lists of items to add or update keyed by DiffDatabaseMapping
  • method_name (str) – attribute of DiffDatabaseMapping to call for performing the operation
  • get_method_name (str) – attribute of SpineDBManager to call for getting affected items
  • signal_name (str) – signal attribute of SpineDBManager to emit if successful
add_alternatives(self, db_map_data)[source]

Adds alternatives to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_scenarios(self, db_map_data)[source]

Adds scenarios to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_object_classes(self, db_map_data)[source]

Adds object classes to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_objects(self, db_map_data)[source]

Adds objects to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_relationship_classes(self, db_map_data)[source]

Adds relationship classes to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_relationships(self, db_map_data)[source]

Adds relationships to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_object_groups(self, db_map_data)[source]

Adds object groups to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_entity_groups(self, db_map_data)[source]

Adds entity groups to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_definitions(self, db_map_data)[source]

Adds parameter definitions to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_values(self, db_map_data)[source]

Adds parameter values to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_checked_parameter_values(self, db_map_data)[source]

Adds parameter values in db without checking integrity.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_value_lists(self, db_map_data)[source]

Adds parameter_value lists to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
add_parameter_tags(self, db_map_data)[source]

Adds parameter tags to db.

Parameters:db_map_data (dict) – lists of items to add keyed by DiffDatabaseMapping
update_alternatives(self, db_map_data)[source]

Updates alternatives in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_scenarios(self, db_map_data)[source]

Updates scenarios in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_object_classes(self, db_map_data)[source]

Updates object classes in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_objects(self, db_map_data)[source]

Updates objects in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_relationship_classes(self, db_map_data)[source]

Updates relationship classes in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_relationships(self, db_map_data)[source]

Updates relationships in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_definitions(self, db_map_data)[source]

Updates parameter definitions in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_values(self, db_map_data)[source]

Updates parameter values in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_checked_parameter_values(self, db_map_data)[source]

Updates parameter values in db without checking integrity.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_expanded_parameter_values(self, db_map_data)[source]

Updates expanded parameter values in db without checking integrity.

Parameters:db_map_data (dict) – lists of expanded items to update keyed by DiffDatabaseMapping
update_parameter_value_lists(self, db_map_data)[source]

Updates parameter_value lists in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
update_parameter_tags(self, db_map_data)[source]

Updates parameter tags in db.

Parameters:db_map_data (dict) – lists of items to update keyed by DiffDatabaseMapping
set_scenario_alternatives(self, db_map_data)[source]

Sets scenario alternatives in db.

Parameters:db_map_data (dict) – lists of items to set keyed by DiffDatabaseMapping
set_parameter_definition_tags(self, db_map_data)[source]

Sets parameter_definition tags in db.

Parameters:db_map_data (dict) – lists of items to set keyed by DiffDatabaseMapping
remove_items(self, db_map_typed_ids)[source]
do_cascade_remove_items(self, db_map_typed_ids)[source]
do_remove_items(self, db_map_typed_ids)[source]

Removes items from database.

Parameters:db_map_typed_ids (dict) – lists of items to remove, keyed by item type (str), keyed by DiffDatabaseMapping
_pop_item(self, db_map, item_type, id_)[source]
uncache_items(self, db_map_typed_ids)[source]

Removes data from cache.

Parameters:db_map_typed_ids
static db_map_ids(db_map_data)[source]
static db_map_class_ids(db_map_data)[source]
_refresh_scenario_alternatives(self, db_map_data)[source]

Refreshes cached scenarios when updating scenario alternatives.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
_refresh_parameter_definitions_by_tag(self, db_map_data)[source]

Refreshes cached parameter definitions when updating parameter tags.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_relationship_classes(self, db_map_data)[source]

Refreshes cached relationship classes when updating object classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_relationships_by_object(self, db_map_data)[source]

Refreshed cached relationships in cascade when updating objects.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions(self, db_map_data)[source]

Refreshes cached parameter definitions in cascade when updating entity classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions_by_value_list(self, db_map_data)[source]

Refreshes cached parameter definitions when updating parameter_value lists.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_entity_class(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating entity classes.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_entity(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating entities.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_alternative(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating alternatives.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_values_by_definition(self, db_map_data)[source]

Refreshes cached parameter values in cascade when updating parameter definitions.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
cascade_refresh_parameter_definitions_by_tag(self, db_map_data)[source]

Refreshes cached parameter definitions when updating parameter tags.

Parameters:db_map_data (dict) – lists of updated items keyed by DiffDatabaseMapping
find_cascading_relationship_classes(self, db_map_ids)[source]

Finds and returns cascading relationship classes for the given object_class ids.

find_cascading_entities(self, db_map_ids, item_type)[source]

Finds and returns cascading entities for the given entity_class ids.

find_cascading_relationships(self, db_map_ids)[source]

Finds and returns cascading relationships for the given object ids.

find_cascading_parameter_data(self, db_map_ids, item_type)[source]

Finds and returns cascading parameter definitions or values for the given entity_class ids.

find_cascading_parameter_definitions_by_value_list(self, db_map_ids)[source]

Finds and returns cascading parameter definitions for the given parameter_value_list ids.

find_cascading_parameter_definitions_by_tag(self, db_map_ids)[source]

Finds and returns cascading parameter definitions for the given parameter_tag ids.

find_cascading_parameter_values_by_entity(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given entity ids.

find_cascading_parameter_values_by_definition(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given parameter_definition ids.

find_groups_by_entity(self, db_map_ids)[source]

Finds and returns groups for the given entity ids.

find_groups_by_member(self, db_map_ids)[source]

Finds and returns groups for the given entity ids.

find_cascading_parameter_values_by_alternative(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given parameter alternative ids.

find_cascading_alternative_scenarios_by_alternative(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given parameter alternative ids.

find_cascading_alternative_scenarios_by_scenario(self, db_map_ids)[source]

Finds and returns cascading parameter values for the given parameter alternative ids.