class Session
Represents a Granta MI Session. Allows the MI Scripting Toolkit to make calls to the Service Layer, and provides access to and search functions for available databases. UseSessionBuilder to create a session and authenticate via one of the supported modes of authentication.
Examples
Authenticating using Windows authenticationbulk_delete_or_withdraw_records(records, batch_size=100, parallelize=False, max_num_threads=6, update_release_states=False, withdrawal_notes=None)
Deletes records from the server (or withdraws them, if in a version-controlled table). Record release states can also be updated using this method.Versionchanged
Changed in version 4.2: Argumentparallelise was renamed to parallelize.
- Parameters:
- records (list [Record ]) – List of records to delete or withdraw.
- batch_size (int , default: 100) – Number of records to process in each batch.
- parallelize (bool, default:
False) – Whether to parallelize the operation. - max_num_threads (int , default: 6) – Maximum number of threads to use when parallelizing.
- update_release_states (bool, default:
False) – Whether to update release states after deletion/withdrawal. - withdrawal_notes (str , optional) – Notes to add when withdrawing records.
- Raises: InvalidRecordStateError – If one or more records have not been pushed to the server.
WARNING
parallelize=True should not be set when using long-running sessions authenticated via OIDC. See
OIDC and parallel operation for more details.
bulk_fetch_release_states(records, batch_size=100, parallelize=False, max_num_threads=6)
Fetches and populates the release states of the specifiedRecord objects.
Versionchanged
Changed in version 4.2: Argumentparallelise was renamed to parallelize.
- Parameters:
- records (list [Record ]) – List of records to fetch release states for.
- batch_size (int , default: 100) – Number of records to process in each batch.
- parallelize (bool, default:
False) – Whether to parallelize the fetch operation. - max_num_threads (int , default: 6) – Maximum number of threads to use when parallelizing.
- Raises: InvalidRecordStateError – If one or more records have not been pushed to the server.
WARNING
parallelize should not be set to True when using long-running sessions authenticated via
OIDC. See OIDC and parallel operation for more details.
SEE ALSO
Table.bulk_fetch_all_record_versions()
: Also populates the Record.all_versions property.
get_db(name=None, db_key=None, guid=None, version_guid=None)
Returns the database matching the specified criteria. Provide at least one of:-
Database name:
name -
Database key:
db_key -
Database GUID:
guid -
Database version GUID:
version_guid - Parameters:
- Returns: Database object matching the specified criteria.
- Return type: Database
- Raises:
get_record_by_id(db_key, hguid=None, vguid=None, history_identity=None, version_number=None)
Returns the record with the specified ID from the database specified bydb_key.
This method resolves records with the following (sets of) references in priority order:
vguid(record version GUID): Uniquely identifies specific record version.history_identity(record history identity): Uniquely identifies record only. Can be optionally combined withversion_number(record version number) to identify a specific record version.hguid(record history GUID): Uniquely identifies record only.
vguid or version_number are not provided for version controlled records, this method will return the
latest available version of the record available to the user. The latest available version is dependent on the
user’s Granta MI permissions.
- Parameters:
- db_key (str) – Database key.
- hguid (str , optional) – Record history GUID, uniquely identifies record.
- vguid (str , optional) – Record version GUID, uniquely identifies record version.
- history_identity (int , optional) – Record history identity.
- version_number (int , optional) – Record version number.
- Returns: Record object with the specified ID.
- Return type: Record
get_records_by_ids(record_identifiers)
Returns aRecord object for each identifier provided.
The returned list of records will have the same number of elements as the provided list of identifiers. If no
record corresponds to that identifier then the corresponding item in the returned list will be None.
Each element in the record_identifiers parameter should be a dictionary with the db_key entry.
Additionally, it must contain one or more of the following (sets of) references in priority order:
vguid(record version GUID): Uniquely identifies specific record version.history_identity(record history identity): Uniquely identifies record only. Can be optionally combined withversion_number(record version number) to identify a specific record version.hguid(record history GUID): Uniquely identifies record only.
vguid or version_number are not provided for version controlled records, this method will
return the latest available version of the record available to the user. The latest available version
is dependent on the user’s Granta MI permissions.
- Parameters: record_identifiers (Sequence [dict [str , str | int ] ]) – Sequence of dictionaries containing record identifiers.
- Returns:
List of Record objects corresponding to the identifiers, or
Noneif the reference did not identify a resolvable record. - Return type: list[Record | None]
persist_oidc_token()
Stores a refresh token in the credential manager for your system for unattended later use. Thekeyring package uses system-managed storage, and supports the following credential managers:
- Windows Credential Manager
- macOS Keychain
- Freedesktop Secret Service
- KDE4 and KDE5 KWallet
WARNING
This method raises awin32ctypes.pywin32.pywintypes.error when it is used to persist a refresh token
longer than 1280 characters in the Windows Credential Manager. If this error is raised, use an alternative
keyring backend. A list of alternative backends are available on the keyring PyPI page.
records_from_string(text, use_strict_version=True)
GeneratesRecord objects from a tab separated values (.tsv) record list.
Notes
The required string format to identify a record is tab separated values (.tsv) with column headersRecordHistoryGuid, RecordGuid, Table, DatabaseKey and RecordFullName. This format may be
generated by copying an MI Viewer record list to the clipboard.
In MI Viewer, a field name may be prefixed by an asterisk to denote a default Match Column. This functionality
is not supported in the MI Scripting Toolkit. Either the RecordHistoryGuid (if use_strict_mode = False)
or RecordGuid (if use_strict_mode = True) will be used to identify the record.
In One MI, use MI Favorites and PyGranta RecordLists to transfer a collection of records from the browser to
Scripting Toolkit.
refetch_dbs()
Fetches the list of databases from the server.Notes
Makes a Service Layer call.search_for_records_by_name(name)
Searches by record name over all databases (usesDatabase.search_for_records_by_name()).
Notes
Makes a Service Layer call for each table in each database.search_for_records_by_text(text)
Performs a simple text search over all databases (usesDatabase.search_for_records_by_text()).
Notes
Makes a Service Layer call for each table in each database.update(records, update_attributes=True, update_links=False, refresh_attributes=True, include_binary_data_in_refresh=False, notes=None, release_notes=None)
Pushes any changes in the specified records (including adding newly-created records) to the server. Returns a list of successfully importedRecord objects. If refresh_attributes set to True, the
imported records’ attributes are exported after the update is complete.
- Parameters:
- records (list [Record ]) – List of records to update.
- update_attributes (bool, default:
True) – Whether to include attribute updates. - update_links (bool, default:
False) – Whether to include link updates. - refresh_attributes (bool, default:
True) – Whether to include updated attribute values in the returned records. See Notes for more details. - include_binary_data_in_refresh (bool, default:
False) – Whether to include binary data in the refresh. See Notes for more details. - notes (str , optional) – Revision notes for the update. Applies to all records in the update.
- release_notes (str , optional) – Version release notes for the update. Applies to all records flagged for release in the update.
- Returns: List of successfully updated Record objects.
- Return type: list[Record]
- Raises:
- ValueError – If a
Recordis imported withnameset toNone. - UnexportedBinaryDataError – If a tabular attribute has been modified destructively and contains unexported binary data that has not been overwritten.
- ValueError – If a
WARNING
Ifinclude_binary_data_in_refresh is set to False, the binary_data property and save() method are
both still available for backwards compatibility. However, they will download the data from Granta MI on
demand for each attribute value. A warning will be generated each time this occurs, but may only occur once
due to the Python warning configuration settings.
This behavior is likely to have a significant performance impact, and so it is recommended that if access to
the binary data is required without a subsequent HTTP GET request, you should specify
include_binary_data_in_refresh = True.
On-demand access to attribute values without using Table.bulk_fetch() is unaffected.
Notes
Ifrefresh_attributes is set to True (default), the data will be re-exported after import. If data
is not required for subsequent operations, setting refresh_attributes to False can improve
performance of this operation significantly.
If include_binary_data_in_refresh is set to False (default), the export will not fetch the binary data
representing the request File and Picture attributes and will instead export URLs (see the
BinaryType.url documentation for more information on how to use the URL to access the data). This
setting is recommended for large files and pictures, or if the binary data itself is not required.
To summarize how the file and picture data can be accessed:
include_binary_data_in_refresh = False(default): TheBinaryType.urlproperty is populated.include_binary_data_in_refresh = True: TheBinaryType.binary_dataproperty, andAttributeFile.save()andAttributePicture.save()methods are all available to access and save the binary data. TheBinaryType.urlproperty is empty and returnsNone.
update_links(records)
Pushes any changes in the links of the specified records (including newly-created records) to the server. Returns a list of successfully modifiedRecord objects. All source and target records must be exported
to/from the server before being linked.
- Parameters: records (list [Record ]) – List of records to update links for.
- Returns: List of successfully updated Record objects.
- Return type: list[Record]
- Raises: InvalidRecordStateError – If one or more records have not been pushed to the server.
property dbs : List[Database]
List of databases on the server.property dbs_by_guid : Dict[str, Database]
Dictionary of databases on the server, indexed by database GUIDproperty dbs_by_key : Dict[str, Database]
Dictionary of databases on the server, indexed by database key.property refresh_token : str | None
The refresh token returned by the OIDC identity provider if one was provided. If a refresh token was not returned by the OIDC identity provider, the initial provided token is returned.- Return type: str, optional
WARNING
Refresh tokens are bearer tokens that grant access to the Granta MI server, and so should be treated as confidential. To use a refresh token for unattended Scripting Toolkit authentication, use thepersist_oidc_token() method to persist the refresh token in a secure credential store.
property service_layer_url : str
Service Layer URL.- Return type: str
property viewer_base_url : str
MI Viewer URL associated with the Service Layer.- Return type: str
class SessionBuilder(service_layer_url, session_configuration)
Builds aSession.
- Parameters:
- service_layer_url (str) – Granta MI Service Layer URL.
- session_configuration (SessionConfiguration , optional) – Additional settings for the connection.
Notes
If MI Scripting Toolkit has been installed on the Granta MI application server, then the Granta MI application server name must be specified in full in the url parameter. If you use ‘localhost’ or a loopback address, MI Scripting Toolkit will be unable to use the specified user account to connect to Granta MI.with_oidc()
Connect with OIDC.- Returns: An OIDC Session builder to finalize the OIDC connection.
- Return type: OIDCSessionBuilder
with_autologon()
Connect with Windows Authentication.- Return type: Session
with_credentials(username, password, domain=None, store_password=False)
Connect using provided credentials.- Parameters:
- Return type: Session
class OIDCSessionBuilder
OIDC session builder. Returned bySessionBuilder.with_oidc().
with_access_token(token)
Authenticate the user via the provided access token. Access tokens have short lifespans. The session will no longer be authenticated once the token expires.with_authorization_code_flow()
Authenticate the user interactively via a web browser. Requires a web browser on the client machine.- Return type: Session
with_device_code_flow()
Authenticate the user interactively using device code flow.- Return type: Session
with_refresh_token(token)
Authenticate the user via the provided refresh token.with_stored_token()
Authenticate the user via a persisted token. The token must have been persisted to the system credential manager viaSession.persist_oidc_token().
- Return type: Session