# Path: ckanext/dcatapit/dcat/const.py
# THEME_BASE_URI = 'http://publications.europa.eu/resource/authority/data-theme/'
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


import json
import logging
import os
from configparser import SafeConfigParser as ConfigParser
from ckan.lib.base import config
from ckan.model import Session
from ckan.model.group import Group, Member
from ckan.plugins import toolkit
from ckanext.dcatapit.dcat.const import THEME_BASE_URI
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE


def _get_group_from_session(gname):
    """
    If Group was created within current session, get
    it from cache instead of db.

    This exists because new, uncommited/unflushed objects are
    not accessible by Session.query.
    """
    for obj in Session.new:
        if isinstance(obj, Group):
            if obj.name == gname:
                return obj


def populate_theme_groups(instance, clean_existing=False):
    """
    For given instance, it finds groups from mapping corresponding to
    Dataset's themes, and will assign dataset to those groups.

    Existing groups will be removed, if clean_existing is set to True.

    This utilizes `ckanext.dcatapit.theme_group_mapping.add_new_groups`
    configuration option. If it's set to true, and mapped group doesn't exist,
    new group will be created.
    """
    add_new = toolkit.asbool(config.get(DCATAPIT_THEME_TO_MAPPING_ADD_NEW_GROUPS))
    themes = []
    for ex in (instance.get('extras') or []):

# Path: ckanext/dcatapit/dcat/const.py
# THEME_BASE_URI = 'http://publications.europa.eu/resource/authority/data-theme/'
# 
# Path: ckanext/dcatapit/schema.py
# FIELD_THEMES_AGGREGATE = 'themes_aggregate'


# Path: ckanext/dcatapit/mapping.py
import json
import logging
import os
from configparser import SafeConfigParser as ConfigParser
from ckan.lib.base import config
from ckan.model import Session
from ckan.model.group import Group, Member
from ckan.plugins import toolkit
from ckanext.dcatapit.dcat.const import THEME_BASE_URI
from ckanext.dcatapit.schema import FIELD_THEMES_AGGREGATE



def _get_group_from_session(gname):
    """
    If Group was created within current session, get
    it from cache instead of db.

    This exists because new, uncommited/unflushed objects are
    not accessible by Session.query.
    """
    for obj in Session.new:
        if isinstance(obj, Group):
            if obj.name == gname:
                return obj


def populate_theme_groups(instance, clean_existing=False):
    """
    For given instance, it finds groups from mapping corresponding to
    Dataset's themes, and will assign dataset to those groups.

    Existing groups will be removed, if clean_existing is set to True.

    This utilizes `ckanext.dcatapit.theme_group_mapping.add_new_groups`
    configuration option. If it's set to true, and mapped group doesn't exist,
    new group will be created.
    """
    add_new = toolkit.asbool(config.get(DCATAPIT_THEME_TO_MAPPING_ADD_NEW_GROUPS))
    themes = []
    for ex in (instance.get('extras') or []):

        if ex['key'] == FIELD_THEMES_AGGREGATE: