<commit_before>"""
This module is the heart of PyEcore. It defines all the basic concepts that
are common to EMF-Java and PyEcore (EObject/EClass...).

These concepts are enough if dynamic metamodel instance are handled (code
generation is not required).
"""
from functools import partial
import sys
import keyword
import inspect
from itertools import takewhile
from ordered_set import OrderedSet, is_iterable
from .notification import ENotifer, Notification, Kind, EObserver


name = 'ecore'
nsPrefix = 'ecore'
nsURI = 'http://www.eclipse.org/emf/2002/Ecore'

# This var will be automatically populated.
# In this case, it MUST be set to an empty dict,
# otherwise, the getEClassifier would be overriden
eClassifiers = {}  # Will be automatically populated
eSubpackages = []


def default_eURIFragment():
    return '/'


def eURIFragment():
    return '#/'


def getEClassifier(name, searchspace=None):
    searchspace = searchspace or eClassifiers
    try:
        return searchspace[name]
    except KeyError:
        return None


class BadValueError(TypeError):
    def __init__(self, got=None, expected=None):
        msg = "Expected type {0}, but got type {1} with value {2} instead"
        msg = msg.format(expected, type(got).__name__, got)
        super().__init__(msg)


class EcoreUtils(object):
    @staticmethod
    def isinstance(obj, _type):
        if obj is None:
            return True
        elif isinstance(obj, EProxy) and not obj._resolved:
            return True
        elif _type is EPackage:
            return isinstance(obj, _type) or \
                       inspect.ismodule(obj) and hasattr(obj, 'nsURI')
        elif _type is EClassifier:
            return obj is _type or isinstance(obj, _type) or \
                        hasattr(obj, '_staticEClass') and obj._staticEClass
        elif isinstance(_type, EEnum):
            return obj in _type
        elif isinstance(_type, (EDataType, EAttribute)):
            return isinstance(obj, _type.eType)
        elif isinstance(_type, EClass):
            if isinstance(obj, EObject):
                return isinstance(obj, _type.python_class)
                # return obj.eClass is _type \
                #        or _type in obj.eClass.eAllSuperTypes()
            return False
        return isinstance(obj, _type) or obj is _type.eClass

    @staticmethod
    def getRoot(obj):
        if not obj:
            return None
        previous = obj
        while previous.eContainer() is not None:
            previous = previous.eContainer()
        return previous


class Core(object):
    def _promote(cls, abstract=False):
        cls.eClass = EClass(cls.__name__, metainstance=cls)
        cls.eClass.abstract = abstract
        cls._staticEClass = True
        # init super types
        for _cls in cls.__bases__:
            if _cls is not EObject and _cls is not ENotifer:
                cls.eClass.eSuperTypes.append(_cls.eClass)
        # init eclass by reflection
        for k, feature in cls.__dict__.items():
            if isinstance(feature, EStructuralFeature):
                if not feature.name:
                    feature.name = k
                cls.eClass.eStructuralFeatures.append(feature)
            elif inspect.isfunction(feature):
                if k == '__init__':
                    continue
                argspect = inspect.getargspec(feature)
                args = argspect.args
                if len(args) < 1 or args[0] != 'self':
                    continue
                operation = EOperation(feature.__name__)
                defaults = argspect.defaults
                len_defaults = len(defaults) if defaults else 0
                nb_required = len(args) - len_defaults
                for i, parameter_name in enumerate(args):
                    parameter = EParameter(parameter_name, eType=ENativeType)
                    if i < nb_required:
                        parameter.required = True
                    operation.eParameters.append(parameter)
                cls.eClass.eOperations.append(operation)

    def register_classifier(cls, abstract=False, promote=False):
        if promote:
            Core._promote(cls, abstract)
        epackage = sys.modules[cls.__module__]
        if not hasattr(epackage, 'eClassifiers'):
            eclassifs = {}
            epackage.eClassifiers = eclassifs
            epackage.getEClassifier = partial(getEClassifier,
                                              searchspace=eclassifs)
        # object.__setattr__(cls.eClass, 'ePackage', epackage)
        cname = cls.name if isinstance(cls, EClassifier) else cls.__name__
        epackage.eClassifiers[cname] = cls
        if hasattr(epackage, 'eResource'):
            cls._eresource = epackage.eResource
        if isinstance(cls, EDataType):
            epackage.eClass.eClassifiers.append(cls)
            cls._container = epackage
        else:
            epackage.eClass.eClassifiers.append(cls.eClass)
            cls.eClass._container = epackage


