Skip to content

cmem_client.repositories.protocols.import_item¤

Protocol interface for repository item import operations.

This module defines the ImportItemProtocol that repositories can implement to support importing items from files. This is commonly used for importing exported projects, graphs, or other resources into Corporate Memory.

The protocol supports replacement, skip-if-existing, fail-if-existing, and merge behaviours, controlled via the on_conflict parameter on import_item().

Examples:

A repository declares which import items it accepts and which import configuration applies when the caller passes none:

>>> from collections.abc import Sequence
>>> from typing import ClassVar
>>> from cmem_client.models.item import FileImportItem, ImportItem, ZipImportItem
>>> from cmem_client.repositories.base.plain_list import PlainListRepository
>>> from cmem_client.repositories.projects import ProjectsImportConfig
>>> from cmem_client.repositories.protocols.import_item import (
...     ImportConfig,
...     ImportItemProtocol,
... )
>>> class ProjectsRepository(PlainListRepository, ImportItemProtocol):
...     _allowed_import_items: ClassVar[Sequence[type[ImportItem]]] = [
...         FileImportItem,
...         ZipImportItem,
...     ]
...     _default_import_config: ImportConfig | None = ProjectsImportConfig()

Classes:

Attributes:

ImportConfig¤

Bases: Model, ABC

Abstract base class for Import Item Configuration Objects

Attributes:

  • use_archive_handler (bool) – When True, automatically uses ArchiveHandler to handle zip files, directories, and single files transparently.

model_config¤

model_config = ConfigDict(extra='allow', populate_by_name=True)

use_archive_handler¤

use_archive_handler: bool = True

ImportConflictPolicy¤

Bases: StrEnum

Controls behavior when the import target already exists.

REPLACE: Delete the existing item, then import the new one. SKIP: Leave the existing item untouched and return without importing. FAIL: Raise an error if the item already exists. MERGE: Add the imported data to the existing item without clearing it first.

Attributes:

FAIL¤

FAIL = 'fail'

MERGE¤

MERGE = 'merge'

REPLACE¤

REPLACE = 'replace'

SKIP¤

SKIP = 'skip'

ImportItemConfig_contra¤

ImportItemConfig_contra = TypeVar('ImportItemConfig_contra', bound=ImportConfig, contravariant=True)

ImportItemProtocol¤

Bases: Protocol[ItemType, ImportItemConfig_contra]

Protocol which allows for importing of items from a file path.

Attributes:

  • _client (Client) – Corporate Memory client used for the HTTP requests of this repository.
  • _dict (dict[str, ItemType]) – Cached contents of the repository, mapping the key of each item to the item itself. Backs the Mapping interface and is populated by fetch_data().
  • _allowed_import_items (Sequence[type[ImportItem]]) – ImportItem types this repository accepts. Repositories may declare it to narrow or widen what import_item() takes. If not defined, defaults to FileImportItem and ZipImportItem, which excludes DirectoryImportItem.
  • _default_import_config (ImportConfig | None) – Import configuration applied when the caller passes none. Repositories declare it for example when use_archive_handler has to be turned

off. If not defined, defaults to None.

  • _logger (Logger) – Logger of this repository, created lazily on first access through the logger property as a child of the client logger.

Functions:

  • import_item – Import an exported file to the repository

import_item¤

import_item(path=None, key=None, on_conflict=ImportConflictPolicy.FAIL, configuration=None)

Import an exported file to the repository

By default, automatically handles zip files, directories, and single files using ImportItem model. Can be disabled by setting use_archive_handler=False in the configuration.

Returns:

  • str – The key of the imported item.

Raises:

  • RepositoryModificationError – If the item already exists and the conflict policy is FAIL, if the import type is not allowed for this repository, if the import request failed, or if the item is not present afterwards.

logger¤

logger: logging.Logger

Gets the client logger

Comments