B
    ]\3                 @   s   d Z ddl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 dd	lmZ d
d ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )aF  
    werkzeug.contrib.wrappers
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    Extra wrappers or mixins contributed by the community.  These wrappers can
    be mixed in into request objects to add extra functionality.

    Example::

        from werkzeug.wrappers import Request as RequestBase
        from werkzeug.contrib.wrappers import JSONRequestMixin

        class Request(RequestBase, JSONRequestMixin):
            pass

    Afterwards this request object provides the extra functionality of the
    :class:`JSONRequestMixin`.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N   )wsgi_decoding_dance)
BadRequest)dump_options_header)parse_options_header)cached_property)	JSONMixinc             C   s(   yt |  W n tk
r"   dS X dS )z/Checks if the given charset is known to Python.FT)codecslookupLookupError)charset r   s/private/var/folders/pf/vfcx6ndx4k5bb3vdnp68mdb40000gn/T/pip-install-6fjfx5ra/Werkzeug/werkzeug/contrib/wrappers.pyis_known_charset"   s
    r   c                   s$   e Zd ZdZe fddZ  ZS )JSONRequestMixinz
    .. deprecated:: 0.15
        Moved to :class:`werkzeug.wrappers.json.JSONMixin`. This old
        import will be removed in version 1.0.
    c                s   t jdtdd tt| jS )Nz'werkzeug.contrib.wrappers.JSONRequestMixin' has moved to 'werkzeug.wrappers.json.JSONMixin'. This old import will be removed in version 1.0.r   )
stacklevel)warningswarnDeprecationWarningsuperr   json)self)	__class__r   r   r   2   s
    zJSONRequestMixin.json)__name__
__module____qualname____doc__propertyr   __classcell__r   r   )r   r   r   +   s   r   c               @   s   e Zd ZdZdZdd ZdS )ProtobufRequestMixina  Add protobuf parsing method to a request object.  This will parse the
    input data through `protobuf`_ if possible.

    :exc:`~werkzeug.exceptions.BadRequest` will be raised if the content-type
    is not protobuf or if the data itself cannot be parsed property.

    .. _protobuf: https://github.com/protocolbuffers/protobuf

    .. deprecated:: 0.15
        This mixin will be removed in version 1.0.
    Tc             C   sx   t jdtdd d| jddkr*td| }y|| j W n tk
r\   tdY nX | j	rt|
 sttd	|S )
z.Parse the data into an instance of proto_type.zu'werkzeug.contrib.wrappers.ProtobufRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   ZprotobufCONTENT_TYPE zNot a Protobuf requestz Unable to parse Protobuf requestzPartial Protobuf request)r   r   r   environgetr   ZParseFromStringdata	Exceptionprotobuf_check_initializationZIsInitialized)r   Z
proto_typeobjr   r   r   parse_protobufR   s    z#ProtobufRequestMixin.parse_protobufN)r   r   r   r   r&   r(   r   r   r   r   r   >   s   r   c               @   sT   e Zd ZdZdd Zdd ZeeeddZ[[dd	 Zd
d Z	eee	ddZ
[[	dS )RoutingArgsRequestMixina  This request mixin adds support for the wsgiorg routing args
    `specification`_.

    .. _specification: https://wsgi.readthedocs.io/en/latest/
       specifications/routing_args.html

    .. deprecated:: 0.15
        This mixin will be removed in version 1.0.
    c             C   s"   t jdtdd | jddd S )Nzx'werkzeug.contrib.wrappers.RoutingArgsRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zwsgiorg.routing_argsr   r   )r   r   r   r"   r#   )r   r   r   r   _get_routing_argsw   s
    z)RoutingArgsRequestMixin._get_routing_argsc             C   s2   t jdtdd | jrtd|| jf| jd< d S )Nzx'werkzeug.contrib.wrappers.RoutingArgsRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zoA shallow request tried to modify the WSGI environment.  If you really want to do that, set `shallow` to False.zwsgiorg.routing_args)r   r   r   shallowRuntimeErrorrouting_varsr"   )r   valuer   r   r   _set_routing_args   s    z)RoutingArgsRequestMixin._set_routing_argsz1
        The positional URL arguments as `tuple`.)docc             C   s@   t jdtdd | jd}|d k	r,|d S i }| js<|| _|S )Nzx'werkzeug.contrib.wrappers.RoutingArgsRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zwsgiorg.routing_args   )r   r   r   r"   r#   r+   r-   )r   rvr   r   r   _get_routing_vars   s    z)RoutingArgsRequestMixin._get_routing_varsc             C   s2   t jdtdd | jrtd| j|f| jd< d S )Nzx'werkzeug.contrib.wrappers.RoutingArgsRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zoA shallow request tried to modify the WSGI environment.  If you really want to do that, set `shallow` to False.zwsgiorg.routing_args)r   r   r   r+   r,   routing_argsr"   )r   r.   r   r   r   _set_routing_vars   s    z)RoutingArgsRequestMixin._set_routing_varsz-
        The keyword URL arguments as `dict`.N)r   r   r   r   r*   r/   r   r4   r3   r5   r-   r   r   r   r   r)   k   s   

