# Path: ckanext/dcatapit/validators.py
# DEFAULT_LANG = config.get('ckan.locale_default', 'en')
# DATE_FORMATS = ['%Y-%m-%d',
#                 '%d-%m-%Y',
#                 '%Y%m%d',
#                 '%Y-%m-%dT%H:%M:%S',
#                 '%Y-%m-%dT%H:%M:%S %z',
#                 '%Y-%m-%dT%H:%M:%S %Z',
#                 '%Y-%m-%d %H:%M:%S',
#                 '%Y-%m-%d %H:%M',
#                 # faulty ones, but still can be used
#                 '%Y',
#                 'N/A%Y',
#                 'N/A %Y',
#                 '%b. %Y',
#                 '%b.%Y',
#                 '%B %Y',
#                 '%m/%Y',
#                 '%d.%m.%Y',
#                 '%d/%m/%Y',
#                 # american notation is disabled, it can be bogus with one above
#                 # '%m/%d/%Y',
#                 # some timestamps have invalid tz offset (without a sign)
#                 '%Y-%m-%dT%H:%M:%S 0100',
#                 '%Y-%m-%dT%H:%M:%S 01:00',
#                 ]
# def is_blank(string):
# def couple_validator(value, context):
# def no_number(value, context):
# def dcatapit_id_unique(value, context):
# def _populate_multilang_dict(prop_val):
# def dcatapit_conforms_to(value, context):
# def dcatapit_alternate_identifier(value, context):
# def dcatapit_creator(value, context):
# def parse_date(val, default=None):
# def serialize_date(val):
# def parse_nullable_date(val):
# def dcatapit_temporal_coverage(value, context):
# def dcatapit_copy_to_context(key, flattened_data, errors, context):
# def dcatapit_remove_theme(key, flattened_data, errors, context):
# def dcatapit_subthemes(key, flattened_data, errors, context):
#     def _get_flattened_theme():
#     def _do_return(value):


import json
import logging
import uuid
import ckan.plugins.toolkit as toolkit
import ckanext.dcatapit.interfaces as interfaces
from datetime import datetime
from sqlalchemy import and_
from ckan.lib.base import config
from ckan.lib.navl.dictization_functions import Invalid
from ckan.logic import ValidationError
from ckan.logic.validators import tag_name_validator
from ckan.model import (
    Group,
    GroupExtra,
    Package,
    PackageExtra,
    repo,
)
from ckan.model.meta import Session
from ckanext.multilang.model import PackageMultilang as ML_PM
from ckanext.dcatapit import validators
    from ckanext.dcatapit.plugin import DCATAPITPackagePlugin
        log.info(
            f"dataset {pdata['name']}: no frequency. Using default, UNKNOWN."
        )
        frequency = 'UNKNOWN'
    pdata['frequency'] = frequency


def update_identifier(pdata):
    identifier = pdata.pop('identifier', None)
    if not identifier:
        to_delete = []
        for idx, ex in enumerate(pdata.get('extras') or []):
            if ex['key'] == 'identifier':
                to_delete.append(idx)
                identifier = ex['value']
        if to_delete:
            for idx in reversed(to_delete):
                pdata['extras'].pop(idx)

    # default theme if nothing available
    if not identifier:
        log.warning(
            f"dataset {pdata['name']}: no identifier. generating new one"
        )
        identifier = str(uuid.uuid4())
    pdata['identifier'] = identifier


def update_modified(pdata):
    try:

# Path: ckanext/dcatapit/validators.py
# DEFAULT_LANG = config.get('ckan.locale_default', 'en')
# DATE_FORMATS = ['%Y-%m-%d',
#                 '%d-%m-%Y',
#                 '%Y%m%d',
#                 '%Y-%m-%dT%H:%M:%S',
#                 '%Y-%m-%dT%H:%M:%S %z',
#                 '%Y-%m-%dT%H:%M:%S %Z',
#                 '%Y-%m-%d %H:%M:%S',
#                 '%Y-%m-%d %H:%M',
#                 # faulty ones, but still can be used
#                 '%Y',
#                 'N/A%Y',
#                 'N/A %Y',
#                 '%b. %Y',
#                 '%b.%Y',
#                 '%B %Y',
#                 '%m/%Y',
#                 '%d.%m.%Y',
#                 '%d/%m/%Y',
#                 # american notation is disabled, it can be bogus with one above
#                 # '%m/%d/%Y',
#                 # some timestamps have invalid tz offset (without a sign)
#                 '%Y-%m-%dT%H:%M:%S 0100',
#                 '%Y-%m-%dT%H:%M:%S 01:00',
#                 ]
# def is_blank(string):
# def couple_validator(value, context):
# def no_number(value, context):
# def dcatapit_id_unique(value, context):
# def _populate_multilang_dict(prop_val):
# def dcatapit_conforms_to(value, context):
# def dcatapit_alternate_identifier(value, context):
# def dcatapit_creator(value, context):
# def parse_date(val, default=None):
# def serialize_date(val):
# def parse_nullable_date(val):
# def dcatapit_temporal_coverage(value, context):
# def dcatapit_copy_to_context(key, flattened_data, errors, context):
# def dcatapit_remove_theme(key, flattened_data, errors, context):
# def dcatapit_subthemes(key, flattened_data, errors, context):
#     def _get_flattened_theme():
#     def _do_return(value):


# Path: ckanext/dcatapit/commands/migrate.py
import json
import logging
import uuid
import ckan.plugins.toolkit as toolkit
import ckanext.dcatapit.interfaces as interfaces
from datetime import datetime
from sqlalchemy import and_
from ckan.lib.base import config
from ckan.lib.navl.dictization_functions import Invalid
from ckan.logic import ValidationError
from ckan.logic.validators import tag_name_validator
from ckan.model import (
    Group,
    GroupExtra,
    Package,
    PackageExtra,
    repo,
)
from ckan.model.meta import Session
from ckanext.multilang.model import PackageMultilang as ML_PM
from ckanext.dcatapit import validators
    from ckanext.dcatapit.plugin import DCATAPITPackagePlugin

        log.info(
            f"dataset {pdata['name']}: no frequency. Using default, UNKNOWN."
        )
        frequency = 'UNKNOWN'
    pdata['frequency'] = frequency


def update_identifier(pdata):
    identifier = pdata.pop('identifier', None)
    if not identifier:
        to_delete = []
        for idx, ex in enumerate(pdata.get('extras') or []):
            if ex['key'] == 'identifier':
                to_delete.append(idx)
                identifier = ex['value']
        if to_delete:
            for idx in reversed(to_delete):
                pdata['extras'].pop(idx)

    # default theme if nothing available
    if not identifier:
        log.warning(
            f"dataset {pdata['name']}: no identifier. generating new one"
        )
        identifier = str(uuid.uuid4())
    pdata['identifier'] = identifier


def update_modified(pdata):
    try:

        data = validators.parse_date(pdata['modified'])