o
    iY9                     @   s   d dl mZ zd dlmZ W n ey   d dlmZ Y nw d dlmZmZ d dl	m
Z
mZ d dlmZ d dlZd dlZd dlZG dd deZd	d
ddddddZdd ZG dd deZG dd deZdS )    )deepcopy)MutableSequence)current_apprequest)	MultiDictFileStorage)
exceptionsNc                   @   s   e Zd Zdd Zdd ZdS )	Namespacec                 C   s"   z| | W S  t y   t|w N)KeyErrorAttributeError)selfname r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/flask_restful/reqparse.py__getattr__   s
   
zNamespace.__getattr__c                 C   s   || |< d S r
   r   )r   r   valuer   r   r   __setattr__   s   zNamespace.__setattr__N)__name__
__module____qualname__r   r   r   r   r   r   r	      s    r	   zthe JSON bodyzthe post bodyzthe query stringz!the post body or the query stringzthe HTTP headerszthe request's cookieszan uploaded file)jsonformargsvaluesheaderscookiesfilesc                 C   s
   t | S r
   )six	text_type)xr   r   r   <lambda>$   s   
 r!   c                   @   sh   e Zd ZdZddddedddddddddfd	d
Zdd Zdd Zdd Zdd Z	dd Z
dddZdS )Argumenta  
    :param name: Either a name or a list of option strings, e.g. foo or
        -f, --foo.
    :param default: The value produced if the argument is absent from the
        request.
    :param dest: The name of the attribute to be added to the object
        returned by :meth:`~reqparse.RequestParser.parse_args()`.
    :param bool required: Whether or not the argument may be omitted (optionals
        only).
    :param action: The basic type of action to be taken when this argument
        is encountered in the request. Valid options are "store" and "append".
    :param ignore: Whether to ignore cases where the argument fails type
        conversion
    :param type: The type to which the request argument should be
        converted. If a type raises an exception, the message in the
        error will be returned in the response. Defaults to :class:`unicode`
        in python2 and :class:`str` in python3.
    :param location: The attributes of the :class:`flask.Request` object
        to source the arguments from (ex: headers, args, etc.), can be an
        iterator. The last item listed takes precedence in the result set.
    :param choices: A container of the allowable values for the argument.
    :param help: A brief description of the argument, returned in the
        response when the argument is invalid. May optionally contain
        an "{error_msg}" interpolation token, which will be replaced with
        the text of the error raised by the type converter.
    :param bool case_sensitive: Whether argument values in the request are
        case sensitive or not (this will convert all values to lowercase)
    :param bool store_missing: Whether the arguments default value should
        be stored if the argument is missing from the request.
    :param bool trim: If enabled, trims whitespace around the argument.
    :param bool nullable: If enabled, allows null value in argument.
    NF)r   r   r   store)=Tc                 C   s^   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _|| _|| _|| _d S r
   )r   defaultdestrequiredignorelocationtypechoicesactionhelpcase_sensitive	operatorsstore_missingtrimnullable)r   r   r%   r&   r'   r(   r*   r)   r+   r,   r-   r/   r.   r0   r1   r2   r   r   r   __init__J   s   
zArgument.__init__c                 C   sP   t | jdkr| jdd }|d || jd  n| j}d| j| j|S )N   r      z...z"Name: {0}, type: {1}, choices: {2})lenr+   appendformatr   r*   )r   r+   r   r   r   __str___   s   
zArgument.__str__c                 C   sJ   d | jj| j| j| j| j| j| j| j	| j
| j| j| j| j| j| j| jS )Nz{0}('{1}', default={2}, dest={3}, required={4}, ignore={5}, location={6}, type="{7}", choices={8}, action='{9}', help={10}, case_sensitive={11}, operators={12}, store_missing={13}, trim={14}, nullable={15}))r9   	__class__r   r   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   )r   r   r   r   __repr__h   s   zArgument.__repr__c                 C   s   t | jtjrt|| jt }t|r| }|dur|S t S t }| jD ]}t||d}t|r4| }|dur=|| q%|S )zPulls values off the request in the provided location
        :param request: The flask request object to parse arguments from
        N)
isinstancer)   r   string_typesgetattrr   callableupdate)r   r   r   r   lr   r   r   sourcep   s    

zArgument.sourcec                 C   s   |d u r| j r	d S tdt|tr| jtkr|S z	| || j|W S  tyT   z| jtju r:| t	|W  Y S | || jW  Y S  tyS   | | Y  Y S w w )NzMust not be null!)
r2   
ValueErrorr=   r   r*   r   	TypeErrordecimalDecimalstr)r   r   opr   r   r   convert   s"   zArgument.convertc                 C   sX   t |}| jr| jj|dn|}| j|i}tjdds|r#||fS tj	d|d dS )aU  Called when an error is raised while parsing. Aborts the request
        with a 400 status and an error message

        :param error: the error that was raised
        :param bundle_errors: do not abort when first error occurs, return a
            dict with the name of the argument and the error message to be
            bundled
        )	error_msgBUNDLE_ERRORSF  messageN)
r   r   r-   r9   r   r   configgetflask_restfulabort)r   errorbundle_errors	error_strrK   msgr   r   r   handle_validation_error   s   
	
