B
    `                 @   s   d dl Z d dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZ dZe	eG dd deZG d	d
 d
eZG dd dZG dd dZdd Zdd ZdS )    N)getmtimegetsize)Response)implementer)	IResponseIResponseFactoryi   c               @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/response.pyr      s   r   c                   s"   e Zd ZdZd fdd	Z  ZS )FileResponsea  
    A Response object that can be used to serve a static file from disk
    simply.

    ``path`` is a file path on disk.

    ``request`` must be a Pyramid :term:`request` object.  Note
    that a request *must* be passed if the response is meant to attempt to
    use the ``wsgi.file_wrapper`` feature of the web server that you're using
    to serve your Pyramid application.

    ``cache_max_age`` is the number of seconds that should be used
    to HTTP cache this response.

    ``content_type`` is the content_type of the response.

    ``content_encoding`` is the content_encoding of the response.
    It's generally safe to leave this set to ``None`` if you're serving a
    binary file.  This argument will be ignored if you also leave
    ``content-type`` as ``None``.
    Nc       
         s   |d krt |\}}t jd||d t|| _t|}t|d}d }|d k	rj|j}	d|	krj|	d |t}|d kr|t	|t}|| _
|| _|d k	r|| _d S )NT)Zconditional_responsecontent_typecontent_encodingrbzwsgi.file_wrapper)_guess_typesuper__init__r   Zlast_modifiedr   openenviron_BLOCK_SIZEFileIterapp_itercontent_lengthZcache_expires)
selfpathrequestZcache_max_ager   r   r   fr   r   )	__class__r   r   r   (   s(    


zFileResponse.__init__)NNNN)r   r	   r
   __doc__r   __classcell__r   r   )r   r   r      s
      r   c               @   s4   e Zd ZdZefddZdd Zdd Zdd	 Zd
S )r   zA fixed-block-size iterator for use as a WSGI app_iter.

    ``file`` is a Python file pointer (or at least an object with a ``read``
    method that takes a size hint).

    ``block_size`` is an optional block size for iteration.
    c             C   s   || _ || _d S )N)file
block_size)r   r!   r"   r   r   r   r   Q   s    zFileIter.__init__c             C   s   | S )Nr   )r   r   r   r   __iter__U   s    zFileIter.__iter__c             C   s   | j | j}|st|S )N)r!   readr"   StopIteration)r   valr   r   r   __next__X   s    zFileIter.__next__c             C   s   | j   d S )N)r!   close)r   r   r   r   r(   ^   s    zFileIter.closeN)	r   r	   r
   r   r   r   r#   r'   r(   r   r   r   r   r   H   s
   r   c               @   s,   e Zd ZdZeZdd Zdd Zdd ZdS )	response_adaptera5	  Decorator activated via a :term:`scan` which treats the function
    being decorated as a :term:`response adapter` for the set of types or
    interfaces passed as ``*types_or_ifaces`` to the decorator constructor.

    For example, if you scan the following response adapter:

    .. code-block:: python

        from pyramid.response import Response
        from pyramid.response import response_adapter

        @response_adapter(int)
        def myadapter(i):
            return Response(status=i)

    You can then return an integer from your view callables, and it will be
    converted into a response with the integer as the status code.

    More than one type or interface can be passed as a constructor argument.
    The decorated response adapter will be called for each type or interface.

    .. code-block:: python

        import json

        from pyramid.response import Response
        from pyramid.response import response_adapter

        @response_adapter(dict, list)
        def myadapter(ob):
            return Response(json.dumps(ob))

    This method will have no effect until a :term:`scan` is performed
    agains the package or module which contains it, ala:

    .. code-block:: python

        from pyramid.config import Configurator
        config = Configurator()
        config.scan('somepackage_containing_adapters')

    Two additional keyword arguments which will be passed to the
    :term:`venusian` ``attach`` function are ``_depth`` and ``_category``.

    ``_depth`` is provided for people who wish to reuse this class from another
    decorator. The default value is ``0`` and should be specified relative to
    the ``response_adapter`` invocation. It will be passed in to the
    :term:`venusian` ``attach`` function as the depth of the callstack when
    Venusian checks if the decorator is being used in a class or module
    context. It's not often used, but it can be useful in this circumstance.

    ``_category`` sets the decorator category name. It can be useful in
    combination with the ``category`` argument of ``scan`` to control which
    views should be processed.

    See the :py:func:`venusian.attach` function in Venusian for more
    information about the ``_depth`` and ``_category`` arguments.

    .. versionchanged:: 1.9.1
       Added the ``_depth`` and ``_category`` arguments.

    c             O   s,   || _ |dd| _|dd| _|| _d S )N_depthr   Z	_categoryZpyramid)types_or_ifacespopdepthcategorykwargs)r   r+   r/   r   r   r   r      s    zresponse_adapter.__init__c             C   s,   |j }x | jD ]}|j||f| j qW d S )N)configr+   Zadd_response_adapterr/   )r   scannernamewrappedr0   Ztype_or_ifacer   r   r   register   s    zresponse_adapter.registerc             C   s"   | j j|| j| j| jd d |S )N   )r.   r-   )venusianattachr4   r.   r-   )r   r3   r   r   r   __call__   s    zresponse_adapter.__call__N)r   r	   r
   r   r6   r   r4   r8   r   r   r   r   r)   b   s
   >r)   c             C   s   | j tdd d}|S )zfObtain a :class: `pyramid.response.Response` using the
    `pyramid.interfaces.IResponseFactory`.
    c             S   s   t  S )N)r   )rr   r   r   <lambda>       z'_get_response_factory.<locals>.<lambda>)default)ZqueryUtilityr   )registryZresponse_factoryr   r   r   _get_response_factory   s    r>   c             C   s&   t j| dd\}}|d krd}||fS )NF)strictzapplication/octet-stream)	mimetypes
guess_type)r   r   r   r   r   r   r      s    r   )r@   os.pathr   r   r6   Zwebobr   Z	_ResponseZzope.interfacer   Zpyramid.interfacesr   r   r   r   r   r)   r>   r   r   r   r   r   <module>   s   7W