r)   c               @   s(   e Zd ZdZedd Zedd ZdS ) ReverseSlashBehaviorRequestMixina@  This mixin reverses the trailing slash behavior of :attr:`script_root`
    and :attr:`path`.  This makes it possible to use :func:`~urlparse.urljoin`
    directly on the paths.

    Because it changes the behavior or :class:`Request` this class has to be
    mixed in *before* the actual request class::

        class MyRequest(ReverseSlashBehaviorRequestMixin, Request):
            pass

    This example shows the differences (for an application mounted on
    `/application` and the request going to `/application/foo/bar`):

        +---------------+-------------------+---------------------+
        |               | normal behavior   | reverse behavior    |
        +===============+===================+=====================+
        | `script_root` | ``/application``  | ``/application/``   |
        +---------------+-------------------+---------------------+
        | `path`        | ``/foo/bar``      | ``foo/bar``         |
        +---------------+-------------------+---------------------+

    .. deprecated:: 0.15
        This mixin will be removed in version 1.0.
    c             C   s6   t jdtdd t| jdp d| j| j}|dS )zRequested path as unicode.  This works a bit like the regular path
        info in the WSGI environment but will not include a leading slash.
        z'werkzeug.contrib.wrappers.ReverseSlashBehaviorRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   Z	PATH_INFOr!   /)	r   r   r   r   r"   r#   r   encoding_errorslstrip)r   pathr   r   r   r:      s    z%ReverseSlashBehaviorRequestMixin.pathc             C   s:   t jdtdd t| jdp d| j| j}|dd S )z8The root path of the script includling a trailing slash.z'werkzeug.contrib.wrappers.ReverseSlashBehaviorRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   ZSCRIPT_NAMEr!   r7   )	r   r   r   r   r"   r#   r   r8   rstrip)r   r:   r   r   r   script_root   s    z,ReverseSlashBehaviorRequestMixin.script_rootN)r   r   r   r   r   r:   r<   r   r   r   r   r6      s   r6   c               @   s(   e Zd ZdZdZdd Zedd ZdS )DynamicCharsetRequestMixina  "If this mixin is mixed into a request class it will provide
    a dynamic `charset` attribute.  This means that if the charset is
    transmitted in the content type headers it's used from there.

    Because it changes the behavior or :class:`Request` this class has
    to be mixed in *before* the actual request class::

        class MyRequest(DynamicCharsetRequestMixin, Request):
            pass

    By default the request object assumes that the URL charset is the
    same as the data charset.  If the charset varies on each request
    based on the transmitted data it's not a good idea to let the URLs
    change based on that.  Most browsers assume either utf-8 or latin1
    for the URLs if they have troubles figuring out.  It's strongly
    recommended to set the URL charset to utf-8::

        class MyRequest(DynamicCharsetRequestMixin, Request):
            url_charset = 'utf-8'

    .. deprecated:: 0.15
        This mixin will be removed in version 1.0.

    .. versionadded:: 0.6
    latin1c             C   s   dS )aK  Called if a charset was provided but is not supported by
        the Python codecs module.  By default latin1 is assumed then
        to not lose any information, you may override this method to
        change the behavior.

        :param charset: the charset that was not found.
        :return: the replacement charset.
        r>   r   )r   r   r   r   r   unknown_charset!  s    	z*DynamicCharsetRequestMixin.unknown_charsetc             C   sV   t jdtdd | jd}|rPt|\}}|d}|rPt|rF|S | |S | jS )z"The charset from the content type.z{'werkzeug.contrib.wrappers.DynamicCharsetRequestMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   r    r   )	r   r   r   r"   r#   r   r   r?   default_charset)r   headerctoptionsr   r   r   r   r   ,  s    

z"DynamicCharsetRequestMixin.charsetN)r   r   r   r   r@   r?   r   r   r   r   r   r   r=      s   r=   c               @   s6   e Zd ZdZdZdd Zdd ZeeeddZ[[d	S )
DynamicCharsetResponseMixina  If this mixin is mixed into a response class it will provide
    a dynamic `charset` attribute.  This means that if the charset is
    looked up and stored in the `Content-Type` header and updates
    itself automatically.  This also means a small performance hit but
    can be useful if you're working with different charsets on
    responses.

    Because the charset attribute is no a property at class-level, the
    default value is stored in `default_charset`.

    Because it changes the behavior or :class:`Response` this class has
    to be mixed in *before* the actual response class::

        class MyResponse(DynamicCharsetResponseMixin, Response):
            pass

    .. deprecated:: 0.15
        This mixin will be removed in version 1.0.

    .. versionadded:: 0.6
    zutf-8c             C   s@   t jdtdd | jd}|r:t|d d}|r:|S | jS )Nz|'werkzeug.contrib.wrappers.DynamicCharsetResponseMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zcontent-typer1   r   )r   r   r   headersr#   r   r@   )r   rA   r   r   r   r   _get_charset\  s    z(DynamicCharsetResponseMixin._get_charsetc             C   sP   t jdtdd | jd}t|\}}|s4td||d< t||| jd< d S )Nz|'werkzeug.contrib.wrappers.DynamicCharsetResponseMixin' is deprecated as of version 0.15 and will be removed in version 1.0.r   )r   zcontent-typez5Cannot set charset if Content-Type header is missing.r   zContent-Type)r   r   r   rE   r#   r   	TypeErrorr   )r   r   rA   rB   rC   r   r   r   _set_charsetk  s    z(DynamicCharsetResponseMixin._set_charsetzj
        The charset for the response.  It's stored inside the
        Content-Type header as a parameter.)r0   N)	r   r   r   r   r@   rF   rH   r   r   r   r   r   r   rD   A  s   rD   )r   r	   r   _compatr   
exceptionsr   httpr   r   utilsr   Zwrappers.jsonr   Z
_JSONMixinr   r   objectr   r)   r6   r=   rD   r   r   r   r   <module>   s   	-W<C