<commit_before># -* -coding: utf - 8 -* -
"""Provides the legacy AttributePlugin class."""
__docformat__ = "restructuredtext en"

__copyright__ = """
Copyright (C) 2008-2013 Hendrikx-ITC B.V.

Distributed under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option) any later
version.  The full license is in the file COPYING, distributed as part of
this software.
"""
from contextlib import closing

from minerva.directory.helpers_v4 import get_entity, get_entitytype_by_id

from minerva.storage.attribute import datapackage as dp
from minerva.storage.attribute import rawdatapackage as rdp
from minerva.storage.attribute.attribute import Attribute
from minerva.storage.attribute.attributestore import AttributeStore
from minerva.storage.attribute.retrieve import retrieve, retrieve_current, \
    retrieve_attributes_for_entity


class AttributePlugin(object):
    DataPackage = dp.DataPackage
    RawDataPackage = rdp.RawDataPackage

    def __init__(self, conn):
        self.conn = conn

    def store(self, datasource, entitytype, datapackage):
        attributes = [Attribute(name) for name in datapackage.attribute_names]

        with closing(self.conn.cursor()) as cursor:
            attributestore = AttributeStore.from_attributes(
                cursor, datasource, entitytype, attributes)

        self.conn.commit()

        attributestore.store_txn(datapackage).run(self.conn)

    def retrieve_attributes_for_entity(self, entity_id, attributes):
        return retrieve_attributes_for_entity(self.conn, entity_id, attributes)

    def retrieve(self, datasource, entitytype, attribute_names, entities,
                 timestamp=None):
        attributestore = AttributeStore(datasource, entitytype)

        return retrieve(self.conn, attributestore.table, attribute_names,
                        entities, timestamp)

    def retrieve_current(self, datasource, entitytype, attribute_names,
                         entities, limit=None):

        attributestore = AttributeStore(datasource, entitytype)

        return retrieve_current(self.conn, attributestore.table,
                                attribute_names, entities)

    def store_raw(self, datasource, rawdatapackage):
        with closing(self.conn.cursor()) as cursor:
            datapackage = rawdatapackage.refine(cursor)

            dn = rawdatapackage.rows[0][0]
            entity = get_entity(cursor, dn)
            entitytype = get_entitytype_by_id(cursor, entity.entitytype_id)

        self.store(datasource, entitytype, datapackage)

    def get_attribute_by_id(self, attribute_id):
        with closing(self.conn.cursor()) as cursor:
            return Attribute.get(cursor, attribute_id)
<commit_msg>Attribute.plugin.retrieve should return everything not just current.<commit_after># -* -coding: utf - 8 -* -
"""Provides the legacy AttributePlugin class."""
__docformat__ = "restructuredtext en"

__copyright__ = """
Copyright (C) 2008-2013 Hendrikx-ITC B.V.

Distributed under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option) any later
version.  The full license is in the file COPYING, distributed as part of
this software.
"""
from contextlib import closing

from minerva.directory.helpers_v4 import get_entity, get_entitytype_by_id

from minerva.storage.attribute import datapackage as dp
from minerva.storage.attribute import rawdatapackage as rdp
from minerva.storage.attribute.attribute import Attribute
from minerva.storage.attribute.attributestore import AttributeStore
from minerva.storage.attribute.retrieve import retrieve, retrieve_current, \
    retrieve_attributes_for_entity


class AttributePlugin(object):
    DataPackage = dp.DataPackage
    RawDataPackage = rdp.RawDataPackage

    def __init__(self, conn):
        self.conn = conn

    def store(self, datasource, entitytype, datapackage):
        attributes = [Attribute(name) for name in datapackage.attribute_names]

        with closing(self.conn.cursor()) as cursor:
            attributestore = AttributeStore.from_attributes(
                cursor, datasource, entitytype, attributes)

        self.conn.commit()

        attributestore.store_txn(datapackage).run(self.conn)

    def retrieve_attributes_for_entity(self, entity_id, attributes):
        return retrieve_attributes_for_entity(self.conn, entity_id, attributes)

    def retrieve(self, datasource, entitytype, attribute_names, entities,
                 timestamp=None):
        attributestore = AttributeStore(datasource, entitytype)

        return retrieve(self.conn, attributestore.history_table, attribute_names,
                        entities, timestamp)

    def retrieve_current(self, datasource, entitytype, attribute_names,
                         entities, limit=None):

        attributestore = AttributeStore(datasource, entitytype)

        return retrieve_current(self.conn, attributestore.table,
                                attribute_names, entities)

    def store_raw(self, datasource, rawdatapackage):
        with closing(self.conn.cursor()) as cursor:
            datapackage = rawdatapackage.refine(cursor)

            dn = rawdatapackage.rows[0][0]
            entity = get_entity(cursor, dn)
            entitytype = get_entitytype_by_id(cursor, entity.entitytype_id)

        self.store(datasource, entitytype, datapackage)

    def get_attribute_by_id(self, attribute_id):
        with closing(self.conn.cursor()) as cursor:
            return Attribute.get(cursor, attribute_id)

{'fraction_non_alphanumeric': 0.07648422178641469, 'fraction_numerical': 0.00463540738099483, 'mean_word_length': 3.4594594594594597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4819496', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1491, 'n_words': 402}