<commit_before># -*- coding: utf-8 -*-

"""This module contains functions for creating and returning abstract models
based on the project settings. These abstract models will be linked
between them with ManyToMany fields, from the leaves to the roots.
"""

# Framework imports
from django.db import models
from django.db.models.loading import get_model
from django.conf import settings

COMPLEX_STRUCTURE = getattr(settings, "COMPLEX_STRUCTURE", None)
COMPLEX_APP_NAME = getattr(settings, "COMPLEX_APP_NAME", None)

abstract_models = {}
abstract_models_data = {}


def process():
    if not (COMPLEX_STRUCTURE or COMPLEX_APP_NAME):
        return
    for root in COMPLEX_STRUCTURE['roots']:
        for entity in root:
            class Meta:
                app_label = COMPLEX_APP_NAME
                abstract = True

            attrs = {'Meta': Meta, '__module__': COMPLEX_APP_NAME + '.models'}
            abstract_models_data[entity] = ['cs_' + entity, attrs]

    countl = 0
    for nodes in COMPLEX_STRUCTURE['nodes']:
        for node in nodes:
            for entity in node:
                # first add module and meta class
                class Meta:
                    app_label = COMPLEX_APP_NAME
                    abstract = True

                attrs = {'Meta': Meta,
                         '__module__': COMPLEX_APP_NAME + '.models'}
                # then add above roots
                for root in COMPLEX_STRUCTURE['roots'][:countl + 1]:
                    for root_entity in root:
                        attrs[root_entity.lower()] = models.ManyToManyField(
                            root_entity, related_name=entity.lower(),
                            verbose_name=root_entity, blank=True, null=True
                        )
                # then add above nodes
                for above_nodes in COMPLEX_STRUCTURE['nodes'][:countl]:
                    for node_list in above_nodes:
                        for node_entity in node_list:
                            attrs[
                                node_entity.lower()] = models.ManyToManyField(
                                node_entity, related_name=entity.lower(),
                                verbose_name=node_entity, blank=True, null=True
                            )
                abstract_models_data[entity] = ['cs_' + entity, attrs]
        countl += 1


def abstract_model(entity_name):
    if len(abstract_models_data) == 0:
        process()
    installed_model = abstract_models.get(entity_name, None)
    if installed_model is None:
        model = abstract_models_data.get(entity_name)
        if model:
            abstract_models[entity_name] = type(
                model[0], (models.Model,), model[1]
            )
            return abstract_models[entity_name]
        else:
            return None
    else:
        return installed_model<commit_msg>Raising explicit errors if misconfiguration or wrong request<commit_after># -*- coding: utf-8 -*-
"""This module contains functions for creating and returning abstract models
based on the project settings. These abstract models will be linked
between them with ManyToMany fields, from the leaves to the roots.
"""

from django.db import models
from django.db.models.loading import get_model
from django.conf import settings

COMPLEX_STRUCTURE = getattr(settings, "COMPLEX_STRUCTURE", None)
COMPLEX_APP_NAME = getattr(settings, "COMPLEX_APP_NAME", None)

abstract_models = {}
abstract_models_data = {}


def process():
    if not (COMPLEX_STRUCTURE or COMPLEX_APP_NAME):
        raise ValueError(
            'COMPLEX_STRUCTURE or COMPLEX_APP_NAME is not set.')
    for root in COMPLEX_STRUCTURE['roots']:
        for entity in root:
            class Meta:
                app_label = COMPLEX_APP_NAME
                abstract = True

            attrs = {'Meta': Meta, '__module__': COMPLEX_APP_NAME + '.models'}
            abstract_models_data[entity] = ['cs_' + entity, attrs]

    countl = 0
    for nodes in COMPLEX_STRUCTURE['nodes']:
        for node in nodes:
            for entity in node:
                # first add module and meta class
                class Meta:
                    app_label = COMPLEX_APP_NAME
                    abstract = True

                attrs = {'Meta': Meta,
                         '__module__': COMPLEX_APP_NAME + '.models'}
                # then add above roots
                for root in COMPLEX_STRUCTURE['roots'][:countl + 1]:
                    for root_entity in root:
                        attrs[root_entity.lower()] = models.ManyToManyField(
                            root_entity, related_name=entity.lower(),
                            verbose_name=root_entity, blank=True, null=True
                        )
                # then add above nodes
                for above_nodes in COMPLEX_STRUCTURE['nodes'][:countl]:
                    for node_list in above_nodes:
                        for node_entity in node_list:
                            attrs[
                                node_entity.lower()] = models.ManyToManyField(
                                node_entity, related_name=entity.lower(),
                                verbose_name=node_entity, blank=True, null=True
                            )
                abstract_models_data[entity] = ['cs_' + entity, attrs]
        countl += 1


def abstract_model(entity_name):
    if len(abstract_models_data) == 0:
        process()
    installed_model = abstract_models.get(entity_name, None)
    if installed_model is None:
        model = abstract_models_data.get(entity_name)
        if model:
            abstract_models[entity_name] = type(
                model[0], (models.Model,), model[1]
            )
            return abstract_models[entity_name]
        else:
            raise ValueError(
                'COMPLEX_STRUCTURE does not contain "%s"' % entity_name)
    else:
        return installed_model
{'fraction_non_alphanumeric': 0.07755859045692126, 'fraction_numerical': 0.0023604788399932556, 'mean_word_length': 1.804728132387707, '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': '724125', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1535, 'n_words': 423}