#!/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 point to import records from DocumentStore.
Example run from command line. Import records from /some/path::
python -m kuha_client.kuha_import --document-store-url=http://localhost:6001/v0 /some/path
Print help::
python -m kuha_client.kuha_import -h
"""
import sys
import os
from kuha_common import cli_setup
from kuha_common.document_store.records import COLLECTIONS
from kuha_client.kuha_client import BatchProcessor
[docs]def import_run(paths, file_log_path=None, **kwargs):
r"""Import run with arguments.
:param paths: Lookup source files from paths.
:param file_log_path: Path to file log.
:param \*\*kwargs: Additional keyword arguments get passed to BatchProcessor.
:returns: 0 on success.
:rtype: int
"""
if file_log_path:
with BatchProcessor.with_file_log(file_log_path, **kwargs) as processor:
processor.import_run(paths)
return 0
processor = BatchProcessor(**kwargs)
processor.import_run(paths)
return 0
[docs]def cli():
"""Parse command line arguments. Call :func:`import_run`.
:returns: Return value of :func:`import_run`
"""
cli_setup.load(os.path.dirname(os.path.realpath(__file__)),
prog='python -m kuha_client.kuha_import',
description='Submit DDI-C files to document store for import.',
config_file='kuha_client.ini')
cli_setup.add_document_store_url(cli_setup.settings.parser, required=True)
cli_setup.add('--collection', type=str, action='append',
help=('Specific collection to process. If not given, will process all collections. '
'Repeatable argument.'),
choices=COLLECTIONS)
cli_setup.add('--file-log-path', type=str, default=None,
help=('Path where to load and store file timestamps for comparing '
'on subsequent runs. Leave unset to submit all found records.'))
cli_setup.add('--source-file-type', type=str, default=BatchProcessor.SOURCEFILETYPE_DDIC,
choices=BatchProcessor.get_supported_sourcefiletypes(),
help=('Source file types used for import. Note that all files found from paths '
'must have same type.'))
cli_setup.add('paths', nargs='+',
help=('Paths to look for files. May be a directory or a file. '
'Repeatable argument.'))
settings = cli_setup.setup(
cli_setup.MOD_DS_CLIENT,
cli_setup.MOD_LOGGING
)
return import_run(settings.paths,
collections=settings.collection,
file_log_path=settings.file_log_path,
sourcefiletype=settings.source_file_type)
if __name__ == '__main__':
sys.exit(cli())