<commit_before>from contextlib import closing
from operator import contains
from functools import partial

from minerva.error import ConfigurationError
from minerva.util import k, identity
from minerva.directory import EntityType
from minerva.storage import Engine
from minerva.storage.trend import TableTrendStore


class TrendEngine(Engine):
    pass_through = k(identity)

    @staticmethod
    def store_cmd(package):
        """
        Return a function to bind a data source to the store command.

        :param package: A DataPackageBase subclass instance
        :return: function that binds a data source to the store command
        :rtype: (data_source) -> (conn) -> None
        """
        return TrendEngine.make_store_cmd(TrendEngine.pass_through)(package)

    @staticmethod
    def make_store_cmd(transform_package):
        """
        Return a function to bind a data source to the store command.

        :param transform_package: (TableTrendStore) -> (DataPackage) -> DataPackage
        """
        def cmd(package):
            def bind_data_source(data_source):
                def execute(conn):
                    trend_store = trend_store_for_package(
                        data_source, package
                    )(conn)

                    trend_store.store(
                        transform_package(trend_store)(package)
                    ).run(conn)

                return execute

            return bind_data_source

        return cmd

    @staticmethod
    def filter_existing_trends(trend_store):
        """
        Return function that transforms a data package to only contain trends that
        are defined by *trend_store*.

        :param trend_store: trend store with defined trends
        :return: (DataPackage) -> DataPackage
        """
        existing_trend_names = {trend.name for trend in trend_store.trends}

        def f(package):
            return package.filter_trends(partial(contains, existing_trend_names))

        return f


def trend_store_for_package(data_source, package):
    def f(conn):
        entity_type_name = package.entity_type_name()

        with closing(conn.cursor()) as cursor:
            entity_type = EntityType.get_by_name(entity_type_name)(
                cursor
            )

            if entity_type is None:
                raise ConfigurationError(
                    "no such entity type: {}".format(entity_type_name)
                )

            return TableTrendStore.get(
                data_source, entity_type, package.granularity
            )(cursor)

    return f<commit_msg>Don't raise an exception when there is no entity type or trend store defined<commit_after>from contextlib import closing
from operator import contains
from functools import partial

from minerva.error import ConfigurationError
from minerva.util import k, identity
from minerva.directory import EntityType
from minerva.storage import Engine
from minerva.storage.trend import TableTrendStore


class TrendEngine(Engine):
    pass_through = k(identity)

    @staticmethod
    def store_cmd(package):
        """
        Return a function to bind a data source to the store command.

        :param package: A DataPackageBase subclass instance
        :return: function that binds a data source to the store command
        :rtype: (data_source) -> (conn) -> None
        """
        return TrendEngine.make_store_cmd(TrendEngine.pass_through)(package)

    @staticmethod
    def make_store_cmd(transform_package):
        """
        Return a function to bind a data source to the store command.

        :param transform_package: (TableTrendStore) -> (DataPackage) -> DataPackage
        """
        def cmd(package):
            def bind_data_source(data_source):
                def execute(conn):
                    trend_store = trend_store_for_package(
                        data_source, package
                    )(conn)

                    if trend_store is not None:
                        trend_store.store(
                            transform_package(trend_store)(package)
                        ).run(conn)

                return execute

            return bind_data_source

        return cmd

    @staticmethod
    def filter_existing_trends(trend_store):
        """
        Return function that transforms a data package to only contain trends that
        are defined by *trend_store*.

        :param trend_store: trend store with defined trends
        :return: (DataPackage) -> DataPackage
        """
        existing_trend_names = {trend.name for trend in trend_store.trends}

        def f(package):
            return package.filter_trends(partial(contains, existing_trend_names))

        return f


def trend_store_for_package(data_source, package):
    def f(conn):
        entity_type_name = package.entity_type_name()

        with closing(conn.cursor()) as cursor:
            entity_type = EntityType.get_by_name(entity_type_name)(
                cursor
            )

            if entity_type is None:
                return None
            else:
                return TableTrendStore.get(
                    data_source, entity_type, package.granularity
                )(cursor)

    return f
{'fraction_non_alphanumeric': 0.06363462640932543, 'fraction_numerical': 0.0, 'mean_word_length': 2.1191895113230035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '4536227', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1292, 'n_words': 393}