o
    ȳg.                     @  s2  d Z ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ e
jrWdd
lmZ dZdZdZeeehZG dd dZ e  Z!duddZ"duddZ#duddZ$duddZ%duddZ&dvddZdwd!d"Z'dxd%d&Z(dyd'd(Z)e	*d)Z+e	*d*Z,e	*d+Z-dzd/d0Z.d1d2 Z/d3d4 Z0d5d6 Z1d{d9d:Z2d{d;d<Z3d|d>d?Z4d|d@dAZ5dydBdCZ6d}dFdGZ7d~dJdKZ8ddNdOZ9ddSdTZ:e!fddVdWZ;dXdY Z<dZd[ Z=dd^d_Z>d`da Z?ddedfZ@ddgdhZAddldmZBddpdqZCddsdtZDdS )z Utility methods for marshmallow.    )annotationsN)Mapping)format_datetimeparsedate_to_datetime)pprint)FieldABC)FieldInstanceResolutionError)RemovedInMarshmallow4Warning)Fieldexcludeincluderaisec                   @  s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_Missingc                 C     dS )NF selfr   r   M/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/marshmallow/utils.py__bool__!      z_Missing.__bool__c                 C     | S Nr   r   r   r   r   __copy__$   r   z_Missing.__copy__c                 C  r   r   r   )r   _r   r   r   __deepcopy__'   r   z_Missing.__deepcopy__c                 C  r   )Nz<marshmallow.missing>r   r   r   r   r   __repr__*   r   z_Missing.__repr__N)__name__
__module____qualname__r   r   r   r   r   r   r   r   r       s
    r   returnboolc                 C  s   t | p	t | S )z%Return True if ``obj`` is a generator)inspectisgeneratorfunctionisgeneratorobjr   r   r   is_generator4      r&   c                 C  s   t | drt | d pt| S )zAReturn True if ``obj`` is an iterable object that isn't a string.__iter__strip)hasattrr&   r$   r   r   r   is_iterable_but_not_string9   s   r+   c                 C  s   t | o	t| t S )zGReturn True if ``obj`` is a collection type, e.g list, tuple, queryset.)r+   
isinstancer   r$   r   r   r   is_collection>   r'   r-   c                 C  s*   zt | |W S  ty   t| | Y S w )zFReturn True if ``val`` is either a subclass or instance of ``class_``.)
issubclass	TypeErrorr,   )valclass_r   r   r   is_instance_or_subclassC   s
   r2   c                 C  s   t | to	t| dS )zjReturn True if ``obj`` has keyed tuple behavior, such as
    namedtuples or SQLAlchemy's KeyedTuples.
    _fields)r,   tupler*   r$   r   r   r   is_keyed_tupleK   s   r5   Nonec                 O  sV   t jdtdd t| tjrttj| g|R i | dS t	| g|R i | dS )zPretty-printing function that can pretty-print OrderedDicts
    like regular dictionaries. Useful for printing the output of
    :meth:`marshmallow.Schema.dump`.

    .. deprecated:: 3.7.0
        marshmallow.pprint will be removed in marshmallow 4.
    zQmarshmallow's pprint function is deprecated and will be removed in marshmallow 4.   )
stacklevelN)
warningswarnr	   r,   collectionsOrderedDictprintjsondumps	py_pprint)r%   argskwargsr   r   r   r   R   s    r   datetimedt.datetimec                 C  s   | j d uo| j | d uS r   )tzinfo	utcoffsetrC   r   r   r   is_awaref   s   rH   
datestringstrc                 C     t | S )zParse a RFC822-formatted datetime string and return a datetime object.

    https://stackoverflow.com/questions/885015/how-to-parse-a-rfc-2822-date-time-into-a-python-datetime  # noqa: B950
    )r   )rI   r   r   r   from_rfcl      rL   c                 C  rK   )ziReturn the RFC822-formatted representation of a datetime object.

    :param datetime: The datetime.
    )r   rG   r   r   r   	rfcformatt   rM   rN   z(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$z4(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$zd(?P<hour>\d{1,2}):(?P<minute>\d{1,2})(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?offsetfloat | dt.timedeltadt.timezonec                 C  sX   t | tjr|  d } | dk rdnd}djtt| d }|| }ttj| d|S )z6Return a tzinfo instance with a fixed offset from UTC.<   r   -+z{:02d}{:02d})minutes)r,   dt	timedeltatotal_secondsformatdivmodabstimezone)rO   signhhmmnamer   r   r   get_fixed_timezone   s   r`   c                 C  s   t | }|std| }|d o|d dd|d< |d}|dkr+tjj}n-|durXt	|dkr=t
