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

from mongoengine.base import BaseDocument
import marshmallow as ma
from marshmallow.compat import with_metaclass
from marshmallow_mongoengine.convert import ModelConverter


class SchemaOpts(ma.SchemaOpts):
    """Options class for `ModelSchema`.
    Adds the following options:

    - ``model``: The Mongoengine Document model to generate the `Schema`
        from (required).
    - ``model_fields_kwargs``: Dict of {field: kwargs} to provide as
        additionals argument during fields creation.
    - ``model_build_obj``: If true, :Schema load: returns a :model: objects
        instead of a dict (default: True).
    - ``model_converter``: `ModelConverter` class to use for converting the
        Mongoengine Document model to marshmallow fields.
    - ``model_dump_only_pk``: If the document autogenerate it primary_key
        (default behaviour in Mongoengine), ignore it from the incomming data
        (default: False)
    """

    def __init__(self, meta):
        super(SchemaOpts, self).__init__(meta)
        self.model = getattr(meta, 'model', None)
        if self.model and not issubclass(self.model, BaseDocument):
            raise ValueError("`model` must be a subclass of mongoengine.base.BaseDocument")
        self.model_fields_kwargs = getattr(meta, 'model_fields_kwargs', {})
        self.model_dump_only_pk = getattr(
            meta, 'model_dump_only_pk', False)
        self.model_converter = getattr(meta, 'model_converter', ModelConverter)
        self.model_build_obj = getattr(meta, 'model_build_obj', True)


class SchemaMeta(ma.schema.SchemaMeta):
    """Metaclass for `ModelSchema`."""

    # override SchemaMeta
    @classmethod
    def get_declared_fields(mcs, klass, *args, **kwargs):
        """Updates declared fields with fields converted from the
        Mongoengine model passed as the `model` class Meta option.
        """
        declared_fields = kwargs.get('dict_class', dict)()
        # Generate the fields provided through inheritance
        opts = klass.opts
        model = getattr(opts, 'model', None)
        if model:
            converter = opts.model_converter()
            declared_fields.update(converter.fields_for_model(
                model,
                fields=opts.fields
            ))
        # Generate the fields provided in the current class
        base_fields = super(SchemaMeta, mcs).get_declared_fields(
            klass, *args, **kwargs
        )
        declared_fields.update(base_fields)
        # Customize fields with provided kwargs
        for field_name, field_kwargs in klass.opts.model_fields_kwargs.items():
            field = declared_fields.get(field_name, None)
            if field:
                # Copy to prevent alteration of a possible parent class's field
                field = copy.copy(field)
                for key, value in field_kwargs.items():
                    setattr(field, key, value)
                declared_fields[field_name] = field
        if opts.model_dump_only_pk and opts.model:
            # If primary key is automatically generated (nominal case), we
            # must make sure this field is read-only
            if opts.model._auto_id_field is True:
                field_name = opts.model._meta['id_field']
                id_field = declared_fields.get(field_name)
                if id_field:
                    # Copy to prevent alteration of a possible parent class's field
                    id_field = copy.copy(id_field)
                    id_field.dump_only = True
                    declared_fields[field_name] = id_field
        return declared_fields


class ModelSchema(with_metaclass(SchemaMeta, ma.Schema)):
    """Base class for Mongoengine model-based Schemas.

    Example: ::

        from marshmallow_mongoengine import ModelSchema
        from mymodels import User

        class UserSchema(ModelSchema):
            class Meta:
                model = User
    """
    OPTIONS_CLASS = SchemaOpts

    def make_object(self, data):
        if self.opts.model_build_obj:
            return self.opts.model(**data)
        else:
            return data<commit_msg>Fix make_object crash when model is missing<commit_after># -*- coding: utf-8 -*-
import copy

from mongoengine.base import BaseDocument
import marshmallow as ma
from marshmallow.compat import with_metaclass
from marshmallow_mongoengine.convert import ModelConverter


class SchemaOpts(ma.SchemaOpts):
    """Options class for `ModelSchema`.
    Adds the following options:

    - ``model``: The Mongoengine Document model to generate the `Schema`
        from (required).
    - ``model_fields_kwargs``: Dict of {field: kwargs} to provide as
        additionals argument during fields creation.
    - ``model_build_obj``: If true, :Schema load: returns a :model: objects
        instead of a dict (default: True).
    - ``model_converter``: `ModelConverter` class to use for converting the
        Mongoengine Document model to marshmallow fields.
    - ``model_dump_only_pk``: If the document autogenerate it primary_key
        (default behaviour in Mongoengine), ignore it from the incomming data
        (default: False)
    """

    def __init__(self, meta):
        super(SchemaOpts, self).__init__(meta)
        self.model = getattr(meta, 'model', None)
        if self.model and not issubclass(self.model, BaseDocument):
            raise ValueError("`model` must be a subclass of mongoengine.base.BaseDocument")
        self.model_fields_kwargs = getattr(meta, 'model_fields_kwargs', {})
        self.model_dump_only_pk = getattr(
            meta, 'model_dump_only_pk', False)
        self.model_converter = getattr(meta, 'model_converter', ModelConverter)
        self.model_build_obj = getattr(meta, 'model_build_obj', True)


class SchemaMeta(ma.schema.SchemaMeta):
    """Metaclass for `ModelSchema`."""

    # override SchemaMeta
    @classmethod
    def get_declared_fields(mcs, klass, *args, **kwargs):
        """Updates declared fields with fields converted from the
        Mongoengine model passed as the `model` class Meta option.
        """
        declared_fields = kwargs.get('dict_class', dict)()
        # Generate the fields provided through inheritance
        opts = klass.opts
        model = getattr(opts, 'model', None)
        if model:
            converter = opts.model_converter()
            declared_fields.update(converter.fields_for_model(
                model,
                fields=opts.fields
            ))
        # Generate the fields provided in the current class
        base_fields = super(SchemaMeta, mcs).get_declared_fields(
            klass, *args, **kwargs
        )
        declared_fields.update(base_fields)
        # Customize fields with provided kwargs
        for field_name, field_kwargs in klass.opts.model_fields_kwargs.items():
            field = declared_fields.get(field_name, None)
            if field:
                # Copy to prevent alteration of a possible parent class's field
                field = copy.copy(field)
                for key, value in field_kwargs.items():
                    setattr(field, key, value)
                declared_fields[field_name] = field
        if opts.model_dump_only_pk and opts.model:
            # If primary key is automatically generated (nominal case), we
            # must make sure this field is read-only
            if opts.model._auto_id_field is True:
                field_name = opts.model._meta['id_field']
                id_field = declared_fields.get(field_name)
                if id_field:
                    # Copy to prevent alteration of a possible parent class's field
                    id_field = copy.copy(id_field)
                    id_field.dump_only = True
                    declared_fields[field_name] = id_field
        return declared_fields


class ModelSchema(with_metaclass(SchemaMeta, ma.Schema)):
    """Base class for Mongoengine model-based Schemas.

    Example: ::

        from marshmallow_mongoengine import ModelSchema
        from mymodels import User

        class UserSchema(ModelSchema):
            class Meta:
                model = User
    """
    OPTIONS_CLASS = SchemaOpts

    def make_object(self, data):
        if self.opts.model_build_obj and self.opts.model:
            return self.opts.model(**data)
        else:
            return data
{'fraction_non_alphanumeric': 0.08117114972625565, 'fraction_numerical': 0.00023803856224708403, 'mean_word_length': 2.4256013045250713, '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': '648654', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2228, 'n_words': 667}