class EObject(ENotifer):
    _staticEClass = True

    def __init__(self):
        self.__subinit__()
        self.__initmetattr__()
        self._isready = True

    def __subinit__(self):
        self._xmiid = None
        self._isset = set()
        self._container = None
        self._isready = False
        self._containment_feature = None
        self._eresource = None
        self.listeners = []
        self._eternal_listener = []
        self._inverse_rels = set()

    def __initmetattr__(self):
        def initattr(cls):
            for key, feature in cls.__dict__.items():
                if not isinstance(feature, EStructuralFeature):
                    continue
                if feature.many:
                    object.__setattr__(self,
                                       key,
                                       ECollection.create(self, feature))
                else:
                    default_value = None
                    if isinstance(feature, EAttribute):
                        default_value = feature.eType.default_value
                    object.__setattr__(self, key, default_value)

        super_cls = takewhile(lambda x: x is not EObject, self.__class__.mro())
        for cls in reversed(list(super_cls)):
            initattr(cls)

    def eContainer(self):
        return self._container

    def eContainmentFeature(self):
        return self._containment_feature

    def eIsSet(self, feature):
        if isinstance(feature, str):
            feature = self.eClass.findEStructuralFeature(feature)
        return feature in self._isset

    @property
    def eResource(self):
        return self._eresource

    def eGet(self, feature):
        if isinstance(feature, str):
            return self.__getattribute__(feature)
        elif isinstance(feature, EStructuralFeature):<commit_msg>Remove closure in __initmetattr__<commit_after>"""
This module is the heart of PyEcore. It defines all the basic concepts that
are common to EMF-Java and PyEcore (EObject/EClass...).

These concepts are enough if dynamic metamodel instance are handled (code
generation is not required).
"""
from functools import partial
import sys
import keyword
import inspect
from itertools import takewhile
from ordered_set import OrderedSet, is_iterable
from .notification import ENotifer, Notification, Kind, EObserver


name = 'ecore'
nsPrefix = 'ecore'
nsURI = 'http://www.eclipse.org/emf/2002/Ecore'

# This var will be automatically populated.
# In this case, it MUST be set to an empty dict,
# otherwise, the getEClassifier would be overriden
eClassifiers = {}  # Will be automatically populated
eSubpackages = []


def default_eURIFragment():
    return '/'


def eURIFragment():
    return '#/'


def getEClassifier(name, searchspace=None):
    searchspace = searchspace or eClassifiers
    try:
        return searchspace[name]
    except KeyError:
        return None


class BadValueError(TypeError):
    def __init__(self, got=None, expected=None):
        msg = "Expected type {0}, but got type {1} with value {2} instead"
        msg = msg.format(expected, type(got).__name__, got)
        super().__init__(msg)


class EcoreUtils(object):
    @staticmethod
    def isinstance(obj, _type):
        if obj is None:
            return True
        elif isinstance(obj, EProxy) and not obj._resolved:
            return True
        elif _type is EPackage:
            return isinstance(obj, _type) or \
                       inspect.ismodule(obj) and hasattr(obj, 'nsURI')
        elif _type is EClassifier:
            return obj is _type or isinstance(obj, _type) or \
                        hasattr(obj, '_staticEClass') and obj._staticEClass
        elif isinstance(_type, EEnum):
            return obj in _type
        elif isinstance(_type, (EDataType, EAttribute)):
            return isinstance(obj, _type.eType)
        elif isinstance(_type, EClass):
            if isinstance(obj, EObject):
                return isinstance(obj, _type.python_class)
                # return obj.eClass is _type \
                #        or _type in obj.eClass.eAllSuperTypes()
            return False
        return isinstance(obj, _type) or obj is _type.eClass

    @staticmethod
    def getRoot(obj):
        if not obj:
            return None
        previous = obj
        while previous.eContainer() is not None:
            previous = previous.eContainer()
        return previous