|d	d nd
}dt
|dd  | }|d
 dkrT| }t|}dd | D }||d< tjdi |S )zParse a string and return a datetime.datetime.

    This function supports time zone offsets. When the input contains one,
    the output uses a timezone with a fixed offset from UTC.
    z-Not a valid ISO8601-formatted datetime stringmicrosecond   0rE   ZN   r   rR      rS   c                 S  "   i | ]\}}|d ur|t |qS r   int.0kvr   r   r   
<dictcomp>      " z%from_iso_datetime.<locals>.<dictcomp>r   )_iso8601_datetime_rematch
ValueError	groupdictljustpoprV   r\   utclenrj   r`   itemsrC   )valuerr   kwrE   offset_minsrO   r   r   r   from_iso_datetime   s"   


 r}   c                 C  s\   t | }|std| }|d o|d dd|d< dd | D }tjdi |S )	zeParse a string and return a datetime.time.

    This function doesn't support time zone offsets.
    z)Not a valid ISO8601-formatted time stringra   rb   rc   c                 S  rh   r   ri   rk   r   r   r   ro      rp   z!from_iso_time.<locals>.<dictcomp>Nr   )_iso8601_time_rerr   rs   rt   ru   ry   rV   timerz   rr   r{   r   r   r   from_iso_time   s   
r   c                 C  s<   t | }|stddd |  D }tjdi |S )z*Parse a string and return a datetime.date.z)Not a valid ISO8601-formatted date stringc                 S  s   i | ]	\}}|t |qS r   ri   rk   r   r   r   ro      s    z!from_iso_date.<locals>.<dictcomp>Nr   )_iso8601_date_rerr   rs   rt   ry   rV   dater   r   r   r   from_iso_date   s
   
r   rz   
typing.Anyc              
   C  s   | du s| du rt dt| } | dk rt dztjj| tjjdjd dW S  ty8 } zt d|d }~w t	yH } zt d|d }~ww )	NTFzNot a valid POSIX timestampr   )tzrE   zTimestamp is too largez"Error converting value to datetime)
rs   floatrV   rC   fromtimestampr\   rw   replaceOverflowErrorOSError)rz   excr   r   r   from_timestamp   s   

r   c                 C  s   t | } t| d S Ni  )r   r   rz   r   r   r   from_timestamp_ms   s   r   r   c                 C  s    t | s| jtjjd} |  S )Nr   )rH   r   rV   r\   rw   	timestampr   r   r   r   r      s   r   c                 C  s   t | d S r   )r   r   r   r   r   timestamp_ms      r   c                 C  s   |   S )zjReturn the ISO8601-formatted representation of a datetime object.

    :param datetime: The datetime.
    )	isoformatrG   r   r   r   r      rM   r   r   dt.timec                 C     t j| S r   )rV   r   r   )r   r   r   r   to_iso_time   r   r   r   dt.datec                 C  r   r   )rV   r   r   )r   r   r   r   to_iso_date   r   r   r0   str | bytesc                 C  s   t | tr
| d} t| S )Nzutf-8)r,   bytesdecoderJ   )r0   r   r   r   ensure_text_type   s   

r   dictlistlist[dict[str, typing.Any]]keyc                   s    fdd| D S )zExtracts a list of dictionary values from a list of dictionaries.
    ::

        >>> dlist = [{'id': 1, 'name': 'foo'}, {'id': 2, 'name': 'bar'}]
        >>> pluck(dlist, 'id')
        [1, 2]
    c                   s   g | ]}|  qS r   r   )rl   dr   r   r   
<listcomp>  s    zpluck.<locals>.<listcomp>r   )r   r   r   r   r   pluck   s   r   	int | strc                 C  s0   t |tsd|v rt| |d|S t| ||S )a  Helper for pulling a keyed value off various types of objects. Fields use
    this method by default to access attributes of the source object. For object `x`
    and attribute `i`, this method first tries to access `x[i]`, and then falls back to
    `x.i` if an exception is raised.

    .. warning::
        If an object `x` does not raise an exception when `x[i]` does not exist,
        `get_value` will never check the value `x.i`. Consider overriding
        `marshmallow.fields.Field.get_value` in this case.
    .)r,   rj   _get_value_for_keyssplit_get_value_for_keyr%   r   defaultr   r   r   	get_value  s   r   c                 C  s<   t |dkrt| |d |S tt| |d ||dd  |S )Nrg   r   )rx   r   r   )r%   keysr   r   r   r   r     s
   r   c                 C  sH   t | dst| ||S z| | W S  ttttfy#   t| || Y S w )N__getitem__)r*   getattrKeyError
IndexErrorr/   AttributeErrorr   r   r   r   r   &  s   

r   dctdict[str, typing.Any]c                 C  sd   d|v r,| dd\}}| |i }t|ts$td| d| d| t||| dS || |< dS )zSet a value in a dict. If `key` contains a '.', it is assumed
    be a path (i.e. dot-delimited string) to the value's location.

    ::

        >>> d = {}
        >>> set_value(d, 'foo.bar', 42)
        >>> d
        {'foo': {'bar': 42}}
    r   rg   zCannot set z in z due to existing value: N)r   
setdefaultr,   dictrs   	set_value)r   r   rz   headresttargetr   r   r   r   0  s   
r   c                 C  s   t | std| d| S )z@Check that an object is callable, else raise a :exc:`TypeError`.Object z is not callable.)callabler/   r$   r   r   r   callable_or_raiseG  s   r   functyping.Callable	list[str]c                 C  s   t t| j S r   )listr!   	signature
parametersr   r   r   r   r   
_signatureN  s   r   c                 C  s:   t | s
t | rt| S t| tjrt| jS t| S )zGiven a callable, return a list of argument names. Handles
    `functools.partial` objects and class-based callables.

    .. versionchanged:: 3.0.0a1
        Do not return bound arguments, eg. ``self``.
    )r!   
isfunctionismethodr   r,   	functoolspartialr   r   r   r   r   get_func_argsR  s
   
r   cls_or_instancetype[Field] | Fieldr
   c                 C  s0   t | trt| tst|  S t | tst| S )zrReturn a field instance from a field class or instance.

    :param cls_or_instance: Field class or instance.
    )r,   typer.   r   r   )r   r   r   r   resolve_field_instancea  s   


r   dt.timedeltarj   c                 C  s   | j d | j d | j S )zCompute the total microseconds of a timedelta

    https://github.com/python/cpython/blob/bb3e0c240bc60fe08d332ff5955d54197f79751c/Lib/datetime.py#L665-L667  # noqa: B950
    iQ i@B )dayssecondsmicrosecondsr   r   r   r   timedelta_to_microsecondso  s   r   r%   c                 C  s   | t vrtd| d| S )Nr   z1 is not a valid value for the 'unknown' parameter)_UNKNOWN_VALUESrs   r$   r   r   r    validate_unknown_parameter_valuew  s
   
r   )r   r    )r   r6   )rC   rD   r   r    )rI   rJ   r   rD   )rC   rD   r   rJ   )rO   rP   r   rQ   )rz   r   r   rD   )rz   rD   r   r   )r   r   r   rJ   )r   r   r   rJ   )r0   r   r   rJ   )r   r   r   rJ   )r   r   )r   r   r   rJ   rz   r   )r   r   r   r   )r   r   r   r
   )rz   r   r   rj   )r%   r   r   rJ   )E__doc__
__future__r   r;   rC   rV   r   r!   r>   retypingr9   collections.abcr   email.utilsr   r   r   r@   marshmallow.baser   marshmallow.exceptionsr   marshmallow.warningsr	   TYPE_CHECKINGmarshmallow.fieldsr
   EXCLUDEINCLUDERAISEr   r   missingr&   r+   r-   r2   r5   rH   rL   rN   compilerq   r   r~   r`   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s~    














	


	










