class documentation

Higher-level wrapper for deploying projects and bulk import/export of data.

Method __init__ Initialise the Gateway.
Method clear_down Clear all data from all datasets in the project.
Method dump_data Export entity data from the project to JSON files on disk.
Method dump_project Export the project schema and dataset configurations to JSON files.
Method load_data Load data from JSON files into the DataGraphs project.
Method load_project Deploy the project schema and datasets to the API.
Property client The underlying Client instance.
Static Method _log_dump_result Undocumented
Static Method _log_load_result Undocumented
Static Method _record_put_result Undocumented
Method _classes_to_dump The classes to export, in order: every non-base, non-excluded class across all datasets, or just class_name.
Method _classes_to_load The classes to load, in order, each with the slug of the dataset it loads into.
Method _dump_class Export one class to its file, accounting for it in result.
Method _dump_datasets Dump the project datasets to JSON files.
Method _dump_schema Dump the project schema to a JSON file.
Method _ensure_no_duplicate_classes Validate that no duplicate class names are found across the provided datasets.
Method _get_schema Undocumented
Method _is_empty_project Undocumented
Method _load_data_for_class Load data for a specific class from a JSON file, accounting for it in result.
Method _load_from_file Read a JSON file and PUT its contents into the project.
Method _map_data_project_urns Re-map URN project segments to match the current client project.
Method _persist_to_file Fetch entities of class_name from the API and write them to a JSON file.
Method _validate_datasets Undocumented
Method _verify_datasets_against_classlist Undocumented
Instance Variable _client Undocumented
Instance Variable _schema Undocumented
Instance Variable _wait_time_ms Undocumented
def __init__(self, client: DatagraphsClient, wait_time_ms: int = DatagraphsClient.DEFAULT_WAIT_TIME_MS):

Initialise the Gateway.

Parameters
client:DatagraphsClientA DataGraphs API client.
wait_time_ms:intDelay in milliseconds between successive API calls.
def clear_down(self):

Clear all data from all datasets in the project.

The datasets themselves are preserved.

def dump_data(self, to_dir_path: str | Path, class_name: str = Schema.ALL_CLASSES, excluded_class_names: str = '', include_date_fields: bool = False) -> DumpResult:

Export entity data from the project to JSON files on disk.

Each class is written to a separate <ClassName>.json file, and only once every entity of the class has been read, so a file never holds a partial export. A class that cannot be read is reported in DumpResult.failed and the remaining classes are still exported: check DumpResult.ok on the result. Base classes are automatically skipped.

Parameters
to_dir_path:str | PathTarget directory. Created automatically if it does not exist.
class_name:strThe class name to dump, or Schema.ALL_CLASSES for all.
excluded_class_names:strComma-separated list of classes to exclude from the export if class_name is Schema.ALL_CLASSES.
include_date_fields:boolWhether to include system date metadata.
Returns
DumpResultThe outcome of the export, per class.
Raises
AuthenticationErrorIf authentication fails; no further classes are attempted. Its result is the DumpResult so far, with the interrupted class in failed and the classes not yet attempted in remaining.
TokenRequestErrorIf an auth token cannot be obtained; no further classes are attempted. Its result is the DumpResult so far, as for AuthenticationError.
def dump_project(self, schema_path: str | Path, datasets_path: str | Path):

Export the project schema and dataset configurations to JSON files.

Files are named {project_name}-v{version}-schema.json and {project_name}-v{version}-datasets.json.

Parameters
schema_path:str | PathDirectory to write the schema JSON file.
datasets_path:str | PathDirectory to write the datasets JSON file.
def load_data(self, class_name: str = Schema.ALL_CLASSES, from_dir_path: str | Path = '', file_path: str | Path = '', isolation: ISOLATION_MODE = ISOLATION_MODE.BISECT) -> LoadResult:

Load data from JSON files into the DataGraphs project.

Base classes (those with subclasses) are automatically skipped when loading all classes. Check LoadResult.ok on the result: a load that returns normally may still have failed or timed-out entities.

