<commit_before>import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckanext.mapactionimporter.logic.action.create

from collections import OrderedDict
from .lib.mappackage import PRODUCT_THEMES

def register_translator():
    # https://github.com/ckan/ckanext-archiver/blob/master/ckanext/archiver/bin/common.py
    # If not set (in cli access), patch the a translator with a mock, so the
    # _() functions in logic layer don't cause failure.
    from paste.registry import Registry
    from pylons import translator
    from ckan.lib.cli import MockTranslator
    if 'registery' not in globals():
        global registry
        registry = Registry()
        registry.prepare()

    if 'translator_obj' not in globals():
        global translator_obj
        translator_obj = MockTranslator()
        registry.register(translator, translator_obj)

def create_product_themes():
    register_translator()

    user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
    context = {'user': user['name']}
    try:
        data = {'id': 'product_themes'}
        vocab = toolkit.get_action('vocabulary_show')(context, data)
        tags = toolkit.get_action('tag_list')(context, {'vocabulary_id': vocab['id']})
        for tag in PRODUCT_THEMES:
            if tag in tags:
                tags.remove(tag)
            else:
                data = {'name': tag, 'vocabulary_id': vocab['id'], 'display_name': "Foo"}
                toolkit.get_action('tag_create')(context, data)
        for tag in tags:
            data = {'name': tag, 'vocabulary_id': vocab['id']}
            toolkit.get_action('tag_delete')(context, data)
    except toolkit.ObjectNotFound:
        data = {'name': 'product_themes'}
        vocab = toolkit.get_action('vocabulary_create')(context, data)
        for tag in PRODUCT_THEMES:
            data = {'name': tag, 'vocabulary_id': vocab['id']}
            toolkit.get_action('tag_create')(context, data)


def product_themes(query=None):
    try:
        tag_list = toolkit.get_action('tag_list')
        product_themes = tag_list(data_dict={'vocabulary_id': 'product_themes', 'all_fields': True, 'query': query})
        return product_themes
    except toolkit.ObjectNotFound:
        return []


class MapactionimporterPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IDatasetForm)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IFacets, inherit=True)
    plugins.implements(plugins.ITemplateHelpers)

    # IFacets
    def dataset_facets(self, facets_dict, package_type):
        #insert the Theme facet after Groups if it's there.
        facets = facets_dict.items()
        keys = facets_dict.keys()
        facets_dict.clear()

        position = 0 #default to the start.

        if 'groups' in keys:
            position = keys.index('groups') + 1
        
        facets.insert(position, ('vocab_product_themes', plugins.toolkit._('Themes')))

        for item in facets:
            facets_dict[item[0]] = item[1]

        return facets_dict


    def group_facets(self, facets_dict, group_type, package_type):
        #insert the Theme facet at the beginning
        facets = facets_dict.items()
        facets_dict.clear()

        facets_dict['vocab_product_themes'] = plugins.toolkit._('Themes')
        for item in facets:
            facets_dict[item[0]] = item[1]

        return facets_dict


    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'mapactionimporter')

    def before_map(self, map_):
        map_.connect(
            'import_mapactionzip',
            '/import_mapactionzip',
            controller='ckanext.mapactionimporter.controllers.zipimport:ZipImportController',
            action='new',
            conditions=dict(method=['GET']),
        )
        map_.connect(
            'import_mapactionzip',
            '/import_mapactionzip',
            controller='ckanext.mapactionimporter.controllers.zipimport:ZipImportController',
            action='import_dataset',
            conditions=dict(method=['POST']),
        )

        return map_

    def get_actions(self):
        return {
            'create_dataset_from_mapaction_zip':
            ckanext.mapactionimporter.logic.action.create.create_dataset_from_zip,
        }

    def get_helpers(self):
        return {'product_themes': product_themes}

    def _modify_package_schema(self, schema):
        schema.update({
            'product_themes': [<commit_msg>Fixed duplicate route name<commit_after>import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckanext.mapactionimporter.logic.action.create

from collections import OrderedDict
from .lib.mappackage import PRODUCT_THEMES

def register_translator():
    # https://github.com/ckan/ckanext-archiver/blob/master/ckanext/archiver/bin/common.py
    # If not set (in cli access), patch the a translator with a mock, so the
    # _() functions in logic layer don't cause failure.
    from paste.registry import Registry
    from pylons import translator
    from ckan.lib.cli import MockTranslator
    if 'registery' not in globals():
        global registry
        registry = Registry()
        registry.prepare()

    if 'translator_obj' not in globals():
        global translator_obj
        translator_obj = MockTranslator()
        registry.register(translator, translator_obj)

def create_product_themes():
    register_translator()

    user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
    context = {'user': user['name']}
    try:
        data = {'id': 'product_themes'}
        vocab = toolkit.get_action('vocabulary_show')(context, data)
        tags = toolkit.get_action('tag_list')(context, {'vocabulary_id': vocab['id']})
        for tag in PRODUCT_THEMES:
            if tag in tags:
                tags.remove(tag)
            else:
                data = {'name': tag, 'vocabulary_id': vocab['id'], 'display_name': "Foo"}
                toolkit.get_action('tag_create')(context, data)
        for tag in tags:
            data = {'name': tag, 'vocabulary_id': vocab['id']}
            toolkit.get_action('tag_delete')(context, data)
    except toolkit.ObjectNotFound:
        data = {'name': 'product_themes'}
        vocab = toolkit.get_action('vocabulary_create')(context, data)
        for tag in PRODUCT_THEMES:
            data = {'name': tag, 'vocabulary_id': vocab['id']}
            toolkit.get_action('tag_create')(context, data)


def product_themes(query=None):
    try:
        tag_list = toolkit.get_action('tag_list')
        product_themes = tag_list(data_dict={'vocabulary_id': 'product_themes', 'all_fields': True, 'query': query})
        return product_themes
    except toolkit.ObjectNotFound:
        return []


class MapactionimporterPlugin(plugins.SingletonPlugin, toolkit.DefaultDatasetForm):
    plugins.implements(plugins.IDatasetForm)
    plugins.implements(plugins.IActions)
    plugins.implements(plugins.IConfigurer)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.IFacets, inherit=True)
    plugins.implements(plugins.ITemplateHelpers)

    # IFacets
    def dataset_facets(self, facets_dict, package_type):
        #insert the Theme facet after Groups if it's there.
        facets = facets_dict.items()
        keys = facets_dict.keys()
        facets_dict.clear()

        position = 0 #default to the start.

        if 'groups' in keys:
            position = keys.index('groups') + 1

        facets.insert(position, ('vocab_product_themes', plugins.toolkit._('Themes')))

        for item in facets:
            facets_dict[item[0]] = item[1]

        return facets_dict


    def group_facets(self, facets_dict, group_type, package_type):
        #insert the Theme facet at the beginning
        facets = facets_dict.items()
        facets_dict.clear()

        facets_dict['vocab_product_themes'] = plugins.toolkit._('Themes')
        for item in facets:
            facets_dict[item[0]] = item[1]

        return facets_dict


    def update_config(self, config_):
        toolkit.add_template_directory(config_, 'templates')
        toolkit.add_public_directory(config_, 'public')
        toolkit.add_resource('fanstatic', 'mapactionimporter')

    def before_map(self, map_):
        map_.connect(
            'import_mapactionzip_form',
            '/import_mapactionzip',
            controller='ckanext.mapactionimporter.controllers.zipimport:ZipImportController',
            action='new',
            conditions=dict(method=['GET']),
        )
        map_.connect(
            'import_mapactionzip',
            '/import_mapactionzip',
            controller='ckanext.mapactionimporter.controllers.zipimport:ZipImportController',
            action='import_dataset',
            conditions=dict(method=['POST']),
        )

        return map_

    def get_actions(self):
        return {
            'create_dataset_from_mapaction_zip':
            ckanext.mapactionimporter.logic.action.create.create_dataset_from_zip,
        }

    def get_helpers(self):
        return {'product_themes': product_themes}

    def _modify_package_schema(self, schema):
        schema.update({
            'product_themes': [
{'fraction_non_alphanumeric': 0.10783487784330244, 'fraction_numerical': 0.0012636899747262005, 'mean_word_length': 2.889025389025389, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4502816', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2770, 'n_words': 567}