
    |hJ                     &   d Z ddlmZ ddlZddlZddlmZ ddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ  ej&                  d
ej(                        ZddZ G d dej.                  e         Z e       Zej4                  Zej6                  Zy)a  Pyramid request argument parsing.

Example usage: ::

    from wsgiref.simple_server import make_server
    from pyramid.config import Configurator
    from pyramid.response import Response
    from marshmallow import fields
    from webargs.pyramidparser import use_args

    hello_args = {"name": fields.Str(load_default="World")}


    @use_args(hello_args)
    def hello_world(request, args):
        return Response("Hello " + args["name"])


    if __name__ == "__main__":
        config = Configurator()
        config.add_route("hello", "/")
        config.add_view(hello_world, route_name="hello")
        app = config.make_wsgi_app()
        server = make_server("0.0.0.0", 6543, app)
        server.serve_forever()
    )annotationsN)Mapping)exception_response)Request)	MultiDict)core)jsonF)boundc                ^    t        j                  | j                  j                  d            S )Nzcontent-type)r   is_jsonheadersget)reqs    T/var/www/html/test/engine/venv/lib/python3.12/site-packages/webargs/pyramidparser.pyis_json_requestr   -   s    <<788    c            	         e Zd ZU dZej
                  ej
                  dej                  j                  Zde	d<    e
ddddej                  j                  ZddZddZddZdd	Zdd
ZddZddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ	 dej                  j*                  ddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)PyramidParserz Pyramid request argument parser.)	matchdictpathzdict[str, str | None]DEFAULT_UNKNOWN_BY_LOCATIONload_matchdictc                    t        |      st        j                  S t        j                  |j                  |j
                        S )zReturn a json payload from the request for the core parser's load_json

        Checks the input mimetype and may return 'missing' if the mimetype is
        non-json, even if the request body is parseable as json.)encoding)r   r   missing
parse_jsonbodycharset)selfr   s     r   _raw_load_jsonzPyramidParser._raw_load_json?   s/    
 s#<<sxx#++>>r   c                :    | j                  |j                  |      S )z9Return query params from the request as a MultiDictProxy.)
_makeproxyGETr    r   schemas      r   load_querystringzPyramidParser.load_querystringI   s    sww//r   c                :    | j                  |j                  |      S )z8Return form values from the request as a MultiDictProxy.)r#   POSTr%   s      r   	load_formzPyramidParser.load_formM   s    sxx00r   c                :    | j                  |j                  |      S )z4Return cookies from the request as a MultiDictProxy.)r#   cookiesr%   s      r   load_cookieszPyramidParser.load_cookiesQ       s{{F33r   c                :    | j                  |j                  |      S )z4Return headers from the request as a MultiDictProxy.)r#   r   r%   s      r   load_headerszPyramidParser.load_headersU   r.   r   c                z    d |j                   j                         D        }| j                  t        |      |      S )z2Return files from the request as a MultiDictProxy.c              3  D   K   | ]  \  }}t        |d       s||f  yw)fileN)hasattr).0kvs      r   	<genexpr>z+PyramidParser.load_files.<locals>.<genexpr>[   s!     KDAq68J!QKs    	 )r)   itemsr#   r   )r    r   r&   filess       r   
load_fileszPyramidParser.load_filesY   s-    KCHHNN$4Ky/88r   c                :    | j                  |j                  |      S )z7Return the request's ``matchdict`` as a MultiDictProxy.)r#   r   r%   s      r   r   zPyramidParser.load_matchdict^   s    s}}f55r   c                   |xs | j                   }t        |t        |      |d      }t        j                  |j
                        }t        |t              r|j                  d      |_        |||_        |)znHandles errors during parsing. Aborts the current HTTP request and
        responds with a 400 error.
        application/json)detailr   content_typeutf-8)	DEFAULT_VALIDATION_STATUSr   strr	   dumpsmessages
