# Path: ckanext/dcatapit/commands/migrate.py
# def do_migrate_data(limit=None, offset=None, skip_orgs=False):
#     from ckanext.dcatapit.plugin import DCATAPITPackagePlugin
# 
#     user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
#     context = {'user': user['name'],
#                'ignore_auth': True,
#                'use_cache': False}
#     pshow = toolkit.get_action('package_show')
#     pupdate = toolkit.get_action('package_update')
#     pcreate = toolkit.get_action('package_create')
#     oshow = toolkit.get_action('organization_show')
#     oupdate = toolkit.get_action('organization_patch')
#     pupdate_schema = DCATAPITPackagePlugin().update_package_schema()
#     pupdate_schema['tags']['name'].remove(tag_name_validator)
#     org_list = get_organization_list()
#     ocount = org_list.count()
#     oidx = 0
#     if not skip_orgs:
#         log.info(f'processing {ocount} organizations')
#         for oidx, oname in enumerate(org_list):
#             odata = oshow(context, {'id': oname, 'include_extras': True,
#                                     'include_tags': False,
#                                     'include_users': False,
#                                     })
# 
#             oidentifier = odata.get('identifier')
#             log.info('processing {}/{} organization: {}'.format(oidx + 1, ocount, odata['name']))
#             # we require identifier for org now.
#             if not oidentifier:
#                 odata.pop('identifier', None)
#                 tmp_identifier = get_temp_org_identifier()
#                 log.info(
#                     f"org: [{odata['name']}] {odata['title']}: "
#                     f'setting temporal identifier: {tmp_identifier}'
#                 )
#                 ocontext = context.copy()
# 
#                 ocontext['allow_partial_update'] = True
#                 # oupdate(ocontext, {'id': odata['id'],
#                 #                  'identifier': tmp_identifier})
#                 update_organization_identifier(odata['id'], tmp_identifier)
#     else:
#         log.info(u'Skipping organizations processing')
#     pcontext = context.copy()
#     pkg_list = get_package_list()
#     pcount = pkg_list.count()
#     log.info(f'processing {pcount} packages')
#     errored = []
# 
#     if offset:
#         pkg_list = pkg_list.offset(offset)
#     if limit:
#         pkg_list = pkg_list.limit(limit)
# 
#     # pidx may be not initialized for empty slice, need separate counter
#     # to count actually processed datasets
#     pidx_count = 0
#     for pidx, pname in enumerate(pkg_list):
#         pcontext['schema'] = pupdate_schema
#         pname = pname[0]
#         log.info(f'processing {pidx + 1}/{pcount} package: {pname}')
#         pdata = pshow(context, {'name_or_id': pname})  # , 'use_default_schema': True})
# 
#         # remove empty conforms_to to avoid silly validation errors
#         if not pdata.get('conforms_to'):
#             pdata.pop('conforms_to', None)
#         # ... the same for alternate_identifier
#         if not pdata.get('alternate_identifier'):
#             pdata.pop('alternate_identifier', None)
# 
#         update_creator(pdata)
#         update_temporal_coverage(pdata)
#         update_theme(pdata)
#         update_identifier(pdata)
#         update_modified(pdata)
#         update_frequency(pdata)
#         update_conforms_to(pdata)
#         update_holder_info(pdata)
#         interfaces.populate_resource_license(pdata)
#         pdata['metadata_modified'] = None
#         log.info(f"updating {pdata['id']} {pdata['name']}")
#         try:
#             out = pupdate(pcontext, pdata)
#             pidx_count += 1
#         except ValidationError as err:
#             log.error(
#                 f"Cannot update due to validation error {pdata['name']}",
#                 exc_info=True
#             )
#             errored.append((pidx, pdata['name'], err,))
#             continue
# 
#         except Exception as err:
#             log.error(
#                 f"Cannot update due to general error {pdata['name']}",
#                 exc_info=True
#             )
#             errored.append((pidx, pdata['name'], err,))
#             continue
#         log.debug('-' * 9)
# 
#     if not skip_orgs:
#         log.info(f'processed {oidx} out of {ocount} organizations')
#     log.info(f'processed {pidx_count} out of {pcount} packages in total')
#     if errored:
#         log.info(f'Following {len(errored)} datasets failed:')
#         for position, ptitile, err in errored:
#             err_summary = getattr(err, 'error', None) or err
# 
#             # this is a hack on dumb override in __str__() in some exception subclasses
#             # stringified exception raises itself otherwise.
#             try:
#                 log.info(
#                     f' {ptitile} at position {position}: {err.__class__}{err_summary}'
#                 )
#             except Exception as err:
#                 err_summary = err
#                 log.error(
#                     f' {ptitile} at position {position}: {err.__class__}{err_summary}'
#                 )
# 
# Path: ckanext/dcatapit/commands/vocabulary.py
# def load_from_file(filename=None, url=None, eurovoc=None, *args, **kwargs):
#     try:
#         g, name, uri, eurovoc = validate_vocabulary(filename, url, eurovoc)
#     except ValueError as e:
#         log.error(f'Error in handling vocabulary: {e}')
#         return -1
# 
#     results = load(g, name, uri, eurovoc)
# 
#     log.info(f'Results: {results}' )


import click
import logging
from ckanext.dcatapit.commands.migrate import do_migrate_data
from ckanext.dcatapit.commands.vocabulary import load_from_file as load_voc
    from ckanext.dcatapit.model import setup_db
#!/usr/bin/env python3


log = logging.getLogger(__name__)


def get_commands():
    return [dcatapit]


@click.group()
def dcatapit():
    # self._load_config()
    pass


@dcatapit.command()

