<commit_before>import cassiopeia.type.dto.common
import cassiopeia.type.core.common


if cassiopeia.type.dto.common.sqlalchemy_imported:
    import sqlalchemy
    import sqlalchemy.orm


class DataStore(object):
    """
    A place to store data. Used for caching/storing data from API calls
    """

    def has_all(self, class_):
        """
        A place to store data. Used for caching/storing data from API calls
        """
        pass

    def get_all(self, class_):
        """
        Checks if the data store has all the values for a type there can be (as reported by the user)

        Args:
            class_ (type): the class to do the check for

        Returns:
            bool: whether all the values are stored
        """
        pass

    def iterate(self, class_):
        """
        Gets all currently stored values for a type

        Args:
            class_ (type): the class to get values for

        Returns:
            list<class_>: all stored values for the type
        """
        pass

    def get(self, class_, keys, key_field):
        """
        Gets an iterator over all currently stored values for a type

        Args:
            class_ (type): the class to get values for

        Returns:
            iterator<class_>: and iterator over all stored values for the type
        """
        pass

    def store(self, objs, keys, complete_sets=[]):
        """
        Gets objects from the data store

        Args:
            class_ (type): the class to get values for
            keys (any | list<any>): the keys that should be used to find the desired values
            key_field (str): the name of the attribute that the key(s) reference

        Returns:
            class_ | list<class_>: the values from storage - None will replace any value that couldn't be found
        """
        pass


#######################
# Void Store resouces #
#######################
@cassiopeia.type.core.common.inheritdocs
class VoidDataStore(DataStore):
    """
    Stores objects in the data store

    Args:
        objs (any): the objects to store
        keys (any | list<any>): the keys to store those values with
        complete_sets (list<type>): include any types for which it should be marked that all possible values are stored
    """

    def has_all(self, class_):
        return False

    def get_all(self, class_):
        return []

    def iterate(self, class_):
        return iter([])

    def get(self, class_, keys, key_field):
        if isinstance(keys, list):
            return [None for _ in range(len(keys))]
        return None

    def store(self, objs, keys, complete_sets=[]):
        pass


#############################
# In-memory Cache resources #
#############################
@cassiopeia.type.core.common.inheritdocs
class Cache(DataStore):
    """
    Stores objects in the data store

    Args:
        objs (any): the objects to store
        keys (any | list<any>): the keys to store those values with
        complete_sets (list<type>): include any types for which it should be marked that all possible values are stored
    """

    def __init__(self):
        self._cache = {}
        self._has_all = {}

    def has_all(self, class_):
        try:
            return self._has_all[class_]
        except KeyError:
            return False

    def get_all(self, class_):
        results = []
        try:
            for obj in self._cache[class_].items():
                results.append(obj[1])
        except KeyError:
            pass

        return results

    def iterate(self, class_):
        try:
            iter(self._cache[class_].values())
        except KeyError:
            return iter([])

    def get(self, class_, keys, key_field):
        if class_ not in self._cache:
            if not isinstance(keys, list):
                return None
            else:
                results = []
                for _ in range(len(keys)):
                    results.append(None)
                return results

        if not isinstance(keys, list):
            try:
                return self._cache[class_][keys]
            except KeyError:
                return None
        else:
            results = []
            for key in keys:
                try:
                    results.append(self._cache[class_][key])
                except KeyError:
                    results.append(None)
            return results

    def store(self, objs, keys, complete_sets=[]):
        is_list = isinstance(objs, list)
        if is_list != isinstance(keys, list):
            raise ValueError("Object(s) and Key(s) must both be lists or both be non-lists")

        if not is_list:
            class_ = type(objs)
            if class_ not in self._cache:
                self._cache[class_] = {}
            self._cache[class_][keys] = objs
        else:
            if len(objs) != len(keys):
                raise ValueError("Objects and Keys must be the same length")

            for i in range(len(objs)):
                class_ = type(objs[i])
                if class_ not in self._cache:
                    self._cache[class_] = {}
                self._cache[class_][keys[i]] = objs[i]

        if complete_sets:
            for class_ in complete_sets:
                self._has_all[class_] = True


