python.shotgun_model.data_handler module

class ShotgunDataHandler(cache_path)[source]

Bases: object

Abstract class that manages low level data storage for Qt models.

This class abstracts away the data management and allows the model to access the data in a simple tree-like fashion. Each node in the tree is also identified by a unique id and can be accessed directly via this id in an O(1) lookup.

It also offers fast serialization and loading. Each ShotgunDataHandler is connected to a single cache file on disk.

Each Qt model typically has a corresponding ShotgunDataHandler subclass where data related business logic is implemented. The following methods need to be implemented by all deriving classes:

  • generate_data_request - called by the model when it needs additional data to be loaded from shotgun. The data handler formulates the exact request to be sent out to the server.
  • update_data - the counterpart of generate_data_request: this is called when the requested shotgun data is returned and needs to be inserted into the data structure.

Data returned back from this class to the Model layer is always sent as ShotgunItemData object to provide a full encapsulation around the internals of this class.

ADDED = 1
DELETED = 2
FORMAT_VERSION = 27
UPDATED = 0
generate_child_nodes(**kwargs)[source]

Generate nodes recursively from the data set

each node will be passed to the factory method for construction.

unique id can be none, meaning generate the top level of the tree

Parameters:
  • unique_id – Unique identifier, typically an int or a string
  • parent_object – Parent object that the requester wants to parent newly created nodes to. This object is passed into the node creation factory method as nodes are being created.
  • factory_fn – Method to execute whenever a child node needs to be created. The factory_fn will be called with the following syntax: factory_fn(parent_object, data_item), where parent_object is the parent_object parameter and data_item is a ShotgunItemData representing the data that the node should be associated with.
Returns:

number of items generated.

generate_data_request(data_retriever, *args, **kwargs)[source]

Generate a data request for a data retriever. Subclassed implementations can add arbitrary arguments in order to control the parameters and loading state.

Once the data has arrived, the caller is expected to call meth:update_data and pass in the received data payload for processing.

Parameters:data_retrieverShotgunDataRetriever instance.
Returns:Request id or None if no work is needed
get_data_item_from_uid(unique_id)[source]

Given a unique id, return a ShotgunItemData Returns None if the given uid is not present in the cache.

Unique ids are constructed by ShotgunDataHandler and are usually retrieved from a ShotgunItemData. They are implementation specific and can be any type object, but are normally strings, ints or None for the root node.

Parameters:unique_id – unique identifier
Returns:ShotgunItemData
is_cache_available()[source]

Returns true if the cache exists on disk, false if not.

Returns:boolean to indicate if cache exists on disk
is_cache_loaded()[source]

Returns true if the cache has been loaded into memory, false if not.

Returns:boolean to indicate if cache is loaded
load_cache(**kwargs)[source]

Loads a cache from disk into memory

remove_cache(**kwargs)[source]

Removes the associated cache file from disk and unloads cache data from memory.

Returns:True if the cache was sucessfully unloaded.
save_cache(**kwargs)[source]

Saves the current cache to disk.

unload_cache()[source]

Unloads any in-memory cache data.

update_data(sg_data)[source]

The counterpart to generate_data_request(). When the data request has been carried out, this method should be called by the calling class and the data payload from Shotgun should be provided via the sg_data parameter. Deriving classes implement the business logic for how to insert the data correctly into the internal data structure.

A list of differences should be returned, indicating which nodes were added, deleted and modified, on the following form:

[
 {
    "data": ShotgunItemData instance,
    "mode": self.UPDATED|ADDED|DELETED
 },
 {
    "data": ShotgunItemData instance,
    "mode": self.UPDATED|ADDED|DELETED
 },
 ...
]
Parameters:sg_data – data payload, usually a dictionary
Returns:list of updates. see above