#!/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
)
def configure():
conf.load('kuha_client.kuha_delete', package='kuha_client',
env_var_prefix='KUHA_', description='Delete records from Document Store.')
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
)
[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':
BatchProcessor(impl.collection_methods()).remove_run(hard_delete=hard_delete)
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)
processor = BatchProcessor(impl.collection_methods([settings.collection]))
processor.remove_run(rec_or_class=record, hard_delete=hard_delete)
if __name__ == '__main__':
sys.exit(cli())