qhana_plugin_runner.storage module

Module containing a file store interface with a implementation for the local file system.

class qhana_plugin_runner.storage.FileStore(app: Flask | None = None)

Bases: FileStoreInterface

Interface class for file store implementations.

get_task_file_url(file_info: TaskFile, external: bool = True) str

Get an URL for a TaskFile object.

See get_file_url()

Parameters:

file_info (TaskFile) – the information of the task file

Returns:

the URL to the file

Return type:

str

init_app(app: Flask)

Init the file store with the Flask app to get access to the flask config.

name: ClassVar[str]
open(file_info: TaskFile) ContextManager[BinaryIO]

Returns a context manager for a file-like object for reading the contents of a task file. :param file_info: the information of the task file :return: context manager for a file-like object

persist_task_result(task_db_id: int, file_: IO | str | bytes, file_name: str, file_type: str, mimetype: str, commit: bool = True) TaskFile

Perist a task result file and store the file information in the database.

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (Path) – the file name of the result file

  • file_type (str) – the file type tag

  • mimetype (str) – the mime type of the file (not optional for result files!)

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Raises:

KeyError – if the task could not be found in the database

Returns:

the file information stored in the database

Return type:

TaskFile

persist_task_temp_file(task_db_id: int, file_: IO | str | bytes, file_name: str, mimetype: str | None = None, commit: bool = True) TaskFile

Perist a temporary task file and store the file information in the database.

Temporary files should only be used during the execution of the task. All temporary files have the file type tag "temp-file".

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (str) – the file name of the result file

  • mimetype (Optional[str]) – the mime type of the file. Defaults to None.

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Returns:

the file information stored in the database

Return type:

TaskFile

prepare_path(path: str | Path) Path

Prepare a file path before saving a file.

The returned path object is always a relative path. If the path is not relative a path relative to the root of that path is returned.

Mitigates simple path traversal attacs.

Parameters:

path (Union[str, Path]) – The file path to prepare

Raises:

ValueError – If the path contains potentially dangerous parts (e.g. ./../file-txt)

Returns:

A relative path

Return type:

Path

class qhana_plugin_runner.storage.FileStoreInterface

Bases: object

Base class defining the file store interface.

get_file_url(file_storage_data: str, external: bool = True) str

Get a URL to the stored file.

If external is False the file store implementation may return an internal URL that must work with open_url().

If external is True the URL must be accessible from outside this microservice (e.g. through an API endpoint of this microservice) without authorization for at least 2 hours.

Parameters:
  • file_storage_data (str) – the file metadata as defined by the file store or as stored in file_storage_data

  • external (bool, optional) – if the URL should be accessible from outside (for downloading the file). Defaults to True.

Returns:

the URL to the file

Return type:

str

get_task_file_url(file_info: TaskFile, external: bool = True) str

Get an URL for a TaskFile object.

See get_file_url()

Parameters:

file_info (TaskFile) – the information of the task file

Returns:

the URL to the file

Return type:

str

open(file_info: TaskFile) ContextManager[BinaryIO]

Returns a context manager for a file-like object for reading the contents of a task file. :param file_info: the information of the task file :return: context manager for a file-like object

persist_file(file_: IO | str | bytes, target: str | Path, mimetype: str)

Persist a file to the file storage.

Parameters:
  • file (Union[IO, str, bytes]) – the file like object to perist to the storage.

  • target (Union[str, Path]) – the file path to save the file on the storage including the file name.

  • mimetype (str) – the content type of the data.

persist_task_result(task_db_id: int, file_: IO | str | bytes, file_name: str, file_type: str, mimetype: str, commit: bool = True) TaskFile

Perist a task result file and store the file information in the database.

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (Path) – the file name of the result file

  • file_type (str) – the file type tag

  • mimetype (str) – the mime type of the file (not optional for result files!)

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Raises:

KeyError – if the task could not be found in the database

Returns:

the file information stored in the database

Return type:

TaskFile

persist_task_temp_file(task_db_id: int, file_: IO | str | bytes, file_name: str, mimetype: str | None = None, commit: bool = True) TaskFile

Perist a temporary task file and store the file information in the database.

Temporary files should only be used during the execution of the task. All temporary files have the file type tag "temp-file".

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (str) – the file name of the result file

  • mimetype (Optional[str]) – the mime type of the file. Defaults to None.

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Returns:

the file information stored in the database

Return type:

TaskFile

class qhana_plugin_runner.storage.FileStoreRegistry(app: Flask | None = None)

Bases: FileStoreInterface

Class acting as a registry for loaded file stores. Forwards calls to the default file store.

get_file_url(file_storage_data: str, external: bool = True, storage_provider: str | None = None) str

Get a URL to the stored file.

If external is False the file store implementation may return an internal URL that must work with open_url().

If external is True the URL must be accessible from outside this microservice (e.g. through an API endpoint of this microservice) without authorization for at least 2 hours.

