<commit_before>"""Support for generation for models based on pyecore."""
import itertools
import os
import re

import multigen.formatter
import multigen.jinja
from pyecore import ecore
from pyecore.resources import Resource
from pyecoregen.adapter import pythonic_names


class EcoreTask(multigen.jinja.JinjaTask):
    """
    Base class for Jinja based generation of Pyecore models.

    Attributes:
        element_type: Ecore type to be searched in model and to be iterated over.
    """

    element_type = None

    def filtered_elements(self, model):
        """Return iterator based on `element_type`."""
        if isinstance(model, self.element_type):
            yield model
        yield from (e for e in model.eAllContents() if isinstance(e, self.element_type))

    @classmethod
    def folder_path_for_package(cls, package: ecore.EPackage):
        """Returns path to folder holding generated artifact for given element."""
        parent = package.eContainer()
        if parent:
            return os.path.join(cls.folder_path_for_package(parent), package.name)
        return package.name

    @staticmethod
    def filename_for_element(package: ecore.EPackage):
        """Returns generated file name."""
        raise NotImplementedError

    def relative_path_for_element(self, element: ecore.EPackage):
        path = os.path.join(self.folder_path_for_package(element),
                            self.filename_for_element(element))
        return path


class EcorePackageInitTask(EcoreTask):
    """Generation of package init file from Ecore model with Jinja2."""

    template_name = 'package.py.tpl'
    element_type = ecore.EPackage

    @staticmethod
    def filename_for_element(package: ecore.EPackage):
        return '__init__.py'

    @staticmethod
    def imported_classifiers_package(p: ecore.EPackage):
        """Determines which classifiers have to be imported into given package."""
        classes = {c for c in p.eClassifiers if isinstance(c, ecore.EClass)}

        references = itertools.chain(*(c.eAllReferences() for c in classes))
        references_types = (r.eType for r in references)
        imported = {c for c in references_types if c.ePackage is not p}

        imported_dict = {}
        for classifier in imported:
            imported_dict.setdefault(classifier.ePackage, set()).add(classifier)

        return imported_dict

    def create_template_context(self, element, **kwargs):
        return super().create_template_context(
            element=element,
            imported_classifiers_package=self.imported_classifiers_package(element)
        )


class EcorePackageModuleTask(EcoreTask):
    """Generation of package model from Ecore model with Jinja2."""

    template_name = 'module.py.tpl'
    element_type = ecore.EPackage

    @staticmethod
    def imported_classifiers(p: ecore.EPackage):
        """Determines which classifiers have to be imported into given module."""
        classes = {c for c in p.eClassifiers if isinstance(c, ecore.EClass)}

        supertypes = itertools.chain(*(c.eAllSuperTypes() for c in classes))
        imported = {c for c in supertypes if c.ePackage is not p}

        attributes = itertools.chain(*(c.eAttributes for c in classes))
        attributes_types = (a.eType for a in attributes)
        imported |= {t for t in attributes_types if t.ePackage not in {p, ecore.eClass}}

        imported_dict = {}
        for classifier in imported:
            imported_dict.setdefault(classifier.ePackage, set()).add(classifier)

        return imported_dict

    @staticmethod
    def classes(p: ecore.EPackage):
        """Returns classes in package in ordered by number of bases."""
        classes = (c for c in p.eClassifiers if isinstance(c, ecore.EClass))<commit_msg>Fix generation when attribute's type is not contained in a package<commit_after>"""Support for generation for models based on pyecore."""
import itertools
import os
import re

import multigen.formatter
import multigen.jinja
from pyecore import ecore
from pyecore.resources import Resource
from pyecoregen.adapter import pythonic_names


class EcoreTask(multigen.jinja.JinjaTask):
    """
    Base class for Jinja based generation of Pyecore models.

    Attributes:
        element_type: Ecore type to be searched in model and to be iterated over.
    """

    element_type = None

    def filtered_elements(self, model):
        """Return iterator based on `element_type`."""
        if isinstance(model, self.element_type):
            yield model
        yield from (e for e in model.eAllContents() if isinstance(e, self.element_type))

    @classmethod
    def folder_path_for_package(cls, package: ecore.EPackage):
        """Returns path to folder holding generated artifact for given element."""
        parent = package.eContainer()
        if parent:
            return os.path.join(cls.folder_path_for_package(parent), package.name)
        return package.name

    @staticmethod
    def filename_for_element(package: ecore.EPackage):
        """Returns generated file name."""
        raise NotImplementedError

    def relative_path_for_element(self, element: ecore.EPackage):
        path = os.path.join(self.folder_path_for_package(element),
                            self.filename_for_element(element))
        return path


class EcorePackageInitTask(EcoreTask):
    """Generation of package init file from Ecore model with Jinja2."""

    template_name = 'package.py.tpl'
    element_type = ecore.EPackage

    @staticmethod
    def filename_for_element(package: ecore.EPackage):
        return '__init__.py'

    @staticmethod
    def imported_classifiers_package(p: ecore.EPackage):
        """Determines which classifiers have to be imported into given package."""
        classes = {c for c in p.eClassifiers if isinstance(c, ecore.EClass)}

        references = itertools.chain(*(c.eAllReferences() for c in classes))
        references_types = (r.eType for r in references)
        imported = {c for c in references_types if c.ePackage is not p}

        imported_dict = {}
        for classifier in imported:
            imported_dict.setdefault(classifier.ePackage, set()).add(classifier)

        return imported_dict

    def create_template_context(self, element, **kwargs):
        return super().create_template_context(
            element=element,
            imported_classifiers_package=self.imported_classifiers_package(element)
        )


class EcorePackageModuleTask(EcoreTask):
    """Generation of package model from Ecore model with Jinja2."""

    template_name = 'module.py.tpl'
    element_type = ecore.EPackage

    @staticmethod
    def imported_classifiers(p: ecore.EPackage):
        """Determines which classifiers have to be imported into given module."""
        classes = {c for c in p.eClassifiers if isinstance(c, ecore.EClass)}

        supertypes = itertools.chain(*(c.eAllSuperTypes() for c in classes))
        imported = {c for c in supertypes if c.ePackage is not p}

        attributes = itertools.chain(*(c.eAttributes for c in classes))
        attributes_types = (a.eType for a in attributes)
        imported |= {t for t in attributes_types if t.ePackage not in {p, ecore.eClass, None}}

        imported_dict = {}
        for classifier in imported:
            imported_dict.setdefault(classifier.ePackage, set()).add(classifier)

        return imported_dict

    @staticmethod
    def classes(p: ecore.EPackage):
        """Returns classes in package in ordered by number of bases."""
        classes = (c for c in p.eClassifiers if isinstance(c, ecore.EClass))
{'fraction_non_alphanumeric': 0.08494411571334648, 'fraction_numerical': 0.0005259697567389875, 'mean_word_length': 3.089247311827957, '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': '2534733', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2083, 'n_words': 648}