isinstanceencoder   )	r    errorr   r&   error_status_codeerror_headersstatus_coderesponser   s	            r   handle_errorzPyramidParser.handle_errorb   sp     (I4+I+I%u:!+	
 zz%..)0:40EG, LPr   c                    ddgi}t        dt        |      d      }t        j                  |      }t	        |t              r|j                  d      |_        |||_        |)Nr	   zInvalid JSON body.i  r>   )r?   r@   rA   )r   rC   r	   rD   rF   rG   r   )r    rH   r   argskwargsrE   rL   r   s           r   _handle_invalid_json_errorz(PyramidParser._handle_invalid_json_errory   sf     123%H4F
 zz(#0:40EG, LPr   NF)locationunknown	as_kwargsarg_namevalidaterI   rJ   c               2   	 xs  j                   rt        d       j                  s dt        t              r;t        t
              st                j                  j                               d	 f
d}
|
S )a@  Decorator that injects parsed arguments into a view callable.
        Supports the *Class-based View* pattern where `request` is saved as an instance
        attribute on a view class.

        :param dict argmap: Either a `marshmallow.Schema`, a `dict`
            of argname -> `marshmallow.fields.Field` pairs, or a callable
            which accepts a request and returns a `marshmallow.Schema`.
        :param req: The request object to parse. Pulled off of the view by default.
        :param str location: Where on the request to load values.
        :param str unknown: A value to pass for ``unknown`` when calling the
            schema's ``load`` method.
        :param bool as_kwargs: Whether to insert arguments as keyword arguments.
        :param str arg_name: Keyword argument name to use for arguments. Mutually
            exclusive with as_kwargs.
        :param callable validate: Validation function that receives the dictionary
            of parsed arguments. If the function returns ``False``, the parser
            will raise a :exc:`ValidationError`.
        :param int error_status_code: Status code passed to error handler functions when
            a `ValidationError` is raised.
        :param dict error_headers: Headers passed to error handler functions when a
            a `ValidationError` is raised.
        z-arg_name and as_kwargs are mutually exclusive_argsc                x   
  t        j                         	 	 	 	 	 	 	 	 d 	
fd       } |_        |S )Nc           	         	 xs | j                   }j                  |	      }j                  |||      \  }} 
| g|i |S # t        $ r | }Y Iw xY w)N)r   rR   rS   rV   rI   rJ   )requestAttributeErrorparse_update_args_kwargs)objrO   rP   r[   parsed_argsrU   argmaprT   rJ   rI   funcrR   r   r    rS   rV   s        r   wrapperz:PyramidParser.use_args.<locals>.decorator.<locals>.wrapper   s    
"!0S[[G #jj%#%&7"/ )   $77&+y( f C1$1&11 & "!G"s   A AA)r_   
typing.AnyrO   rd   rP   rd   returnrd   )	functoolswraps__wrapped__)rb   rc   rU   ra   rT   rJ   rI   rR   r   r    rS   rV   s   ` r   	decoratorz)PyramidParser.use_args.<locals>.decorator   sR    __T"22(22>H22 2 #2. #'GNr   )rb   r
   re   r
   )rR   
ValueErrorUSE_ARGS_POSITIONALrF   r   dictschema_class	from_dict)r    ra   r   rR   rS   rT   rU   rV   rI   rJ   ri   s   `````````` r   use_argszPyramidParser.use_args   s    F ,t}}ILMMD$<$<"5)H fg&fd+f8T&&008:F	 	8 r    )r   r   re   rd   )r   r   r&   	ma.Schemare   rd   )rH   zma.ValidationErrorr   r   r&   rq   rI   
int | NonerJ   typing.Mapping[str, str] | Nonere   typing.NoReturn)
rH   z)json.JSONDecodeError | UnicodeDecodeErrorr   r   rO   rd   rP   rd   re   rt   )N)ra   zcore.ArgMapr   zRequest | NonerR   
str | NonerS   ru   rT   boolrU   ru   rV   zcore.ValidateArgrI   rr   rJ   rs   re   z%typing.Callable[..., typing.Callable])__name__
__module____qualname____doc__maRAISEr   Parserr   __annotations__rl   __location_map__r!   r'   r*   r-   r0   r;   r   rM   rQ   DEFAULT_LOCATIONro   rp   r   r   r   r   1   s   * XX: ++
1
1:!6 
  " ++
&
&?01449
6!  	 & 7 
.8  	
  
$ #M
  ${{;;"#%)(,9=MM M
 M M M M #M &M 7M 
/Mr   r   )r   r   re   rv   )rz   
__future__r   rf   typingcollections.abcr   marshmallowr{   pyramid.httpexceptionsr   pyramid.requestr   webob.multidictr   webargsr   webargs.corer	   TypeVarCallabler
   r   r}   r   parserro   
use_kwargsrp   r   r   <module>r      sz   6 #   #  5 # %  FNN3foo.9dDKK( dN 
??
r   