<commit_before>from types import MethodType

from django.db.models.fields.related import RelatedObject, ManyToManyField

from getqryresult import GetQryResult


def _create_read_doc(cls, fieldnames=None):
    meta = cls._meta
    clspath = '%s.%s' % (cls.__module__, cls.__name__)
    fieldnames = fieldnames or meta.model._meta.get_all_field_names()
    resultfields = []
    for fieldname in fieldnames:
        field = meta.model._meta.get_field_by_name(fieldname)[0]
        if isinstance(field, ManyToManyField):
            pass
        elif isinstance(field, RelatedObject):
            pass
        else:
            if hasattr(field, 'help_text'):
                help_text = field.help_text
            else:
                help_text = ''
            #print type(field), field.name, help_text
            resultfields.append(':param %s: %s' % (field.name, help_text))

    #throws = [
            #':throws devilry.apps.core.models.Node.DoesNotExist:',
            #'   If the node with ``idorkw`` does not exists, or if',
            #'   parentnode is not None, and no node with ``id==parentnode_id``',
            #'   exists.']

    get_doc = '\n'.join(
            ['Get a %(modelname)s object.'] + ['\n\n'] + resultfields)
    modelname = meta.model.__name__
    return get_doc % vars()


def _parse_fieldgroups(fieldlst, fieldgroups):
    if isinstance(fieldlst, dict):
        base = fieldlst.get('__BASE__', [])
        fields = list(base)
        for group in fieldgroups:
            fields.extend(fieldlst.get(group, []))
        return fields
    else:
        return fieldlst


def _writeauth_get(cls, user, idorkw):
    if isinstance(idorkw, dict):
        kw = idorkw
    else:
        kw = dict(pk=idorkw)
    obj = cls._meta.model.objects.get(**kw)
    cls.write_authorize(user, obj)
    return obj

def _readauth_get(cls, user, idorkw):
    if isinstance(idorkw, dict):
        kw = idorkw
    else:
        kw = dict(pk=idorkw)
    obj = cls._meta.model.objects.get(**kw)
    cls.read_authorize(user, obj)
    return obj


def _recurse_getmodelattr(instance, path):
    cur = getattr(instance, path.pop(0))
    if not path:
        return cur<commit_msg>simplified: simplified_api.py: Fixed a bug in _parse_fieldgroups.\n\nIterated over the wrong datastructure, which caused the extra resultfields not to be parsed<commit_after>from types import MethodType

from django.db.models.fields.related import RelatedObject, ManyToManyField

from getqryresult import GetQryResult


def _create_read_doc(cls, fieldnames=None):
    meta = cls._meta
    clspath = '%s.%s' % (cls.__module__, cls.__name__)
    fieldnames = fieldnames or meta.model._meta.get_all_field_names()
    resultfields = []
    for fieldname in fieldnames:
        field = meta.model._meta.get_field_by_name(fieldname)[0]
        if isinstance(field, ManyToManyField):
            pass
        elif isinstance(field, RelatedObject):
            pass
        else:
            if hasattr(field, 'help_text'):
                help_text = field.help_text
            else:
                help_text = ''
            #print type(field), field.name, help_text
            resultfields.append(':param %s: %s' % (field.name, help_text))

    #throws = [
            #':throws devilry.apps.core.models.Node.DoesNotExist:',
            #'   If the node with ``idorkw`` does not exists, or if',
            #'   parentnode is not None, and no node with ``id==parentnode_id``',
            #'   exists.']

    get_doc = '\n'.join(
            ['Get a %(modelname)s object.'] + ['\n\n'] + resultfields)
    modelname = meta.model.__name__
    return get_doc % vars()


def _parse_fieldgroups(fieldlst, fieldgroups):
    if isinstance(fieldlst, dict):
        base = fieldlst.get('__BASE__', [])
        fields = list(base)
        for group in fieldlst.keys():
            fields.extend(fieldlst.get(group, []))
        return fields
    else:
        return fieldlst


def _writeauth_get(cls, user, idorkw):
    if isinstance(idorkw, dict):
        kw = idorkw
    else:
        kw = dict(pk=idorkw)
    obj = cls._meta.model.objects.get(**kw)
    cls.write_authorize(user, obj)
    return obj

def _readauth_get(cls, user, idorkw):
    if isinstance(idorkw, dict):
        kw = idorkw
    else:
        kw = dict(pk=idorkw)
    obj = cls._meta.model.objects.get(**kw)
    cls.read_authorize(user, obj)
    return obj


def _recurse_getmodelattr(instance, path):
    cur = getattr(instance, path.pop(0))
    if not path:
        return cur
{'fraction_non_alphanumeric': 0.11413404114134042, 'fraction_numerical': 0.0008847600088476001, 'mean_word_length': 2.4865073245952196, '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': '7373962', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1418, 'n_words': 340}