Source code for kuha_client.kuha_delete

#!/usr/bin/env python3
# Author(s): Toni Sissala
# Copyright 2022 Finnish Social Science Data Archive FSD / University of Tampere
# Licensed under the EUPL. See LICENSE.txt for full license.
"""Callable module serves as entry poin to delete records from DocumentStore.

Example run from command line. Delete study with ID::

    python -m kuha_client.kuha_delete --document-store-url=http://localhost:6001/v0 studies 5afa741d6fb71d7b2d333982

Print help::

    python -m kuha_client.kuha_delete -h

"""
import sys

from kuha_common import cli_setup, conf
from kuha_common.document_store.records import record_by_collection
from kuha_client import BatchProcessor, impl, open_file_logging_cache


def configure():
    conf.load(
        'kuha_client.kuha_delete',
        package='kuha_client',
        env_var_prefix='KUHA_',
        description='Delete records from Document Store.',
    )
    conf.add_config_arg()
    conf.add(
        '--file-cache',
        type=str,
        env_var='FILE_CACHE',
        help='Path to a cache file. Leave unset to not use file caching. '
        'Removing records will invalidate the cache file.',
    )
    conf.add(
        '--delete-type',
        choices=['soft', 'hard'],
        default='soft',
        help="Select delete type. Soft is for logical deletions, hard is for physical deletions.",
        env_var='DELETE_TYPE',
    )
    conf.add(
        'collection',
        choices=list(impl.COLLECTIONS_METHODS.keys()) + ['ALL'],
        help="Select collection which records shall be deleted. WARNING: selecting ALL will "
        "delete all records from all collections regardless of conditions.",
    )
    conf.add('conditions', nargs='+')
    return cli_setup.setup_common_modules(cli_setup.MOD_DS_CLIENT, cli_setup.MOD_LOGGING, cli_setup.MOD_DS_QUERY)


def _call_processor_remove_run(hard_delete, collections_methods=None, file_cache_path=None, record=None):
    collections_methods = collections_methods or impl.collection_methods()
    if file_cache_path:
        with open_file_logging_cache(file_cache_path) as cache:
            proc = BatchProcessor(collections_methods, cache=cache)
            proc.remove_run(rec_or_class=record, hard_delete=hard_delete)
            return
    proc = BatchProcessor(collections_methods)
    proc.remove_run(rec_or_class=record, hard_delete=hard_delete)
    return


[docs] def cli(): """Parse command line arguments and call :meth:`BatchProcessor.remove_run`""" settings = configure() hard_delete = settings.delete_type == 'hard' if settings.collection == 'ALL': _call_processor_remove_run(hard_delete, file_cache_path=settings.file_cache) return record = record_by_collection(settings.collection) cond = {} for condition in settings.conditions: if condition == 'ALL': break if '=' in condition: key, value = condition.split('=') cond.update({key: value}) else: cond.update({'_id': condition}) if cond: record = record(cond) _call_processor_remove_run( hard_delete, collections_methods=impl.collection_methods([settings.collection]), file_cache_path=settings.file_cache, record=record, )
if __name__ == '__main__': sys.exit(cli())