#!/usr/bin/env python3
# Author(s): Toni Sissala
# Copyright 2020 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
import os
from kuha_common import cli_setup
from kuha_common.document_store.records import (
COLLECTIONS,
record_by_collection
)
from kuha_client.kuha_client import BatchProcessor
[docs]def cli():
"""Parse command line arguments and call :meth:`BatchProcessor.remove_run`
:returns: 0 on success.
:rtype: int
"""
cli_setup.load(os.path.dirname(os.path.realpath(__file__)),
prog='python -m kuha_client.kuha_delete',
description='Delete records from Document Store.',
config_file='kuha_client.ini')
cli_setup.add_document_store_url(cli_setup.settings.parser, required=True)
cli_setup.add('collection', choices=COLLECTIONS + ['ALL'],
help="Select collection which records shall be deleted. WARNING: selecting ALL will "
"delete all records from all collections regardless of conditions.")
cli_setup.add('conditions', nargs='+')
settings = cli_setup.setup(
cli_setup.MOD_DS_CLIENT,
cli_setup.MOD_LOGGING
)
cli_setup.settings.setup_document_store_query()
if settings.collection == 'ALL':
BatchProcessor().remove_run()
return 0
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()
processor.remove_run(record)
return 0
if __name__ == '__main__':
sys.exit(cli())