<commit_before>import inspect

import six
from django.db import models

from ...core.classtypes.objecttype import ObjectType, ObjectTypeMeta
from ...relay.types import Connection, Node, NodeMeta
from .utils import DJANGO_FILTER_INSTALLED
from .converter import convert_django_field
from .options import DjangoOptions
from .utils import get_reverse_fields, maybe_queryset


class DjangoObjectTypeMeta(ObjectTypeMeta):
    options_class = DjangoOptions

    def construct_fields(cls):
        only_fields = cls._meta.only_fields
        reverse_fields = get_reverse_fields(cls._meta.model)
        all_fields = sorted(list(cls._meta.model._meta.fields) +
                            list(cls._meta.model._meta.local_many_to_many))
        all_fields += list(reverse_fields)
        already_created_fields = {f.attname for f in cls._meta.local_fields}

        for field in all_fields:
            is_not_in_only = only_fields and field.name not in only_fields
            is_already_created = field.name in already_created_fields
            is_excluded = field.name in cls._meta.exclude_fields or is_already_created
            if is_not_in_only or is_excluded:
                # We skip this field if we specify only_fields and is not
                # in there. Or when we exclude this field in exclude_fields
                continue
            converted_field = cls.convert_django_field(field)
            cls.add_to_class(field.name, converted_field)

    def convert_django_field(cls, field):
        return convert_django_field(field)

    def construct(cls, *args, **kwargs):
        cls = super(DjangoObjectTypeMeta, cls).construct(*args, **kwargs)
        if not cls._meta.abstract:
            if not cls._meta.model:
                raise Exception(
                    'Django ObjectType %s must have a model in the Meta class attr' %
                    cls)
            elif not inspect.isclass(cls._meta.model) or not issubclass(cls._meta.model, models.Model):
                raise Exception('Provided model in %s is not a Django model' % cls)

            cls.construct_fields()
        return cls


class DjangoFilterObjectTypeMeta(ObjectTypeMeta):

    def convert_django_field(cls, field):
        from graphene.contrib.django.filter import DjangoFilterConnectionField
        field = super(DjangoFilterObjectTypeMeta, cls).convert_django_field(field)
        field.connection_field_class = DjangoFilterConnectionField
        return field


class InstanceObjectType(ObjectType):

    class Meta:
        abstract = True

    def __init__(self, _root=None):
        if _root:
            assert isinstance(_root, self._meta.model), (
                '{} received a non-compatible instance ({}) '
                'when expecting {}'.format(
                    self.__class__.__name__,
                    _root.__class__.__name__,
                    self._meta.model.__name__
                ))
        super(InstanceObjectType, self).__init__(_root=_root)

    @property
    def instance(self):
        return self._root

    @instance.setter
    def instance(self, value):
        self._root = value

    def __getattr__(self, attr):
        return getattr(self._root, attr)


class DjangoObjectType(six.with_metaclass(
        DjangoObjectTypeMeta, InstanceObjectType)):

    class Meta:
        abstract = True


class DjangoConnection(Connection):

    @classmethod
    def from_list(cls, iterable, *args, **kwargs):
        iterable = maybe_queryset(iterable)
        return super(DjangoConnection, cls).from_list(iterable, *args, **kwargs)


django_node_meta_bases = (DjangoObjectTypeMeta, NodeMeta)
# Only include filter functionality if available
if DJANGO_FILTER_INSTALLED:
    django_node_meta_bases = (DjangoFilterObjectTypeMeta,) + django_node_meta_bases


class DjangoNodeMeta(*django_node_meta_bases):
    pass


class NodeInstance(Node, InstanceObjectType):

    class Meta:
        abstract = True


class DjangoNode(six.with_metaclass(
        DjangoNodeMeta, NodeInstance)):

    class Meta:
        abstract = True

    @classmethod
    def get_node(cls, id, info=None):
        try:
            instance = cls._meta.model.objects.get(id=id)
            return cls(instance)
        except cls._meta.model.DoesNotExist:
            return None

    connection_type = DjangoConnection<commit_msg>Fixed incompatible syntax in Python 2.7<commit_after>import inspect

import six
from django.db import models

