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:
FileStoreInterfaceInterface 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()
- 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_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:
- Raises:
KeyError – if the task could not be found in the database
- Returns:
the file information stored in the database
- Return type:
- 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:
- Returns:
the file information stored in the database
- Return type:
- 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:
objectBase 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
externalisFalsethe file store implementation may return an internal URL that must work withopen_url().If
externalisTruethe 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_dataexternal (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:
- get_task_file_url(file_info: TaskFile, external: bool = True) str
Get an URL for a TaskFile object.
See
get_file_url()
- 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.
- 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:
- Raises:
KeyError – if the task could not be found in the database
- Returns:
the file information stored in the database
- Return type:
- 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:
- Returns:
the file information stored in the database
- Return type:
- class qhana_plugin_runner.storage.FileStoreRegistry(app: Flask | None = None)
Bases:
FileStoreInterfaceClass 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
externalisFalsethe file store implementation may return an internal URL that must work withopen_url().If
externalisTruethe 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_dataexternal (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:
- get_task_file_url(file_info: TaskFile, external: bool = True) str
Get an URL for a TaskFile object.
See
get_file_url()
- 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.
- 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:
- Raises:
KeyError – if the task could not be found in the database
- Returns:
the file information stored in the database
- Return type:
- 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:
- Returns:
the file information stored in the database
- Return type:
- class qhana_plugin_runner.storage.LocalFileStore(app: Flask)
Bases:
FileStoreA 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
externalisFalsethe file store implementation may return an internal URL that must work withopen_url().If
externalisTruethe 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_dataexternal (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:
- get_task_file_url(file_info: TaskFile, external: bool = True) str
Get an URL for a TaskFile object.
See
get_file_url()
- 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
- class qhana_plugin_runner.storage.UrlFileStore(app: Flask)
Bases:
FileStoreA 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
externalisFalsethe file store implementation may return an internal URL that must work withopen_url().If
externalisTruethe 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_dataexternal (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:
- qhana_plugin_runner.storage.register_file_store(app: Flask)
Register the file store instance with the flask app.