########################
# SQLAlchemy resources #
########################
if cassiopeia.type.dto.common.sqlalchemy_imported:
    class HasAllStatus(cassiopeia.type.dto.common.BaseDB):
        __tablename__ = "HasAll"
        class_ = sqlalchemy.Column(sqlalchemy.String(50), primary_key=True)
        have_all = sqlalchemy.Column(sqlalchemy.Boolean)

        def get_name(class_):
            return "{module}.{name}".format(module=class_.dto_type.__module__, name=class_.dto_type.__name__)

        def __init__(self, class_, have_all=True):
            self.class_ = HasAllStatus.get_name(class_)
            self.have_all = have_all

    @cassiopeia.type.core.common.inheritdocs
    class SQLAlchemyDB(DataStore):
        class Iterator(object):
            def __init__(self, class_, result):
                self.class_ = class_
                self.result = result

            def __next__(self):
                try:
                    val = self.class_(self.result[self.index])
                    self.index += 1
                    return val
                except IndexError:
                    raise StopIteration

            def __iter__(self):
                self.index = 0
                return self

        def __init__(self, flavor, host, database, username, password):
            """
            Stores objects in the data store<commit_msg>This should be a static method since we don't pass in self<commit_after>import cassiopeia.type.dto.common
import cassiopeia.type.core.common


if cassiopeia.type.dto.common.sqlalchemy_imported:
    import sqlalchemy
    import sqlalchemy.orm


class DataStore(object):
    """
    A place to store data. Used for caching/storing data from API calls
    """

    def has_all(self, class_):
        """
        A place to store data. Used for caching/storing data from API calls
        """
        pass

    def get_all(self, class_):
        """
        Checks if the data store has all the values for a type there can be (as reported by the user)

        Args:
            class_ (type): the class to do the check for

        Returns:
            bool: whether all the values are stored
        """
        pass

    def iterate(self, class_):
        """
        Gets all currently stored values for a type

        Args:
            class_ (type): the class to get values for

        Returns:
            list<class_>: all stored values for the type
        """
        pass

    def get(self, class_, keys, key_field):
        """
        Gets an iterator over all currently stored values for a type

        Args:
            class_ (type): the class to get values for

        Returns:
            iterator<class_>: and iterator over all stored values for the type
        """
        pass

    def store(self, objs, keys, complete_sets=[]):
        """
        Gets objects from the data store

        Args:
            class_ (type): the class to get values for
            keys (any | list<any>): the keys that should be used to find the desired values
            key_field (str): the name of the attribute that the key(s) reference

        Returns:
            class_ | list<class_>: the values from storage - None will replace any value that couldn't be found
        """
        pass


#######################
# Void Store resouces #
#######################
@cassiopeia.type.core.common.inheritdocs
class VoidDataStore(DataStore):
    """
    Stores objects in the data store

    Args:
        objs (any): the objects to store
        keys (any | list<any>): the keys to store those values with
        complete_sets (list<type>): include any types for which it should be marked that all possible values are stored
    """

    def has_all(self, class_):
        return False

    def get_all(self, class_):
        return []

    def iterate(self, class_):
        return iter([])

    def get(self, class_, keys, key_field):
        if isinstance(keys, list):
            return [None for _ in range(len(keys))]
        return None

    def store(self, objs, keys, complete_sets=[]):
        pass


#############################
# In-memory Cache resources #
#############################
@cassiopeia.type.core.common.inheritdocs
class Cache(DataStore):
    """
    Stores objects in the data store

    Args:
        objs (any): the objects to store
        keys (any | list<any>): the keys to store those values with
        complete_sets (list<type>): include any types for which it should be marked that all possible values are stored
    """

    def __init__(self):
        self._cache = {}
        self._has_all = {}

    def has_all(self, class_):
        try:
            return self._has_all[class_]
        except KeyError:
            return False

    def get_all(self, class_):
        results = []
        try:
            for obj in self._cache[class_].items():
                results.append(obj[1])
        except KeyError:
            pass

        return results

    def iterate(self, class_):
        try:
            iter(self._cache[class_].values())
        except KeyError:
            return iter([])

    def get(self, class_, keys, key_field):
        if class_ not in self._cache:
            if not isinstance(keys, list):
                return None
            else:
                results = []
                for _ in range(len(keys)):
                    results.append(None)
                return results

        if not isinstance(keys, list):
            try:
                return self._cache[class_][keys]
            except KeyError:
                return None
        else:
            results = []
            for key in keys:
                try:
                    results.append(self._cache[class_][key])
                except KeyError:
                    results.append(None)
            return results

    def store(self, objs, keys, complete_sets=[]):
        is_list = isinstance(objs, list)
        if is_list != isinstance(keys, list):
            raise ValueError("Object(s) and Key(s) must both be lists or both be non-lists")

        if not is_list:
            class_ = type(objs)
            if class_ not in self._cache:
                self._cache[class_] = {}
            self._cache[class_][keys] = objs
        else:
            if len(objs) != len(keys):
                raise ValueError("Objects and Keys must be the same length")

            for i in range(len(objs)):
                class_ = type(objs[i])
                if class_ not in self._cache:
                    self._cache[class_] = {}
                self._cache[class_][keys[i]] = objs[i]

        if complete_sets:
            for class_ in complete_sets:
                self._has_all[class_] = True


########################
# SQLAlchemy resources #
########################
if cassiopeia.type.dto.common.sqlalchemy_imported:
    class HasAllStatus(cassiopeia.type.dto.common.BaseDB):
        __tablename__ = "HasAll"
        class_ = sqlalchemy.Column(sqlalchemy.String(50), primary_key=True)
        have_all = sqlalchemy.Column(sqlalchemy.Boolean)

        @staticmethod
        def get_name(class_):
            return "{module}.{name}".format(module=class_.dto_type.__module__, name=class_.dto_type.__name__)

        def __init__(self, class_, have_all=True):
            self.class_ = HasAllStatus.get_name(class_)
            self.have_all = have_all

    @cassiopeia.type.core.common.inheritdocs
    class SQLAlchemyDB(DataStore):
        class Iterator(object):
            def __init__(self, class_, result):
                self.class_ = class_
                self.result = result

            def __next__(self):
                try:
                    val = self.class_(self.result[self.index])
                    self.index += 1
                    return val
                except IndexError:
                    raise StopIteration

            def __iter__(self):
                self.index = 0
                return self

        def __init__(self, flavor, host, database, username, password):
            """
            Stores objects in the data store
{'fraction_non_alphanumeric': 0.10580433648759408, 'fraction_numerical': 0.00074510096118024, 'mean_word_length': 1.8167890870933894, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4501001', 'n_tokens_mistral': 3723, 'n_tokens_neox': 3533, 'n_words': 1143}