Parameters
class_name:strThe class name to load, or Schema.ALL_CLASSES to load every non-base class found across all datasets.
from_dir_path:str | PathDirectory containing <ClassName>.json files.
file_path:str | PathExplicit path to a single JSON file (used when loading a specific class_name).
isolation:ISOLATION_MODEHow to find the rejected entities of a batch the API rejects (see Client.put).
Returns
LoadResultThe outcome of the load, per class.
Raises
ValueErrorIf isolation is not an ISOLATION_MODE value.
AuthenticationErrorIf authentication fails; no further classes are attempted. Its result is the LoadResult so far, with the interrupted class's unwritten entities in unsent and the classes not yet attempted in remaining.
TokenRequestErrorIf an auth token cannot be obtained; no further classes are attempted. Its result is the LoadResult so far, as for AuthenticationError.
def load_project(self, schema: Schema, datasets: list[Dataset], validation_mode: VALIDATION_MODE = VALIDATION_MODE.PROMPT, schema_apply_mode: SCHEMA_APPLY_MODE = SCHEMA_APPLY_MODE.APPLY):

Deploy the project schema and datasets to the API.

N.B. This deletes and/or removes all blocking data dependencies before applying the new schema and datasets.

Parameters
schema:SchemaThe schema to deploy.
datasets:list[Dataset]The datasets to deploy.
validation_mode:VALIDATION_MODEHow to handle mismatches between deployment and existing datasets. See VALIDATION_MODE.
schema_apply_mode:SCHEMA_APPLY_MODEUndocumented
Raises
ValueErrorIf validation_mode is not a VALIDATION_MODE value, or duplicate class names are found across datasets.
@property
client: DatagraphsClient =

The underlying Client instance.

@staticmethod
def _log_dump_result(result: DumpResult):

Undocumented

@staticmethod
def _log_load_result(result: LoadResult):

Undocumented

@staticmethod
def _record_put_result(result: LoadResult, class_name: str, put_result: PutResult):

Undocumented

def _classes_to_dump(self, class_name: str, excluded_class_names: str) -> list[str]:

The classes to export, in order: every non-base, non-excluded class across all datasets, or just class_name.

def _classes_to_load(self, result: LoadResult, class_name: str) -> list[tuple[str, str]]:

The classes to load, in order, each with the slug of the dataset it loads into.

A single requested class that is in no dataset is recorded as skipped.

def _dump_class(self, result: DumpResult, class_name: str, to_dir_path: Path, include_date_fields: bool):

Export one class to its file, accounting for it in result.

def _dump_datasets(self, datasets_path: str | Path, name_prefix: str):

Dump the project datasets to JSON files.

def _dump_schema(self, schema_path: str | Path, name_prefix: str):

Dump the project schema to a JSON file.

def _ensure_no_duplicate_classes(self, datasets: list[Dataset]):

Validate that no duplicate class names are found across the provided datasets.

def _get_schema(self) -> Schema:

Undocumented

def _is_empty_project(self, datasets: list[Dataset]) -> bool:

Undocumented

def _load_data_for_class(self, result: LoadResult, class_name: str, dataset_slug: str, isolation: ISOLATION_MODE, from_dir_path: str | Path = '', file_path: str | Path = ''):

Load data for a specific class from a JSON file, accounting for it in result.

def _load_from_file(self, class_name: str, dataset_slug: str, isolation: ISOLATION_MODE, from_dir_path: str | Path = '', file_path: str | Path = '') -> PutResult | SkippedClass:

Read a JSON file and PUT its contents into the project.

Returns:
The outcome of the write, or why the class was skipped if there was no file or it held no entities.
def _map_data_project_urns(self, data: list[dict]) -> list[dict]:

Re-map URN project segments to match the current client project.

Args:
data: A list of entity dicts, each containing an id URN.
Returns:
The list with URNs re-mapped where necessary.
Raises:
ValueError: If an entity is not a dict, or has an id that is not a string.
def _persist_to_file(self, class_name: str, to_dir_path: Path, include_date_fields: bool) -> tuple[Path, int]:

Fetch entities of class_name from the API and write them to a JSON file.

The file is written to a temporary name and then renamed into place, so it is either complete or left as it was. It keeps the permissions of the file it replaces, or, for a new file, gets those the umask allows.

Returns
tuple[Path, int]The file written, and the number of entities in it.
def _validate_datasets(self, deployment_datasets: list[Dataset], existing_datasets: list[Dataset], validation_mode: VALIDATION_MODE):

Undocumented

def _verify_datasets_against_classlist(self, datasets: list[Dataset], classes: list[str]):

Undocumented

_client =

Undocumented

_schema =

Undocumented

_wait_time_ms =

Undocumented