class Core(object):
    def _promote(cls, abstract=False):
        cls.eClass = EClass(cls.__name__, metainstance=cls)
        cls.eClass.abstract = abstract
        cls._staticEClass = True
        # init super types
        for _cls in cls.__bases__:
            if _cls is not EObject and _cls is not ENotifer:
                cls.eClass.eSuperTypes.append(_cls.eClass)
        # init eclass by reflection
        for k, feature in cls.__dict__.items():
            if isinstance(feature, EStructuralFeature):
                if not feature.name:
                    feature.name = k
                cls.eClass.eStructuralFeatures.append(feature)
            elif inspect.isfunction(feature):
                if k == '__init__':
                    continue
                argspect = inspect.getargspec(feature)
                args = argspect.args
                if len(args) < 1 or args[0] != 'self':
                    continue
                operation = EOperation(feature.__name__)
                defaults = argspect.defaults
                len_defaults = len(defaults) if defaults else 0
                nb_required = len(args) - len_defaults
                for i, parameter_name in enumerate(args):
                    parameter = EParameter(parameter_name, eType=ENativeType)
                    if i < nb_required:
                        parameter.required = True
                    operation.eParameters.append(parameter)
                cls.eClass.eOperations.append(operation)

    def register_classifier(cls, abstract=False, promote=False):
        if promote:
            Core._promote(cls, abstract)
        epackage = sys.modules[cls.__module__]
        if not hasattr(epackage, 'eClassifiers'):
            eclassifs = {}
            epackage.eClassifiers = eclassifs
            epackage.getEClassifier = partial(getEClassifier,
                                              searchspace=eclassifs)
        # object.__setattr__(cls.eClass, 'ePackage', epackage)
        cname = cls.name if isinstance(cls, EClassifier) else cls.__name__
        epackage.eClassifiers[cname] = cls
        if hasattr(epackage, 'eResource'):
            cls._eresource = epackage.eResource
        if isinstance(cls, EDataType):
            epackage.eClass.eClassifiers.append(cls)
            cls._container = epackage
        else:
            epackage.eClass.eClassifiers.append(cls.eClass)
            cls.eClass._container = epackage


class EObject(ENotifer):
    _staticEClass = True

    def __init__(self):
        self.__subinit__()
        self.__initmetattr__()
        self._isready = True

    def __subinit__(self):
        self._xmiid = None
        self._isset = set()
        self._container = None
        self._isready = False
        self._containment_feature = None
        self._eresource = None
        self.listeners = []
        self._eternal_listener = []
        self._inverse_rels = set()

    def __initmetattr__(self):
        super_cls = takewhile(lambda x: x is not EObject, self.__class__.mro())
        for cls in reversed(list(super_cls)):
            for key, feature in cls.__dict__.items():
                if not isinstance(feature, EStructuralFeature):
                    continue
                if feature.many:
                    object.__setattr__(self,
                                       key,
                                       ECollection.create(self, feature))
                else:
                    default_value = None
                    if isinstance(feature, EAttribute):
                        default_value = feature.eType.default_value
                    object.__setattr__(self, key, default_value)

    def eContainer(self):
        return self._container

    def eContainmentFeature(self):
        return self._containment_feature

    def eIsSet(self, feature):
        if isinstance(feature, str):
            feature = self.eClass.findEStructuralFeature(feature)
        return feature in self._isset

    @property
    def eResource(self):
        return self._eresource

    def eGet(self, feature):
        if isinstance(feature, str):
            return self.__getattribute__(feature)
        elif isinstance(feature, EStructuralFeature):
{'fraction_non_alphanumeric': 0.07875109681193332, 'fraction_numerical': 0.0014624159110851126, 'mean_word_length': 1.9836387434554974, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799147', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3550, 'n_words': 991}