# Path: ckanext/dcatapit/commands/migrate.py
# def do_migrate_data(limit=None, offset=None, skip_orgs=False):
#     from ckanext.dcatapit.plugin import DCATAPITPackagePlugin
# 
#     user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
#     context = {'user': user['name'],
#                'ignore_auth': True,
#                'use_cache': False}
#     pshow = toolkit.get_action('package_show')
#     pupdate = toolkit.get_action('package_update')
#     pcreate = toolkit.get_action('package_create')
#     oshow = toolkit.get_action('organization_show')
#     oupdate = toolkit.get_action('organization_patch')
#     pupdate_schema = DCATAPITPackagePlugin().update_package_schema()
#     pupdate_schema['tags']['name'].remove(tag_name_validator)
#     org_list = get_organization_list()
#     ocount = org_list.count()
#     oidx = 0
#     if not skip_orgs:
#         log.info(f'processing {ocount} organizations')
#         for oidx, oname in enumerate(org_list):
#             odata = oshow(context, {'id': oname, 'include_extras': True,
#                                     'include_tags': False,
#                                     'include_users': False,
#                                     })
# 
#             oidentifier = odata.get('identifier')
#             log.info('processing {}/{} organization: {}'.format(oidx + 1, ocount, odata['name']))
#             # we require identifier for org now.
#             if not oidentifier:
#                 odata.pop('identifier', None)
#                 tmp_identifier = get_temp_org_identifier()
#                 log.info(
#                     f"org: [{odata['name']}] {odata['title']}: "
#                     f'setting temporal identifier: {tmp_identifier}'
#                 )
#                 ocontext = context.copy()
# 
#                 ocontext['allow_partial_update'] = True
#                 # oupdate(ocontext, {'id': odata['id'],
#                 #                  'identifier': tmp_identifier})
#                 update_organization_identifier(odata['id'], tmp_identifier)
#     else:
#         log.info(u'Skipping organizations processing')
#     pcontext = context.copy()
#     pkg_list = get_package_list()
#     pcount = pkg_list.count()
#     log.info(f'processing {pcount} packages')
#     errored = []
# 
#     if offset:
#         pkg_list = pkg_list.offset(offset)
#     if limit:
#         pkg_list = pkg_list.limit(limit)
# 
#     # pidx may be not initialized for empty slice, need separate counter
#     # to count actually processed datasets
#     pidx_count = 0
#     for pidx, pname in enumerate(pkg_list):
#         pcontext['schema'] = pupdate_schema
#         pname = pname[0]
#         log.info(f'processing {pidx + 1}/{pcount} package: {pname}')
#         pdata = pshow(context, {'name_or_id': pname})  # , 'use_default_schema': True})
# 
#         # remove empty conforms_to to avoid silly validation errors
#         if not pdata.get('conforms_to'):
#             pdata.pop('conforms_to', None)
#         # ... the same for alternate_identifier
#         if not pdata.get('alternate_identifier'):
#             pdata.pop('alternate_identifier', None)
# 
#         update_creator(pdata)
#         update_temporal_coverage(pdata)
#         update_theme(pdata)
#         update_identifier(pdata)
#         update_modified(pdata)
#         update_frequency(pdata)
#         update_conforms_to(pdata)
#         update_holder_info(pdata)
#         interfaces.populate_resource_license(pdata)
#         pdata['metadata_modified'] = None
#         log.info(f"updating {pdata['id']} {pdata['name']}")
#         try:
#             out = pupdate(pcontext, pdata)
#             pidx_count += 1
#         except ValidationError as err:
#             log.error(
#                 f"Cannot update due to validation error {pdata['name']}",
#                 exc_info=True
#             )
#             errored.append((pidx, pdata['name'], err,))
#             continue
# 
#         except Exception as err:
#             log.error(
#                 f"Cannot update due to general error {pdata['name']}",
#                 exc_info=True
#             )
#             errored.append((pidx, pdata['name'], err,))
#             continue
#         log.debug('-' * 9)
# 
#     if not skip_orgs:
#         log.info(f'processed {oidx} out of {ocount} organizations')
#     log.info(f'processed {pidx_count} out of {pcount} packages in total')
#     if errored:
#         log.info(f'Following {len(errored)} datasets failed:')
#         for position, ptitile, err in errored:
#             err_summary = getattr(err, 'error', None) or err
# 
#             # this is a hack on dumb override in __str__() in some exception subclasses
#             # stringified exception raises itself otherwise.
#             try:
#                 log.info(
#                     f' {ptitile} at position {position}: {err.__class__}{err_summary}'
#                 )
#             except Exception as err:
#                 err_summary = err
#                 log.error(
#                     f' {ptitile} at position {position}: {err.__class__}{err_summary}'
#                 )
# 
# Path: ckanext/dcatapit/commands/vocabulary.py
# def load_from_file(filename=None, url=None, eurovoc=None, *args, **kwargs):
#     try:
#         g, name, uri, eurovoc = validate_vocabulary(filename, url, eurovoc)
#     except ValueError as e:
#         log.error(f'Error in handling vocabulary: {e}')
#         return -1
# 
#     results = load(g, name, uri, eurovoc)
# 
#     log.info(f'Results: {results}' )


# Path: ckanext/dcatapit/commands/dcatapit.py
import click
import logging
from ckanext.dcatapit.commands.migrate import do_migrate_data
from ckanext.dcatapit.commands.vocabulary import load_from_file as load_voc
    from ckanext.dcatapit.model import setup_db

#!/usr/bin/env python3


log = logging.getLogger(__name__)


def get_commands():
    return [dcatapit]


@click.group()
def dcatapit():
    # self._load_config()
    pass


@dcatapit.command()

def initdb():