o
    ȳg-"                     @  s   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 dZ
dZdZd	Zd
ZdZG dd dZd&dd
Z				d'd(ddZ		d)d*ddZ			d+d,ddZ		d)d*ddZ			d+d,dd	Z	d-d.d$d%ZdS )/a  Decorators for registering schema pre-processing and post-processing methods.
These should be imported from the top-level `marshmallow` module.

Methods decorated with
`pre_load <marshmallow.decorators.pre_load>`, `post_load <marshmallow.decorators.post_load>`,
`pre_dump <marshmallow.decorators.pre_dump>`, `post_dump <marshmallow.decorators.post_dump>`,
and `validates_schema <marshmallow.decorators.validates_schema>` receive
``many`` as a keyword argument. In addition, `pre_load <marshmallow.decorators.pre_load>`,
`post_load <marshmallow.decorators.post_load>`,
and `validates_schema <marshmallow.decorators.validates_schema>` receive
``partial``. If you don't need these arguments, add ``**kwargs`` to your method
signature.


Example: ::

    from marshmallow import (
        Schema,
        pre_load,
        pre_dump,
        post_load,
        validates_schema,
        validates,
        fields,
        ValidationError,
    )


    class UserSchema(Schema):
        email = fields.Str(required=True)
        age = fields.Integer(required=True)

        @post_load
        def lowerstrip_email(self, item, many, **kwargs):
            item["email"] = item["email"].lower().strip()
            return item

        @pre_load(pass_many=True)
        def remove_envelope(self, data, many, **kwargs):
            namespace = "results" if many else "result"
            return data[namespace]

        @post_dump(pass_many=True)
        def add_envelope(self, data, many, **kwargs):
            namespace = "results" if many else "result"
            return {namespace: data}

        @validates_schema
        def validate_email(self, data, **kwargs):
            if len(data["email"]) < 3:
                raise ValidationError("Email must be more than 3 characters", "email")

        @validates("age")
        def validate_age(self, data, **kwargs):
            if data < 14:
                raise ValidationError("Too young!")

.. note::
    These decorators only work with instance methods. Class and static
    methods are not supported.

.. warning::
    The invocation order of decorated methods of the same type is not guaranteed.
    If you need to guarantee order of different processing steps, you should put
    them in the same processing method.
    )annotationsN)defaultdict)AnyCallablecastpre_dump	post_dumppre_load	post_load	validatesvalidates_schemac                   @  s   e Zd ZU dZded< dS )MarshmallowHookNz(dict[str, list[tuple[bool, Any]]] | None__marshmallow_hook__)__name__
__module____qualname__r   __annotations__ r   r   R/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/marshmallow/decorators.pyr   R   s   
 r   
field_namestrreturnCallable[..., Any]c                 C  s   t dt| dS )zeRegister a field validator.

    :param field_name: Name of the field that the method validates.
    Nr   )set_hook	VALIDATESr   r   r   r   r   V   s   FTfnCallable[..., Any] | None	pass_manyboolpass_originalskip_on_field_errorsc                 C  s   t | t|||dS )a2  Register a schema-level validator.

    By default it receives a single object at a time, transparently handling the ``many``
    argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.validate` call.
    If ``pass_many=True``, the raw data (which may be a collection) is passed.

    If ``pass_original=True``, the original data (before unmarshalling) will be passed as
    an additional argument to the method.

    If ``skip_on_field_errors=True``, this validation method will be skipped whenever
    validation errors have been detected when validating fields.

    .. versionchanged:: 3.0.0b1
        ``skip_on_field_errors`` defaults to `True`.

    .. versionchanged:: 3.0.0
        ``partial`` and ``many`` are always passed as keyword arguments to
        the decorated method.
    )manyr    r!   )r   VALIDATES_SCHEMA)r   r   r    r!   r   r   r   r   ^   s   c                 C     t | t|dS )a  Register a method to invoke before serializing an object. The method
    receives the object to be serialized and returns the processed object.

    By default it receives a single object at a time, transparently handling the ``many``
    argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.dump` call.
    If ``pass_many=True``, the raw data (which may be a collection) is passed.

    .. versionchanged:: 3.0.0
        ``many`` is always passed as a keyword arguments to the decorated method.
    r"   )r   PRE_DUMPr   r   r   r   r   r      s   c                 C     t | t||dS )a  Register a method to invoke after serializing an object. The method
    receives the serialized object and returns the processed object.

    By default it receives a single object at a time, transparently handling the ``many``
    argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.dump` call.
    If ``pass_many=True``, the raw data (which may be a collection) is passed.

    If ``pass_original=True``, the original data (before serializing) will be passed as
    an additional argument to the method.

    .. versionchanged:: 3.0.0
        ``many`` is always passed as a keyword arguments to the decorated method.
    r"   r    )r   	POST_DUMPr   r   r    r   r   r   r      s   c                 C  r$   )a(  Register a method to invoke before deserializing an object. The method
    receives the data to be deserialized and returns the processed data.

    By default it receives a single object at a time, transparently handling the ``many``
    argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.load` call.
    If ``pass_many=True``, the raw data (which may be a collection) is passed.

    .. versionchanged:: 3.0.0
        ``partial`` and ``many`` are always passed as keyword arguments to
        the decorated method.
    r%   )r   PRE_LOADr'   r   r   r   r	      s   c                 C  r(   )a  Register a method to invoke after deserializing an object. The method
    receives the deserialized data and returns the processed data.

    By default it receives a single object at a time, transparently handling the ``many``
    argument passed to the `Schema <marshmallow.Schema>`'s :func:`~marshmallow.Schema.load` call.
    If ``pass_many=True``, the raw data (which may be a collection) is passed.

    If ``pass_original=True``, the original data (before deserializing) will be passed as
    an additional argument to the method.

    .. versionchanged:: 3.0.0
        ``partial`` and ``many`` are always passed as keyword arguments to
        the decorated method.
    r)   )r   	POST_LOADr+   r   r   r   r
      s   tagr"   kwargsr   c                 K  st   | du rt jtf||d|S tt| }z|j}W n ty*   tt |_}Y nw |dur8|| 	||f | S )aV  Mark decorated function as a hook to be picked up later.
    You should not need to use this method directly.

    .. note::
        Currently only works with functions and instance methods. Class and
        static methods are not supported.

    :return: Decorated function if supplied, else this decorator with its args
        bound.
    N)r.   r"   )
	functoolspartialr   r   r   r   AttributeErrorr   listappend)r   r.   r"   r/   functionhook_configr   r   r   r      s   

r   )r   r   r   r   )NFFT)
r   r   r   r   r    r   r!   r   r   r   )NF)r   r   r   r   r   r   )NFF)r   r   r   r   r    r   r   r   )F)
r   r   r.   r   r"   r   r/   r   r   r   )__doc__
__future__r   r0   collectionsr   typingr   r   r   r&   r*   r,   r-   r   r#   r   r   r   r   r   r	   r
   r   r   r   r   r   <module>   sD    C
	#