# Path: ckanext/dcatapit/harvesters/utils.py
# def map_ckan_license(harvest_object=None, pkg_dict=None):
#     """
#     license in resources' extra:
#         if it exists, perform simple validation. If not valid, replace with the unknown license type
#         if it does not exist, try to map the dataset's license to a license in the controlled voc
#         fallback to the unknown license type
#     :param harvest_object:
#     :param pkg_dict:
#     :type harvest_object: HarvestObject model
#     :type pkg_dict: dict dictized dataset
# 
#     :return: This will return dataset's dict with modified licenses
#     :rtype: dict with dictized dataset
#     """
#     if not (harvest_object or pkg_dict) or (harvest_object and pkg_dict):
#         raise ValueError('You should provide either harvest_object or pkg_dict')
# 
#     if harvest_object:
#         data = json.loads(harvest_object.content)
#     else:
#         data = pkg_dict
# 
#     dataset_license = get_license_from_package(data)
# 
#     for res in data.get('resources') or []:
#         if res.get('license_type'):
#             l, _ = License.find_by_token(res['license_type'])
#             res['license_type'] = l.uri
#         else:
#             res['license_type'] = dataset_license.uri
#     return data
# 
# Path: ckanext/dcatapit/mapping.py
# def map_nonconformant_groups(harvest_object):
#     """
#     Adds themes to fetched data
#     """
#     themes_data = _load_mapping_data()
#     if not themes_data:
#         return
# 
#     data = json.loads(harvest_object.content)
#     _groups = data.get('groups')
#     if not _groups:
#         return
# 
#     groups = [g['name'] for g in _groups]
#     groups.extend([g['display_name'] for g in _groups if 'display_name' in g])
# 
#     new_themes = _get_new_themes(groups, themes_data, add_existing=False)
#     if not new_themes:
#         return
# 
#     # ensure themes are upper-case, otherwise will be discarded by validators
# 
# 
#     extra = data.get('extras') or []
# 
#     aggr = themes_to_aggr_json(new_themes)
#     _set_extra(extra, FIELD_THEMES_AGGREGATE, aggr)
#     data[FIELD_THEMES_AGGREGATE] = aggr
# 
#     theme_list = json.dumps([theme_name_to_uri(name) for name in new_themes])
#     _set_extra(extra, 'theme', theme_list)
#     data['theme'] = theme_list
# 
#     data['extras'] = extra
# 
#     harvest_object.content = json.dumps(data)
#     Session.add(harvest_object)
#     Session.flush()


import json
from ckanext.dcatapit.harvesters.utils import map_ckan_license
from ckanext.dcatapit.mapping import map_nonconformant_groups
from ckanext.harvest.harvesters.ckanharvester import CKANHarvester
#!/usr/bin/env python3




class CKANMappingHarvester(CKANHarvester):

    def info(self):
        return {
            'name': 'CKAN-DCATAPIT',
            'title': 'CKAN harvester for DCATAPIT',
            'description': 'Special version of CKANHarvester, which will map groups to themes',
            'form_config_interface': 'Text'
        }

    def import_stage(self, harvest_object):
        map_nonconformant_groups(harvest_object)

# Path: ckanext/dcatapit/harvesters/utils.py
# def map_ckan_license(harvest_object=None, pkg_dict=None):
#     """
#     license in resources' extra:
#         if it exists, perform simple validation. If not valid, replace with the unknown license type
#         if it does not exist, try to map the dataset's license to a license in the controlled voc
#         fallback to the unknown license type
#     :param harvest_object:
#     :param pkg_dict:
#     :type harvest_object: HarvestObject model
#     :type pkg_dict: dict dictized dataset
# 
#     :return: This will return dataset's dict with modified licenses
#     :rtype: dict with dictized dataset
#     """
#     if not (harvest_object or pkg_dict) or (harvest_object and pkg_dict):
#         raise ValueError('You should provide either harvest_object or pkg_dict')
# 
#     if harvest_object:
#         data = json.loads(harvest_object.content)
#     else:
#         data = pkg_dict
# 
#     dataset_license = get_license_from_package(data)
# 
#     for res in data.get('resources') or []:
#         if res.get('license_type'):
#             l, _ = License.find_by_token(res['license_type'])
#             res['license_type'] = l.uri
#         else:
#             res['license_type'] = dataset_license.uri
#     return data
# 
# Path: ckanext/dcatapit/mapping.py
# def map_nonconformant_groups(harvest_object):
#     """
#     Adds themes to fetched data
#     """
#     themes_data = _load_mapping_data()
#     if not themes_data:
#         return
# 
#     data = json.loads(harvest_object.content)
#     _groups = data.get('groups')
#     if not _groups:
#         return
# 
#     groups = [g['name'] for g in _groups]
#     groups.extend([g['display_name'] for g in _groups if 'display_name' in g])
# 
#     new_themes = _get_new_themes(groups, themes_data, add_existing=False)
#     if not new_themes:
#         return
# 
#     # ensure themes are upper-case, otherwise will be discarded by validators
# 
# 
#     extra = data.get('extras') or []
# 
#     aggr = themes_to_aggr_json(new_themes)
#     _set_extra(extra, FIELD_THEMES_AGGREGATE, aggr)
#     data[FIELD_THEMES_AGGREGATE] = aggr
# 
#     theme_list = json.dumps([theme_name_to_uri(name) for name in new_themes])
#     _set_extra(extra, 'theme', theme_list)
#     data['theme'] = theme_list
# 
#     data['extras'] = extra
# 
#     harvest_object.content = json.dumps(data)
#     Session.add(harvest_object)
#     Session.flush()


# Path: ckanext/dcatapit/harvesters/ckanharvester.py
import json
from ckanext.dcatapit.harvesters.utils import map_ckan_license
from ckanext.dcatapit.mapping import map_nonconformant_groups
from ckanext.harvest.harvesters.ckanharvester import CKANHarvester

#!/usr/bin/env python3




class CKANMappingHarvester(CKANHarvester):

    def info(self):
        return {
            'name': 'CKAN-DCATAPIT',
            'title': 'CKAN harvester for DCATAPIT',
            'description': 'Special version of CKANHarvester, which will map groups to themes',
            'form_config_interface': 'Text'
        }

    def import_stage(self, harvest_object):
        map_nonconformant_groups(harvest_object)

        data = map_ckan_license(harvest_object=harvest_object)