
`Tc           @   s  d  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	 d d l
 m
 Z
 m Z d d l m Z d d l m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d	 l m Z m Z d d
 l m Z m  Z  d d l! m" Z" m# Z# m$ Z$ m% Z% d   Z& e' e' e' d d  Z) d   Z* d d  Z+ d   Z, e- d  Z. d   Z/ d d d  Z0 d d d  Z1 d d d  Z2 d d d  Z3 d d e- d  Z4 d e5 f d     YZ6 d e5 f d     YZ7 e d e5 f d     Y Z8 d  d!  Z9 e d" e5 f d#     Y Z: d$   Z; d d+ d'  Z< d d, d(  Z= e d) e5 f d*     Y Z> d S(-   s   
    werkzeug.wsgi
    ~~~~~~~~~~~~~

    This module implements WSGI related helpers.

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iN(   t   chain(   t   adler32(   t   timet   mktime(   t   datetime(   t   partialt   update_wrapper(
   t	   iteritemst	   text_typet   string_typest   implements_iteratort   make_literal_wrappert
   to_unicodet   to_bytest   wsgi_get_bytest   try_coerce_nativet   PY2(   t   _empty_streamt   _encode_idna(   t   is_resource_modifiedt	   http_date(   t
   uri_to_irit	   url_quotet	   url_parset   url_joinc            s   t    f d      S(   s
  Marks a function as responder.  Decorate a function with it and it
    will automatically call the return value as WSGI application.

    Example::

        @responder
        def application(environ, start_response):
            return Response('Hello World!')
    c             s     |    |  d   S(   Ni(    (   t   a(   t   f(    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   <lambda>(   s    (   R   (   R   (    (   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt	   responder   s    
c         C   s   |  d d t  |  |  g } | j } | rB t d j |  d  S| t t |  j d d    j d   | d  | s | t t |  j d d   j d    | s t	 |   } | r | d |  q q n  t d j |   S(   s  A handy helper function that recreates the full URL as IRI for the
    current request or parts of it.  Here an example:

    >>> from werkzeug.test import create_environ
    >>> env = create_environ("/?param=foo", "http://localhost/script")
    >>> get_current_url(env)
    'http://localhost/script/?param=foo'
    >>> get_current_url(env, root_only=True)
    'http://localhost/script/'
    >>> get_current_url(env, host_only=True)
    'http://localhost/'
    >>> get_current_url(env, strip_querystring=True)
    'http://localhost/script/'

    This optionally it verifies that the host is in a list of trusted hosts.
    If the host is not in there it will raise a
    :exc:`~werkzeug.exceptions.SecurityError`.

    Note that the string returned might contain unicode characters as the
    representation is an IRI not an URI.  If you need an ASCII only
    representation you can use the :func:`~werkzeug.urls.iri_to_uri`
    function:

    >>> from werkzeug.urls import iri_to_uri
    >>> iri_to_uri(get_current_url(env))
    'http://localhost/script/?param=foo'

    :param environ: the WSGI environment to get the current URL from.
    :param root_only: set `True` if you only want the root URL.
    :param strip_querystring: set to `True` if you don't want the querystring.
    :param host_only: set to `True` if the host URL should be returned.
    :param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted`
                          for more information.
    s   wsgi.url_schemes   ://t    t   /t   SCRIPT_NAMEt	   PATH_INFOt   ?(
   t   get_hostt   appendR   t   joinR   R   t   gett   rstript   lstript   get_query_string(   t   environt	   root_onlyt   strip_querystringt	   host_onlyt   trusted_hostst   tmpt   catt   qs(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   get_current_url+   s    $	+
+c         C   s   |  s
 t  St | t  r% | g } n  d   } | |   }  xo | D]g } | j d  ri | d } t } n t  } | |  } | |  k r t S| rA |  j d |  rA t SqA Wt  S(   sh  Checks if a host is trusted against a list.  This also takes care
    of port normalization.

    .. versionadded:: 0.9

    :param hostname: the hostname to check
    :param trusted_list: a list of hostnames to check against.  If a
                         hostname starts with a dot it will match against
                         all subdomains as well.
    c         S   s/   d |  k r% |  j  d d  d }  n  t |   S(   Nt   :i   i    (   t   rsplitR   (   t   hostname(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt
   _normalizeo   s    t   .i   (   t   Falset
   isinstanceR	   t
   startswitht   Truet   endswith(   R4   t   trusted_listR5   t   reft   suffix_match(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   host_is_trusted^   s"    	
	c         C   s   d |  k r, |  d j  d  d j   } nX d |  k rE |  d } n? |  d } |  d |  d f d d f k r | d |  d 7} n  | d k	 r t | |  s d d l m } | d |   q n  | S(   s  Return the real host for the given WSGI environment.  This takes care
    of the `X-Forwarded-Host` header.  Optionally it verifies that the host
    is in a list of trusted hosts.  If the host is not in there it will raise
    a :exc:`~werkzeug.exceptions.SecurityError`.

    :param environ: the WSGI environment to get the host of.
    :param trusted_hosts: a list of trusted hosts, see :func:`host_is_trusted`
                          for more information.
    t   HTTP_X_FORWARDED_HOSTt   ,i    t	   HTTP_HOSTt   SERVER_NAMEs   wsgi.url_schemet   SERVER_PORTt   httpst   443t   httpt   80R2   i(   t   SecurityErrors   Host "%s" is not trusted(   s   httpsRF   (   s   httpRH   N(   t   splitt   stript   NoneR?   t   werkzeug.exceptionsRI   (   R)   R-   t   rvRI   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR"      s    
 
c         C   sS   |  j  d  } | d k	 rO y t d t |   SWqO t t f k
 rK qO Xn  d S(   s   Returns the content length from the WSGI environment as
    integer.  If it's not available `None` is returned.

    .. versionadded:: 0.9

    :param environ: the WSGI environ to fetch the content length from.
    t   CONTENT_LENGTHi    N(   R%   RL   t   maxt   intt
   ValueErrort	   TypeError(   R)   t   content_length(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   get_content_length   s    c         C   sR   |  d } t  |   } |  j d  r) | S| d k rE | rA t pD | St | |  S(   sq  Returns the input stream from the WSGI environment and wraps it
    in the most sensible way possible.  The stream returned is not the
    raw WSGI stream in most cases but one that is safe to read from
    without taking into account the content length.

    .. versionadded:: 0.9

    :param environ: the WSGI environ to fetch the stream from.
    :param safe: indicates weather the function should use an empty
                 stream as safe fallback or just return the original
                 WSGI input stream if it can't wrap it safely.  The
                 default is to return an empty string in those cases.
    s
   wsgi.inputs   wsgi.input_terminatedN(   RU   R%   RL   R   t   LimitedStream(   R)   t   safe_fallbackt   streamRT   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   get_input_stream   s    
c         C   s.   t  |  j d d   } t t | d d  S(   sN  Returns the `QUERY_STRING` from the WSGI environment.  This also takes
    care about the WSGI decoding dance on Python 3 environments as a
    native string.  The string returned will be restricted to ASCII
    characters.

    .. versionadded:: 0.9

    :param environ: the WSGI environment object to get the query string from.
    t   QUERY_STRINGR   t   safes   :&%=+$!*'(),(   R   R%   R   R   (   R)   R0   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR(      s    
s   utf-8t   replacec         C   s.   t  |  j d d   } t | | | d t S(   s  Returns the `PATH_INFO` from the WSGI environment and properly
    decodes it.  This also takes care about the WSGI decoding dance
    on Python 3 environments.  if the `charset` is set to `None` a
    bytestring is returned.

    .. versionadded:: 0.9

    :param environ: the WSGI environment object to get the path from.
    :param charset: the charset for the path info, or `None` if no
                    decoding should be performed.
    :param errors: the decoding error handling.
    R    R   t   allow_none_charset(   R   R%   R   R:   (   R)   t   charsett   errorst   path(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   get_path_info   s    c         C   s.   t  |  j d d   } t | | | d t S(   s  Returns the `SCRIPT_NAME` from the WSGI environment and properly
    decodes it.  This also takes care about the WSGI decoding dance
    on Python 3 environments.  if the `charset` is set to `None` a
    bytestring is returned.

    .. versionadded:: 0.9

    :param environ: the WSGI environment object to get the path from.
    :param charset: the charset for the path, or `None` if no
                    decoding should be performed.
    :param errors: the decoding error handling.
    R   R   R]   (   R   R%   R   R:   (   R)   R^   R_   R`   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   get_script_name   s    c         C   s   |  j  d  } | s d S|  j  d d  } | } | j d  } | | k rm | d t |  t |  7} n  d | k r d |  d <| | |  d <t |  } n@ | j d d  \ } } d | |  d <| | |  d <t |  } t | | | d t S(   s  Removes and returns the next segment of `PATH_INFO`, pushing it onto
    `SCRIPT_NAME`.  Returns `None` if there is nothing left on `PATH_INFO`.

    If the `charset` is set to `None` a bytestring is returned.

    If there are empty segments (``'/foo//bar``) these are ignored but
    properly pushed to the `SCRIPT_NAME`:

    >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
    >>> pop_path_info(env)
    'a'
    >>> env['SCRIPT_NAME']
    '/foo/a'
    >>> pop_path_info(env)
    'b'
    >>> env['SCRIPT_NAME']
    '/foo/a/b'

    .. versionadded:: 0.5

    .. versionchanged:: 0.9
       The path is now decoded and a charset and encoding
       parameter can be provided.

    :param environ: the WSGI environment that is modified.
    R    R   R   R   i   R]   N(   R%   RL   R'   t   lenR   RJ   R   R:   (   R)   R^   R_   R`   t   script_namet   old_pathRN   t   segment(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   pop_path_info  s"    !
c         C   sQ   |  j  d d  j d  j d d  } | rM t t | d  | | d t Sd S(   s-  Returns the next segment on the `PATH_INFO` or `None` if there
    is none.  Works like :func:`pop_path_info` without modifying the
    environment:

    >>> env = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/a/b'}
    >>> peek_path_info(env)
    'a'
    >>> peek_path_info(env)
    'a'

    If the `charset` is set to `None` a bytestring is returned.

    .. versionadded:: 0.5

    .. versionchanged:: 0.9
       The path is now decoded and a charset and encoding
       parameter can be provided.

    :param environ: the WSGI environment that is checked.
    R    R   R   i   i    R]   N(   R%   R'   RJ   R   R   R:   (   R)   R^   R_   t   segments(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   peek_path_info6  s    'c         C   sC  d   } t  | | |  } t |  t  r? t |  d t }  n  t  |  | |  } t |  d  \ } }	 }
 t t | |   d  \ } } } | | |	  }	 | | |  } | r xC | | f D] } | d k r d Sq Wn | d	 k o | | k s d S|	 | k rd S|
 j d  }
 | j	 |
  s(d Sd | t
 |
  j d  S(
   s  Extracts the path info from the given URL (or WSGI environment) and
    path.  The path info returned is a unicode string, not a bytestring
    suitable for a WSGI environment.  The URLs might also be IRIs.

    If the path info could not be determined, `None` is returned.

    Some examples:

    >>> extract_path_info('http://example.com/app', '/app/hello')
    u'/hello'
    >>> extract_path_info('http://example.com/app',
    ...                   'https://example.com/app/hello')
    u'/hello'
    >>> extract_path_info('http://example.com/app',
    ...                   'https://example.com/app/hello',
    ...                   collapse_http_schemes=False) is None
    True

    Instead of providing a base URL you can also pass a WSGI environment.

    .. versionadded:: 0.6

    :param environ_or_baseurl: a WSGI environment dict, a base URL or
                               base IRI.  This is the root of the
                               application.
    :param path_or_url: an absolute path from the server root, a
                        relative path (in which case it's the path info)
                        or a full URL.  Also accepts IRIs and unicode
                        parameters.
    :param charset: the charset for byte data in URLs
    :param errors: the error handling on decode
    :param collapse_http_schemes: if set to `False` the algorithm does
                                  not assume that http and https on the
                                  same server point to the same
                                  resource.
    c         S   s   | j  d d  d j  d d  } t |  d k r| | \ } } |  d k rX | d k sp |  d k r | d	 k r d  } q n | d
 } d  } | d  k	 r | d | 7} n  | S(   Nu   @i   iu   :i   u   httpu   80u   httpsu   443i    (   RJ   Rc   RL   (   t   schemet   netloct   partst   port(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   _normalize_netlocw  s    "
R*   i   u   httpu   httpsu   /(   u   httpu   httpsN(   u   httpu   https(   R   R8   t   dictR1   R:   R   R   RL   R&   R9   Rc   R'   (   t   environ_or_baseurlt   path_or_urlR^   R_   t   collapse_http_schemesRn   R`   t   base_irit   base_schemet   base_netloct	   base_patht
   cur_schemet
   cur_netloct   cur_pathRj   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   extract_path_infoQ  s.    &		"t   SharedDataMiddlewarec           B   sb   e  Z d  Z d e d d d  Z d   Z d   Z d   Z d   Z	 d	   Z
 d
   Z d   Z RS(   s)	  A WSGI middleware that provides static content for development
    environments or simple server setups. Usage is quite simple::

        import os
        from werkzeug.wsgi import SharedDataMiddleware

        app = SharedDataMiddleware(app, {
            '/shared': os.path.join(os.path.dirname(__file__), 'shared')
        })

    The contents of the folder ``./shared`` will now be available on
    ``http://example.com/shared/``.  This is pretty useful during development
    because a standalone media server is not required.  One can also mount
    files on the root folder and still continue to use the application because
    the shared data middleware forwards all unhandled requests to the
    application, even if the requests are below one of the shared folders.

    If `pkg_resources` is available you can also tell the middleware to serve
    files from package data::

        app = SharedDataMiddleware(app, {
            '/shared': ('myapplication', 'shared_files')
        })

    This will then serve the ``shared_files`` folder in the `myapplication`
    Python package.

    The optional `disallow` parameter can be a list of :func:`~fnmatch.fnmatch`
    rules for files that are not accessible from the web.  If `cache` is set to
    `False` no caching headers are sent.

    Currently the middleware does not support non ASCII filenames.  If the
    encoding on the file system happens to be the encoding of the URI it may
    work but this could also be by accident.  We strongly suggest using ASCII
    only file names for static files.

    The middleware will guess the mimetype using the Python `mimetype`
    module.  If it's unable to figure out the charset it will fall back
    to `fallback_mimetype`.

    .. versionchanged:: 0.5
       The cache timeout is configurable now.

    .. versionadded:: 0.6
       The `fallback_mimetype` parameter was added.

    :param app: the application to wrap.  If you don't want to wrap an
                application you can pass it :exc:`NotFound`.
    :param exports: a dict of exported files and folders.
    :param disallow: a list of :func:`~fnmatch.fnmatch` rules.
    :param fallback_mimetype: the fallback mimetype for unknown files.
    :param cache: enable or disable caching headers.
    :Param cache_timeout: the cache timeout in seconds for the headers.
    i<   i   s
   text/plainc   
         s  | |  _  i  |  _ | |  _ | |  _ x t |  D] \ } } t | t  r^ |  j |   }	 nU t | t  r t	 j
 j |  r |  j |  }	 q |  j |  }	 n t d |   |	 |  j | <q1 W  d  k	 r d d l m     f d   |  _ n  | |  _ d  S(   Ns   unknown def %ri(   t   fnmatchc            s     |    S(   N(    (   t   x(   R|   t   disallow(    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    (   t   appt   exportst   cachet   cache_timeoutR   R8   t   tuplet   get_package_loaderR	   t   osR`   t   isfilet   get_file_loadert   get_directory_loaderRS   RL   R|   t
   is_allowedt   fallback_mimetype(
   t   selfR   R   R~   R   R   R   t   keyt   valuet   loader(    (   R~   R|   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   __init__  s"    				c         C   s   t  S(   s   Subclasses can override this method to disallow the access to
        certain files.  However by providing `disallow` in the constructor
        this method is overwritten.
        (   R:   (   R   t   filename(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    c            s     f d   S(   Nc              s=   t    d  t j t j j     t t j j     f S(   Nt   rb(   t   openR   t   utcfromtimestampR   R`   t   getmtimeRQ   t   getsize(    (   R   (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    (    (   R   R   (    (   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   _opener  s    c            s      f d   S(   Nc            s   t  j j     j   f S(   N(   R   R`   t   basenameR   (   R}   (   R   R   (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    (    (   R   R   (    (   R   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    c            sn   d d l  m } m } m } t j    | |   |    t  |          f d   } | S(   Ni(   t   DefaultProvidert   ResourceManagert   get_providerc            s     d  k r d St j        j    s5 d St j    }  ri |  j  j      f S|      f d   f S(   Nc              s    j       d f S(   Ni    (   t   get_resource_stream(    (   R`   t   managert   loadtimet   provider(    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    (   NN(   NN(   RL   t	   posixpathR$   t   has_resourceR   R   t   get_resource_filename(   R`   R   (   R   t   filesystem_boundR   t   package_pathR   R   (   R`   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    	(   t   pkg_resourcesR   R   R   R   t   utcnowR8   (   R   t   packageR   R   R   R   R   (    (   R   R   R   R   R   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    	c            s      f d   } | S(   Nc            s_   |  d  k	 r$ t j j   |   }  n   }  t j j |   r[ t j j |    j |   f Sd S(   N(   NN(   RL   R   R`   R$   R   R   R   (   R`   (   t	   directoryR   (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR   !  s    (    (   R   R   R   (    (   R   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR      s    c         C   sN   t  | t  s' | j t j    } n  d t | j    | t |  d @f S(   Ns   wzsdm-%d-%s-%sl    (   R8   t   bytest   encodet   syst   getfilesystemencodingR   t	   timetupleR   (   R   t   mtimet	   file_sizet   real_filename(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   generate_etag+  s    c         C   s  t  |  } t r* | j t j    } n  | j d  } xA t j t j f D]- } | rL | d k rL | j	 | d  } qL qL Wd j
 d g g  | j d  D] } | r | d k r | ^ q  } d  } x t |  j  D] \ } }	 | | k r|	 d   \ }
 } | d  k	 rPqn  | j d  s0| d 7} n  | j |  r |	 | t |   \ }
 } | d  k	 rnPqnq q W| d  k s|  j |
  r|  j | |  St j |
  } | d p|  j } |   \ } } } d t   f g } |  j r|  j } |  j | | |
  } | d d | f d d	 | f g 7} t | | d
 | s_| j   | d |  g  S| j d t t   |  f  n | j d  | j d | f d t  |  f d t |  f f  | d |  t! | |  S(   NR   R   s   ..i    t   Datet   Etags   "%s"s   Cache-Controls   max-age=%d, publict   last_modifieds   304 Not Modifiedt   Expirest   publics   Content-Types   Content-Lengths   Last-Modifieds   200 OK(   s   Cache-ControlR   ("   Ra   R   R   R   R   RK   R   t   sept   altsepR\   R$   RJ   RL   R   R   R;   R9   Rc   R   R   t	   mimetypest
   guess_typeR   R   R   R   R   R   t   closeR#   R   t   extendt   strt	   wrap_file(   R   R)   t   start_responset   cleaned_pathR   R}   R`   t   file_loadert   search_pathR   R   t   guessed_typet	   mime_typeR   R   R   t   headerst   timeoutt   etag(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   __call__4  sZ    ""		
#	Ni  i  (   t   __name__t
   __module__t   __doc__RL   R:   R   R   R   R   R   R   R   R   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR{     s   6							t   DispatcherMiddlewarec           B   s#   e  Z d  Z d d  Z d   Z RS(   s  Allows one to mount middlewares or applications in a WSGI application.
    This is useful if you want to combine multiple WSGI applications::

        app = DispatcherMiddleware(app, {
            '/app2':        app2,
            '/app3':        app3
        })
    c         C   s   | |  _  | p i  |  _ d  S(   N(   R   t   mounts(   R   R   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR   u  s    	c         C   s   | j  d d  } d } x~ d | k r | |  j k rG |  j | } Pn  | j d  } d j | d   } d | d | f } q W|  j j  | |  j  } | j  d d  } | | | d <| | d <| | |  S(   NR    R   R   is   /%s%sR   (   R%   R   RJ   R$   R   (   R   R)   R   t   scriptt	   path_infoR   t   itemst   original_script_name(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR   y  s    
N(   R   R   R   RL   R   R   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR   k  s   t   ClosingIteratorc           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   s!  The WSGI specification requires that all middlewares and gateways
    respect the `close` callback of an iterator.  Because it is useful to add
    another close action to a returned iterator and adding a custom iterator
    is a boring task this class can be used for that::

        return ClosingIterator(app(environ, start_response), [cleanup_session,
                                                              cleanup_locals])

    If there is just one close function it can be passed instead of the list.

    A closing iterator is not needed if the application uses response objects
    and finishes the processing if the response is started::

        try:
            return response(environ, start_response)
        finally:
            cleanup_session()
            cleanup_locals()
    c         C   s   t  |  } t t |  |  _ | d  k r3 g  } n$ t |  rK | g } n t |  } t | d d   } | r | j d |  n  | |  _	 d  S(   NR   i    (
   t   iterR   t   nextt   _nextRL   t   callablet   listt   getattrt   insertt
   _callbacks(   R   t   iterablet	   callbackst   iteratort   iterable_close(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    	c         C   s   |  S(   N(    (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   __iter__  s    c         C   s
   |  j    S(   N(   R   (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   __next__  s    c         C   s   x |  j  D] } |   q
 Wd  S(   N(   R   (   R   t   callback(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    N(   R   R   R   RL   R   R   R   R   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s
   		i    c         C   s   |  j  d t  | |  S(   s  Wraps a file.  This uses the WSGI server's file wrapper if available
    or otherwise the generic :class:`FileWrapper`.

    .. versionadded:: 0.5

    If the file wrapper from the WSGI server is used it's important to not
    iterate over it from inside the application but to pass it through
    unchanged.  If you want to pass out a file wrapper inside a response
    object you have to set :attr:`~BaseResponse.direct_passthrough` to `True`.

    More information about file wrappers are available in :pep:`333`.

    :param file: a :class:`file`-like object with a :meth:`~file.read` method.
    :param buffer_size: number of bytes for one iteration.
    s   wsgi.file_wrapper(   R%   t   FileWrapper(   R)   t   filet   buffer_size(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    R   c           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   s`  This class can be used to convert a :class:`file`-like object into
    an iterable.  It yields `buffer_size` blocks until the file is fully
    read.

    You should not use this class directly but rather use the
    :func:`wrap_file` function that uses the WSGI server's file wrapper
    support if it's available.

    .. versionadded:: 0.5

    If you're using this object together with a :class:`BaseResponse` you have
    to use the `direct_passthrough` mode.

    :param file: a :class:`file`-like object with a :meth:`~file.read` method.
    :param buffer_size: number of bytes for one iteration.
    i    c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    	c         C   s&   t  |  j d  r" |  j j   n  d  S(   NR   (   t   hasattrR   R   (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    c         C   s   |  S(   N(    (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    c         C   s,   |  j  j |  j  } | r | St    d  S(   N(   R   t   readR   t   StopIteration(   R   t   data(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    (   R   R   R   R   R   R   R   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s
   		c         c   s   t  |  t t t f  r' t d   n  t |  d  sY x |  D] } | r= | Vq= q= Wd St  |  t  r | d k	 r t |  |  }  n  |  j } x | |  } | s Pn  | Vq d S(   s-   Helper for the line and chunk iter functions.sB   Passed a string or byte object instead of true iterator or stream.R   N(	   R8   R   t	   bytearrayR   RS   R   RV   RL   R   (   RX   t   limitR   t   itemt   _read(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   _make_chunk_iter  s    	i
   i   c   
      #   s   t  |  | |    t   d  } | s+ d St |  } | d   | d  } | d  } | d   t | f          f d   }  } xT |   D]I }	 |	 | k r | d | k r | |	 7}  }	 n  | r | Vn  |	 } q W| r | Vn  d S(   s(  Safely iterates line-based over an input stream.  If the input stream
    is not a :class:`LimitedStream` the `limit` parameter is mandatory.

    This uses the stream's :meth:`~file.read` method internally as opposite
    to the :meth:`~file.readline` method that is unsafe and can only be used
    in violation of the WSGI specification.  The same problem applies to the
    `__iter__` function of the input stream which calls :meth:`~file.readline`
    without arguments.

    If you need line-by-line processing it's strongly recommended to iterate
    over the input stream using this helper function.

    .. versionchanged:: 0.8
       This function now ensures that the limit was reached.

    .. versionadded:: 0.9
       added support for iterators as input stream.

    :param stream: the stream or iterate to iterate over.
    :param limit: the limit in bytes for the stream.  (Usually
                  content length.  Not necessary if the `stream`
                  is a :class:`LimitedStream`.
    :param buffer_size: The optional buffer size.
    R   Ns   s   
s   
c          3   s    j  }  g  } x t   d  } | s+ Pn  g  } xW t | | j t   D]= } | j |  | rJ | d  k rJ |  |  Vg  } qJ qJ W| } q | r |  |  Vn  d  S(   NR   i(   R$   R   R    t
   splitlinesR:   R#   (   t   _joint   buffert   new_datat   new_bufR   (   t   _itert   crlft   empty(    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   _iter_basic_lines-  s    		i(   R   R   R   R    (
   RX   R   R   t
   first_itemt   st   crt   lfR   t   previousR   (    (   R   R   R   sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   make_line_iter  s*    
	
c         c   sW  t  |  | |  } t | d  } | s+ d St | f |  } t | t  r t |  } t j d t j |   j	 } d j
 } n8 t |  } t j d t j |  d  j	 } d j
 } g  } x{ t | d  }	 |	 s Pn  | |	  }
 g  } xD t | |
  D]3 } | | k r%| |  Vg  } q | j |  q W| } q | rS| |  Vn  d S(   s  Works like :func:`make_line_iter` but accepts a separator
    which divides chunks.  If you want newline based processing
    you should use :func:`make_line_iter` instead as it
    supports arbitrary newline markers.

    .. versionadded:: 0.8

    .. versionadded:: 0.9
       added support for iterators as input stream.

    :param stream: the stream or iterate to iterate over.
    :param separator: the separator that divides chunks.
    :param limit: the limit in bytes for the stream.  (Usually
                  content length.  Not necessary if the `stream`
                  is otherwise already limited).
    :param buffer_size: The optional buffer size.
    R   Ns   (%s)u    t   (t   )(   R   R   R    R8   R   R   t   ret   compilet   escapeRJ   R$   R   R#   (   RX   t	   separatorR   R   R   R   t   _splitR   R   R   t   chunksR   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   make_chunk_iterL  s6    #			RV   c           B   s   e  Z d  Z d   Z d   Z e d    Z d   Z d   Z d d  Z	 d d	  Z d d
  Z d d  Z d   Z d   Z RS(   s  Wraps a stream so that it doesn't read more than n bytes.  If the
    stream is exhausted and the caller tries to get more bytes from it
    :func:`on_exhausted` is called which by default returns an empty
    string.  The return value of that function is forwarded
    to the reader function.  So if it returns an empty string
    :meth:`read` will return an empty string as well.

    The limit however must never be higher than what the stream can
    output.  Otherwise :meth:`readlines` will try to read past the
    limit.

    .. admonition:: Note on WSGI compliance

       calls to :meth:`readline` and :meth:`readlines` are not
       WSGI compliant because it passes a size argument to the
       readline methods.  Unfortunately the WSGI PEP is not safely
       implementable without a size argument to :meth:`readline`
       because there is no EOF marker in the stream.  As a result
       of that the use of :meth:`readline` is discouraged.

       For the same reason iterating over the :class:`LimitedStream`
       is not portable.  It internally calls :meth:`readline`.

       We strongly suggest using :meth:`read` only or using the
       :func:`make_line_iter` which safely iterates line-based
       over a WSGI input stream.

    :param stream: the stream to wrap.
    :param limit: the limit for the stream, must not be longer than
                  what the string can provide if the stream does not
                  end with `EOF` (like `wsgi.input`)
    c         C   s.   | j  |  _ | j |  _ d |  _ | |  _ d  S(   Ni    (   R   R   t   readlinet	   _readlinet   _posR   (   R   RX   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    	c         C   s   |  S(   N(    (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    c         C   s   |  j  |  j k S(   s4   If the stream is exhausted this attribute is `True`.(   R  R   (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   is_exhausted  s    c         C   s   |  j  d  S(   s   This is called when the stream tries to read past the limit.
        The return value of this function is returned from the reading
        function.
        i    (   R   (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   on_exhausted  s    c         C   s   d d l  m } |    d S(   s  What should happen if a disconnect is detected?  The return
        value of this function is returned from read functions in case
        the client went away.  By default a
        :exc:`~werkzeug.exceptions.ClientDisconnected` exception is raised.
        i(   t   ClientDisconnectedN(   RM   R
  (   R   R
  (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   on_disconnect  s    i   i@   c         C   sS   |  j  |  j } | } x6 | d k rN t | |  } |  j |  | | 8} q Wd S(   s  Exhaust the stream.  This consumes all the data left until the
        limit is reached.

        :param chunk_size: the size for a chunk.  It will read the chunk
                           until the stream is exhausted and throw away
                           the results.
        i    N(   R   R  t   minR   (   R   t
   chunk_sizet   to_readt   chunk(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   exhaust  s    c         C   s   |  j  |  j k r |  j   S| d k s4 | d k r@ |  j } n  t |  j |  j  |  } y |  j |  } Wn t t f k
 r |  j   SX| r t	 |  | k r |  j   S|  j  t	 |  7_  | S(   sy   Read `size` bytes or if size is not provided everything is read.

        :param size: the number of bytes read.
        iN(
   R  R   R	  RL   R  R   t   IOErrorRR   R  Rc   (   R   t   sizeR  R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    

c         C   s   |  j  |  j k r |  j   S| d k r; |  j |  j  } n t | |  j |  j   } y |  j |  } Wn t t f k
 r |  j   SX| r | r |  j   S|  j  t	 |  7_  | S(   s   Reads one line from the stream.N(
   R  R   R	  RL   R  R  RR   R  R  Rc   (   R   R  t   line(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR    s    

c         C   s   |  j  } g  } | d k	 r4 t |  j | |  } n	 |  j } xd | d k	 r` | | |  j  8} n  |  j  | k rs Pn  | j |  j |   | d k	 r@ |  j  } q@ q@ | S(   s   Reads a file into a list of strings.  It calls :meth:`readline`
        until the file is read to the end.  It does support the optional
        `size` argument if the underlaying stream supports it for
        `readline`.
        N(   R  RL   R  R   R#   R  (   R   R  t   last_post   resultt   end(    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt	   readlines  s    		c         C   s   |  j  S(   sK   Returns the position of the stream.

        .. versionadded:: 0.9
        (   R  (   R   (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   tell  s    c         C   s"   |  j    } | s t    n  | S(   N(   R  R   (   R   R  (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyR     s    i   N(   R   R   R   R   R   t   propertyR  R	  R  R  RL   R   R  R  R  R   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyRV     s   !							i (  i (  (?   R   R   R   R   R   R   t	   itertoolsR    t   zlibR   R   R   R   t	   functoolsR   R   t   werkzeug._compatR   R   R	   R
   R   R   R   R   R   R   t   werkzeug._internalR   R   t   werkzeug.httpR   R   t   werkzeug.urlsR   R   R   R   R   R7   RL   R1   R?   R"   RU   R:   RY   R(   Ra   Rb   Rg   Ri   Rz   t   objectR{   R   R   R   R   R   R   R  RV   (    (    (    sS   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/wsgi.pyt   <module>
   sP   F"	2	%	"	4W .$	F4