import argparse
import sys
from pathlib import Path
import yaml

from invenio_app.factory import create_api
from invenio_access.permissions import system_identity

from {{ python.proxies_current_service|package_name }} import {{ python.proxies_current_service|base_name }}



def cli():
    parser = argparse.ArgumentParser(description='Imports data from yaml file')
    parser.add_argument('filename', help='Filename to import the data from', nargs='?')

    args = parser.parse_args()

    if args.filename:
        filename = args.filename
    else:
        filename = Path(sys.argv[0]).parent / 'sample_data.yaml'

    api = create_api()

    with api.app_context():
        with open(filename) as f:
            for doc in yaml.load_all(f, yaml.SafeLoader):
                {{ python.proxies_current_service|base_name }}.create(system_identity, doc)


if __name__ == "__main__":
    cli()