from ...core.classtypes.objecttype import ObjectType, ObjectTypeMeta
from ...relay.types import Connection, Node, NodeMeta
from .utils import DJANGO_FILTER_INSTALLED
from .converter import convert_django_field
from .options import DjangoOptions
from .utils import get_reverse_fields, maybe_queryset


class DjangoObjectTypeMeta(ObjectTypeMeta):
    options_class = DjangoOptions

    def construct_fields(cls):
        only_fields = cls._meta.only_fields
        reverse_fields = get_reverse_fields(cls._meta.model)
        all_fields = sorted(list(cls._meta.model._meta.fields) +
                            list(cls._meta.model._meta.local_many_to_many))
        all_fields += list(reverse_fields)
        already_created_fields = {f.attname for f in cls._meta.local_fields}

        for field in all_fields:
            is_not_in_only = only_fields and field.name not in only_fields
            is_already_created = field.name in already_created_fields
            is_excluded = field.name in cls._meta.exclude_fields or is_already_created
            if is_not_in_only or is_excluded:
                # We skip this field if we specify only_fields and is not
                # in there. Or when we exclude this field in exclude_fields
                continue
            converted_field = cls.convert_django_field(field)
            cls.add_to_class(field.name, converted_field)

    def convert_django_field(cls, field):
        return convert_django_field(field)

    def construct(cls, *args, **kwargs):
        cls = super(DjangoObjectTypeMeta, cls).construct(*args, **kwargs)
        if not cls._meta.abstract:
            if not cls._meta.model:
                raise Exception(
                    'Django ObjectType %s must have a model in the Meta class attr' %
                    cls)
            elif not inspect.isclass(cls._meta.model) or not issubclass(cls._meta.model, models.Model):
                raise Exception('Provided model in %s is not a Django model' % cls)

            cls.construct_fields()
        return cls


class DjangoFilterObjectTypeMeta(ObjectTypeMeta):

    def convert_django_field(cls, field):
        from graphene.contrib.django.filter import DjangoFilterConnectionField
        field = super(DjangoFilterObjectTypeMeta, cls).convert_django_field(field)
        field.connection_field_class = DjangoFilterConnectionField
        return field


class InstanceObjectType(ObjectType):

    class Meta:
        abstract = True

    def __init__(self, _root=None):
        if _root:
            assert isinstance(_root, self._meta.model), (
                '{} received a non-compatible instance ({}) '
                'when expecting {}'.format(
                    self.__class__.__name__,
                    _root.__class__.__name__,
                    self._meta.model.__name__
                ))
        super(InstanceObjectType, self).__init__(_root=_root)

    @property
    def instance(self):
        return self._root

    @instance.setter
    def instance(self, value):
        self._root = value

    def __getattr__(self, attr):
        return getattr(self._root, attr)


class DjangoObjectType(six.with_metaclass(
        DjangoObjectTypeMeta, InstanceObjectType)):

    class Meta:
        abstract = True


class DjangoConnection(Connection):

    @classmethod
    def from_list(cls, iterable, *args, **kwargs):
        iterable = maybe_queryset(iterable)
        return super(DjangoConnection, cls).from_list(iterable, *args, **kwargs)


django_filter_metabase = type
# Only include filter functionality if available
if DJANGO_FILTER_INSTALLED:
    django_filter_metabase = DjangoFilterObjectTypeMeta


class DjangoNodeMeta(django_filter_metabase, DjangoObjectTypeMeta, NodeMeta):
    pass


class NodeInstance(Node, InstanceObjectType):

    class Meta:
        abstract = True


class DjangoNode(six.with_metaclass(
        DjangoNodeMeta, NodeInstance)):

    class Meta:
        abstract = True

    @classmethod
    def get_node(cls, id, info=None):
        try:
            instance = cls._meta.model.objects.get(id=id)
            return cls(instance)
        except cls._meta.model.DoesNotExist:
            return None

    connection_type = DjangoConnection
{'fraction_non_alphanumeric': 0.0736599423631124, 'fraction_numerical': 0.00023054755043227666, 'mean_word_length': 2.676271186440678, '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': '4916482', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2346, 'n_words': 590}