spinetoolbox.spine_db_manager
The SpineDBManager class.
Module Contents
Classes
Class to manage DBs within a project. |
Functions
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, parent, synchronous=False)[source]
Bases:
PySide6.QtCore.QObject
Class to manage DBs within a project.
- Parameters
settings (QSettings) – Toolbox settings
parent (QObject, optional) – parent object
synchronous (bool) – If True, fetch database synchronously
- items_added[source]
Emitted whenever items are added to a DB.
- Parameters
str – item type, such as “object_class”
dict – mapping DatabaseMapping to list of added dict-items.
- items_updated[source]
Emitted whenever items are updated in a DB.
- Parameters
str – item type, such as “object_class”
dict – mapping DatabaseMapping to list of updated dict-items.
- items_removed[source]
Emitted whenever items are removed from a DB.
- Parameters
str – item type, such as “object_class”
dict – mapping DatabaseMapping to list of updated dict-items.
- _get_worker(db_map)[source]
Returns a worker.
- Parameters
db_map (DatabaseMapping) – database mapping
- Returns
worker for the db_map
- Return type
- register_fetch_parent(db_map, parent)[source]
Registers a fetch parent.
- Parameters
db_map (DatabaseMapping) – target database mapping
parent (FetchParent) – fetch parent
- can_fetch_more(db_map, parent)[source]
Whether or not we can fetch more items of given type from given db.
- Parameters
db_map (DatabaseMapping) –
parent (FetchParent) – The object that requests the fetching and that might want to react to further DB modifications.
- Returns
bool
- fetch_more(db_map, parent)[source]
Fetches more items of given type from given db.
- Parameters
db_map (DatabaseMapping) –
parent (FetchParent) – The object that requests the fetching.
- get_icon_mngr(db_map)[source]
Returns an icon manager for given db_map.
- Parameters
db_map (DatabaseMapping) –
- Returns
SpineDBIconManager
- static db_map_key(db_map)[source]
Creates an identifier for given db_map.
- Parameters
db_map (DatabaseMapping) – database mapping
- Returns
identification key
- Return type
int
- db_map_from_key(key)[source]
Returns database mapping that corresponds to given identification key.
- Parameters
key (int) – identification key
- Returns
database mapping
- Return type
DatabaseMapping
- Raises
KeyError – raised if database map is not found
- db_map(url)[source]
Returns a database mapping for given URL.
- Parameters
url (str) – a database URL
- Returns
a database map or None if not found
- Return type
DatabaseMapping
- close_session(url)[source]
Pops any db map on the given url and closes its connection.
- Parameters
url (str) –
- get_db_map(url, logger, ignore_version_error=False, window=False, codename=None, create=False)[source]
Returns a DatabaseMapping instance from url if possible, None otherwise. If needed, asks the user to upgrade to the latest db version.
- Parameters
url (str, URL) –
logger (LoggerInterface) –
ignore_version_error (bool, optional) –
window (bool, optional) –
codename (str, NoneType, optional) –
create (bool, optional) –
- Returns
DatabaseMapping, NoneType
- _do_get_db_map(url, **kwargs)[source]
Returns a memorized DatabaseMapping instance from url. Called by get_db_map.
- Parameters
url (str, URL) –
codename (str, NoneType) –
upgrade (bool) –
create (bool) –
- Returns
DatabaseMapping
- register_listener(listener, *db_maps)[source]
Register given listener for all given db_map’s signals.
- Parameters
listener (object) –
*db_maps –
- unregister_listener(listener, *db_maps, dirty_db_maps=None, commit_dirty=False, commit_msg='')[source]
Unregisters given listener from given db_map signals. If any of the db_maps becomes an orphan and is dirty, prompts user to commit or rollback.
- Parameters
listener (object) –
*db_maps –
commit_dirty (bool) – True to commit dirty database mapping, False to roll back
commit_msg (str) – commit message
- Returns
All the db maps that failed to commit
- Return type
failed_db_maps (list)
- update_data_store_db_maps()[source]
Updates the db maps of the dict that maps db_maps to Data Stores
- is_dirty(db_map)[source]
Returns True if mapping has pending changes.
- Parameters
db_map (DatabaseMapping) – database mapping
- Returns
True if db_map has pending changes, False otherwise
- Return type
bool
- dirty(*db_maps)[source]
Filters clean mappings from given database maps.
- Parameters
*db_maps – mappings to check
- Returns
dirty mappings
- Return type
list of DatabaseMapping
- dirty_and_without_editors(listener, *db_maps)[source]
Checks which of the given database mappings are dirty and have no editors.
- Parameters
listener (Any) – a listener object
*db_maps – mappings to check
- Returns
mappings that are dirty and don’t have editors
- Return type
list of DatabaseMapping
- commit_session(commit_msg, *dirty_db_maps, cookie=None)[source]
Commits the current session.
- Parameters
commit_msg (str) – commit message for all database maps
*dirty_db_maps – dirty database maps to commit
cookie (object, optional) – a free form identifier which will be forwarded to
SpineDBWorker.commit_session
- _do_commit_session(db_map, commit_msg, cookie=None)[source]
Commits session for given db.
- Parameters
db_map (DatabaseMapping) – db map
commit_msg (str) – commit message
cookie (Any) – a cookie to include in receive_session_committed call
- Returns
bool
- notify_session_committed(cookie, *db_maps)[source]
Notifies manager and listeners when a commit has taken place by a third party.
- Parameters
cookie (Any) – commit cookie
*db_maps – database maps that were committed
- rollback_session(*dirty_db_maps)[source]
Rolls back the current session.
- Parameters
*dirty_db_maps – dirty database maps to commit
- _do_rollback_session(db_map)[source]
Rolls back session for given db.
- Parameters
db_map (DatabaseMapping) – db map
- entity_class_renderer(db_map, entity_class_id, for_group=False, color=None)[source]
Returns an icon renderer for a given entity class.
- Parameters
db_map (DatabaseMapping) – database map
entity_class_id (int) – entity class id
for_group (bool) – if True, return the group object icon instead
color (QColor, optional) – icon color
- Returns
requested renderer or None if no entity class was found
- Return type
QSvgRenderer
- entity_class_icon(db_map, entity_class_id, for_group=False)[source]
Returns an appropriate icon for a given entity class.
- Parameters
db_map (DatabaseMapping) – database map
entity_class_id (int) – entity class’ id
for_group (bool) – if True, return the group object icon instead
- Returns
requested icon or None if no entity class was found
- Return type
QIcon
- static get_item(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 (DatabaseMapping) –
item_type (str) –
id (TempId) –
- Returns
the item
- Return type
PublicItem
- static get_items(db_map, item_type)[source]
Returns a list of the items of the given type in the given db map.
- Parameters
db_map (DatabaseMapping) –
item_type (str) –
- Returns
list
- get_items_by_field(db_map, item_type, field, value)[source]
Returns a list of items of the given type in the given db map that have the given value for the given field.
- Parameters
db_map (DatabaseMapping) –
item_type (str) –
field (str) –
value –
- Returns
list
- get_item_by_field(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 (DatabaseMapping) –
item_type (str) –
field (str) –
value –
- Returns
dict
- static display_data_from_parsed(parsed_data)[source]
Returns the value’s database representation formatted for Qt.ItemDataRole.DisplayRole.
- static tool_tip_data_from_parsed(parsed_data)[source]
Returns the value’s database representation formatted for Qt.ItemDataRole.ToolTipRole.
- get_value(db_map, item, role=Qt.ItemDataRole.DisplayRole)[source]
Returns the value or default value of a parameter.
- Parameters
db_map (DatabaseMapping) – database mapping
item (PublicItem) – parameter value item, parameter definition item, or list value item
role (Qt.ItemDataRole) – data role
- Returns
any
- get_value_from_data(data, role=Qt.ItemDataRole.DisplayRole)[source]
Returns the value or default value of a parameter directly from data. Used by
EmptyParameterModel.data()
.- Parameters
data (str) – joined value and type
role (int, optional) –
- Returns
any
- _format_value(parsed_value, role=Qt.ItemDataRole.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(db_map, item_type, id_)[source]
Returns the value or default value indexes of a parameter.
- Parameters
db_map (DatabaseMapping) –
item_type (str) – either “parameter_definition” or “parameter_value”
id (int) – The parameter_value or definition id
- get_value_index(db_map, item_type, id_, index, role=Qt.ItemDataRole.DisplayRole)[source]
Returns the value or default value of a parameter for a given index.
- Parameters
db_map (DatabaseMapping) –
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) –
- get_value_list_item(db_map, id_, index, role=Qt.ItemDataRole.DisplayRole)[source]
Returns one value item of a parameter_value_list.
- Parameters
db_map (DatabaseMapping) –
id (int) – The parameter_value_list id
index (int) – The value item index
role (int, optional) –
- get_parameter_value_list(db_map, id_, role=Qt.ItemDataRole.DisplayRole)[source]
Returns a parameter_value_list formatted for the given role.
- Parameters
db_map (DatabaseMapping) –
id (int) – The parameter_value_list id
role (int, optional) –
- import_data(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(DatabaseMapping, dict())) – 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_alternatives(db_map_data)[source]
Adds alternatives to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_scenarios(db_map_data)[source]
Adds scenarios to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_entity_classes(db_map_data)[source]
Adds entity classes to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_entities(db_map_data)[source]
Adds entities to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_entity_groups(db_map_data)[source]
Adds entity groups to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_entity_alternatives(db_map_data)[source]
Adds entity alternatives to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_parameter_definitions(db_map_data)[source]
Adds parameter definitions to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_parameter_values(db_map_data)[source]
Adds parameter values to db without checking integrity.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_parameter_value_lists(db_map_data)[source]
Adds parameter_value lists to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_list_values(db_map_data)[source]
Adds parameter_value list values to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_metadata(db_map_data)[source]
Adds metadata to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_entity_metadata(db_map_data)[source]
Adds entity metadata to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_parameter_value_metadata(db_map_data)[source]
Adds parameter value metadata to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_ext_entity_metadata(db_map_data)[source]
Adds entity metadata together with all necessary metadata to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- add_ext_parameter_value_metadata(db_map_data)[source]
Adds parameter value metadata together with all necessary metadata to db.
- Parameters
db_map_data (dict) – lists of items to add keyed by DatabaseMapping
- update_alternatives(db_map_data)[source]
Updates alternatives in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_scenarios(db_map_data)[source]
Updates scenarios in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_entity_classes(db_map_data)[source]
Updates entity classes in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_entities(db_map_data)[source]
Updates entities in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_entity_alternatives(db_map_data)[source]
Updates entity alternatives in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_parameter_definitions(db_map_data)[source]
Updates parameter definitions in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_parameter_values(db_map_data)[source]
Updates parameter values in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_expanded_parameter_values(db_map_data)[source]
Updates expanded parameter values in db.
- Parameters
db_map_data (dict) – lists of expanded items to update keyed by DatabaseMapping
- update_parameter_value_lists(db_map_data)[source]
Updates parameter_value lists in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_list_values(db_map_data)[source]
Updates parameter_value list values in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_metadata(db_map_data)[source]
Updates metadata in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_entity_metadata(db_map_data)[source]
Updates entity metadata in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_parameter_value_metadata(db_map_data)[source]
Updates parameter value metadata in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_ext_entity_metadata(db_map_data)[source]
Updates entity metadata in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- update_ext_parameter_value_metadata(db_map_data)[source]
Updates parameter value metadata in db.
- Parameters
db_map_data (dict) – lists of items to update keyed by DatabaseMapping
- set_scenario_alternatives(db_map_data)[source]
Sets scenario alternatives in db.
- Parameters
db_map_data (dict) – lists of items to set keyed by DatabaseMapping
- static get_data_to_set_scenario_alternatives(db_map, scenarios)[source]
Returns data to add and remove, in order to set wide scenario alternatives.
- Parameters
db_map (DatabaseMapping) – the db_map
scenarios –
One or more wide scenario
dict
objects to set. Each item must include the following keys:”id”: integer scenario id
”alternative_id_list”: list of alternative ids for that scenario
- Returns
list: scenario_alternative
dict
objects to add. set: integer scenario_alternative ids to remove
- purge_items(db_map_item_types, **kwargs)[source]
Purges selected items from given database.
- Parameters
db_map_item_types (dict) – mapping from database map to list of purgable item types
**kwargs – keyword arguments passed to
remove_items()
- add_items(item_type, db_map_data, identifier=None, **kwargs)[source]
Pushes commands to add items to undo stack.
- update_items(item_type, db_map_data, identifier=None, **kwargs)[source]
Pushes commands to update items to undo stack.
- add_update_items(item_type, db_map_data, identifier=None, **kwargs)[source]
Pushes commands to add_update items to undo stack.
- remove_items(db_map_typed_ids, identifier=None, **kwargs)[source]
Pushes commands to remove items to undo stack.
- do_remove_items(db_map, item_type, ids, check=True)[source]
Removes items from database.
- Parameters
db_map – DatabaseMapping instance
item_type (str) – database item type
ids (set) – ids to remove
- do_restore_items(db_map, item_type, ids)[source]
Restores items in database.
- Parameters
db_map – DatabaseMapping instance
item_type (str) – database item type
ids (set) – ids to restore
- find_cascading_entity_classes(db_map_ids)[source]
Finds and returns cascading entity classes for the given dimension ids.
- find_cascading_entities(db_map_ids)[source]
Finds and returns cascading entities for the given element ids.
- find_cascading_parameter_data(db_map_ids, item_type)[source]
Finds and returns cascading parameter definitions or values for the given entity_class ids.
- find_cascading_parameter_values_by_entity(db_map_ids)[source]
Finds and returns cascading parameter values for the given entity ids.
- find_cascading_parameter_values_by_definition(db_map_ids)[source]
Finds and returns cascading parameter values for the given parameter_definition ids.
- find_cascading_scenario_alternatives_by_scenario(db_map_ids)[source]
Finds and returns cascading scenario alternatives for the given scenario ids.
- duplicate_entity(orig_name, dup_name, class_name, db_maps)[source]
Duplicates entity, its parameter values and related multidimensional entities.
- Parameters
orig_name (str) – original entity’s name
dup_name (str) – duplicate’s name
class_name (str) – entity class name
db_maps (Iterable of DatabaseMapping) – database mappings where duplication should take place
- static export_to_json(file_path, data_for_export, caller)[source]
Exports given data into JSON file.
- static export_to_excel(file_path, data_for_export, caller)[source]
Exports given data into Excel file.
- static get_all_multi_spine_db_editors()[source]
Yields all instances of MultiSpineDBEditor currently open.
- Yields
MultiSpineDBEditor
- get_all_spine_db_editors()[source]
Yields all instances of SpineDBEditor currently open.
- Yields
SpineDBEditor
- open_db_editor(db_url_codenames, reuse_existing_editor)[source]
Opens a SpineDBEditor with given urls. Uses an existing MultiSpineDBEditor if any. Also, if the same urls are open in an existing SpineDBEditor, just raises that one instead of creating another.
- Parameters
db_url_codenames (dict) – mapping url to codename
reuse_existing_editor (bool) – if True and the same URL is already open, just raise the existing window