z Argument.handle_validation_errorc                 C   s  |  |}g }d}d}| jD ]}| j|ddd }||v rt|dr)||}	n||}	t|	tr8| j	dks;|	g}	|	D ]}
t|
drK| j
rK|
 }
t|
d	rf| jsf|
 }
t| jd
rfdd | jD | _z| |
|}
W n% ty } z| jrW Y d}~q=| ||W  Y d}~    S d}~ww | jr|
| jvrtjdds|r| td|
|    S | td|
| ||jv r|j| ||
 q=q|s| jrt| jtjrdt| j| j}ndd | jD }dd|}tjdds|r| t||S | t|| |s*t | j!r%| ! |fS | j!|fS | j	dkr4||fS | j	dksAt"|dkrG|d |fS ||fS )a^  Parses argument value(s) from the request, converting according to
        the argument's type.

        :param request: The flask request object to parse arguments from
        :param bundle_errors: Do not abort when first error occurs, return a
            dict with the name of the argument and the error message to be
            bundled
        FTr$       getlistr8   striplower__iter__c                 S   s   g | ]}|  qS r   )r]   ).0choicer   r   r   
<listcomp>   s    z"Argument.parse.<locals>.<listcomp>NrL   z{0} is not a valid choicez!Missing required parameter in {0}c                 S   s   g | ]}t ||qS r   )_friendly_locationrQ   )r_   locr   r   r   ra      s    z or r#   r   )#rC   r/   r   replacehasattrr[   rQ   r=   r   r,   r1   r\   r.   r]   r+   rJ   	Exceptionr(   rX   r   rP   rD   r9   unparsed_argumentspopr8   r'   r)   r   r>   rb   joinr@   r%   r7   )r   r   rU   rC   results
_not_found_foundoperatorr   r   r   rT   rK   friendly_locationsr   r   r   parse   s   
	


 

zArgument.parse)F)r   r   r   __doc__r   r3   r:   r<   rC   rJ   rX   ro   r   r   r   r   r"   '   s    !
	r"   c                   @   sL   e Zd ZdZeeddfddZdd Zdd	d
Zdd Z	dd Z
dd ZdS )RequestParsera^  Enables adding and parsing of multiple arguments in the context of a
    single request. Ex::

        from flask_restful import reqparse

        parser = reqparse.RequestParser()
        parser.add_argument('foo')
        parser.add_argument('int_bar', type=int)
        args = parser.parse_args()

    :param bool trim: If enabled, trims whitespace on all arguments in this
        parser
    :param bool bundle_errors: If enabled, do not abort when first error occurs,
        return a dict with the name of the argument and the error message to be
        bundled and return all validation errors
    Fc                 C   s"   g | _ || _|| _|| _|| _d S r
   )r   argument_classnamespace_classr1   rU   )r   rr   rs   r1   rU   r   r   r   r3     s
   
zRequestParser.__init__c                 O   sp   t |dkrt|d | jr| j|d  n| j| j|i | | jr6| jtu r6|d| j| jd _| S )a  Adds an argument to be parsed.

        Accepts either a single instance of Argument or arguments to be passed
        into :class:`Argument`'s constructor.

        See :class:`Argument`'s constructor for documentation on the
        available options.
        rZ   r   r1   r6   )r7   r=   rr   r   r8   r1   r"   rQ   )r   r   kwargsr   r   r   add_argument   s   
zRequestParser.add_argumentNrM   c           	      C   s   |du rt }|  }|rt| d|ni |_i }| jD ]$}||| j\}}t	|t
r5|| d}|s:|jrB|||jpA|j< q|rLtj||d |r^|jr^tdd|j  |S )aQ  Parse all arguments from the provided request and return the results
        as a Namespace

        :param req: Can be used to overwrite request from Flask
        :param strict: if req includes args not in parser, throw 400 BadRequest exception
        :param http_error_code: use custom error code for `flask_restful.abort()`
        NrY   rN   zUnknown arguments: %sz, )r   rs   dictrr   rC   rg   r   ro   rU   r=   rD   rA   r0   r&   r   rR   rS   r   
BadRequestri   keys)	r   reqstricthttp_error_code	namespaceerrorsargr   foundr   r   r   
parse_args6  s(   




zRequestParser.parse_argsc                 C   s0   |  | j| j}t| j|_| j|_| j|_|S )zE Creates a copy of this RequestParser with the same set of arguments )r;   rr   rs   r   r   r1   rU   )r   parser_copyr   r   r   copyW  s
   zRequestParser.copyc                 O   s^   | j |g|R i |}t| jdd D ]\}}|j|jkr,| j|= | j|  | S q| S )zB Replace the argument matching the given name with a new version. N)rr   	enumerater   r   r8   )r   r   r   rt   new_argindexr~   r   r   r   replace_argument_  s   zRequestParser.replace_argumentc                 C   s8   t | jdd D ]\}}||jkr| j|=  | S q	| S )z. Remove the argument matching the given name. N)r   r   r   )r   r   r   r~   r   r   r   remove_argumenti  s   
zRequestParser.remove_argument)NFrM   )r   r   r   rp   r"   r	   r3   ru   r   r   r   r   r   r   r   r   rq     s    

!
rq   )r   r   collections.abcr   ImportErrorcollectionsflaskr   r   werkzeug.datastructuresr   r   werkzeugr   rR   rF   r   rv   r	   rb   r   objectr"   rq   r   r   r   r   <module>   s2    
 `