Parameters:
  • file_storage_data (str) – the file metadata as defined by the file store or as stored in file_storage_data

  • external (bool, optional) – if the URL should be accessible from outside (for downloading the file). Defaults to True.

Returns:

the URL to the file

Return type:

str

get_task_file_url(file_info: TaskFile, external: bool = True) str

Get an URL for a TaskFile object.

See get_file_url()

Parameters:

file_info (TaskFile) – the information of the task file

Returns:

the URL to the file

Return type:

str

init_app(app: Flask)

Init the file store with the Flask app to get access to the flask config.

open(file_info: TaskFile) ContextManager[BinaryIO]

Returns a context manager for a file-like object for reading the contents of a task file. :param file_info: the information of the task file :return: context manager for a file-like object

persist_file(file_: IO | str | bytes, target: str | Path, mimetype: str = 'application/octet-stream', storage_provider: str | None = None)

Persist a file to the file storage.

Parameters:
  • file (Union[IO, str, bytes]) – the file like object to perist to the storage.

  • target (Union[str, Path]) – the file path to save the file on the storage including the file name.

  • mimetype (str) – the content type of the data.

persist_task_result(task_db_id: int, file_: IO | str | bytes, file_name: str, file_type: str, mimetype: str, commit: bool = True, storage_provider: str | None = None) TaskFile

Perist a task result file and store the file information in the database.

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (Path) – the file name of the result file

  • file_type (str) – the file type tag

  • mimetype (str) – the mime type of the file (not optional for result files!)

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Raises:

KeyError – if the task could not be found in the database

Returns:

the file information stored in the database

Return type:

TaskFile

persist_task_temp_file(task_db_id: int, file_: IO | str | bytes, file_name: str, mimetype: str | None = None, commit: bool = True, storage_provider: str | None = None) TaskFile

Perist a temporary task file and store the file information in the database.

Temporary files should only be used during the execution of the task. All temporary files have the file type tag "temp-file".

Parameters:
  • task_db_id (int) – the id of the task in the database

  • file (Union[IO, str, bytes]) – the file object to persist

  • file_name (str) – the file name of the result file

  • mimetype (Optional[str]) – the mime type of the file. Defaults to None.

  • commit (bool) – if true commits the current DB transaction. Defaults to True.

Returns:

the file information stored in the database

Return type:

TaskFile

class qhana_plugin_runner.storage.LocalFileStore(app: Flask)

Bases: FileStore

A file store implementation using the local file system.

get_file_url(file_storage_data: str, external: bool = True) str

Get a URL to the stored file.

If external is False the file store implementation may return an internal URL that must work with open_url().

If external is True the URL must be accessible from outside this microservice (e.g. through an API endpoint of this microservice) without authorization for at least 2 hours.

Parameters:
  • file_storage_data (str) – the file metadata as defined by the file store or as stored in file_storage_data

  • external (bool, optional) – if the URL should be accessible from outside (for downloading the file). Defaults to True.

Returns:

the URL to the file

Return type:

str

get_task_file_url(file_info: TaskFile, external: bool = True) str

Get an URL for a TaskFile object.

See get_file_url()

Parameters:

file_info (TaskFile) – the information of the task file

Returns:

the URL to the file

Return type:

str

name: ClassVar[str] = 'local_filesystem'
open(file_info: TaskFile) ContextManager[BinaryIO]

Returns a context manager for a file-like object for reading the contents of a task file. :param file_info: the information of the task file :return: context manager for a file-like object

persist_file(file_: IO | str | bytes, target: str | Path, mimetype: str)

Persist a file to the file storage.

Parameters:
  • file (Union[IO, str, bytes]) – the file like object to perist to the storage.

  • target (Union[str, Path]) – the file path to save the file on the storage including the file name.

  • mimetype (str) – the content type of the data.

persist_raw_data(data: str | bytes, target: str | Path, mimetype: str)
class qhana_plugin_runner.storage.UrlFileStore(app: Flask)

Bases: FileStore

A file store implementation using url references.

get_file_url(file_storage_data: str, external: bool = True) str

Get a URL to the stored file.

If external is False the file store implementation may return an internal URL that must work with open_url().

If external is True the URL must be accessible from outside this microservice (e.g. through an API endpoint of this microservice) without authorization for at least 2 hours.

Parameters:
  • file_storage_data (str) – the file metadata as defined by the file store or as stored in file_storage_data

  • external (bool, optional) – if the URL should be accessible from outside (for downloading the file). Defaults to True.

Returns:

the URL to the file

Return type:

str

name: ClassVar[str] = 'url_file_store'
persist_file(file_: IO | str | bytes, target: str | Path, mimetype: str)

Persist a file to the file storage.

Parameters:
  • file (Union[IO, str, bytes]) – the file like object to perist to the storage.

  • target (Union[str, Path]) – the file path to save the file on the storage including the file name.

  • mimetype (str) – the content type of the data.

qhana_plugin_runner.storage.register_file_store(app: Flask)

Register the file store instance with the flask app.