³ò
+e|Mc           @   sÂ   d  Z  d d k Z d d k l Z d Z d Z d Z d Z d	 Z d
 e	 f d „  ƒ  YZ
 d e	 f d „  ƒ  YZ d e	 f d „  ƒ  YZ d d g Z d e j o# d d k Z e i d e i ƒ n d S(   s~  
Upload Progress Monitor

This is a WSGI middleware component which monitors the status of files
being uploaded.  It includes a small query application which will return
a list of all files being uploaded by particular session/user.

>>> from paste.httpserver import serve
>>> from paste.urlmap import URLMap
>>> from paste.auth.basic import AuthBasicHandler
>>> from paste.debug.debugapp import SlowConsumer, SimpleApplication
>>> # from paste.progress import *
>>> realm = 'Test Realm'
>>> def authfunc(username, password):
...     return username == password
>>> map = URLMap({})
>>> ups = UploadProgressMonitor(map, threshold=1024)
>>> map['/upload'] = SlowConsumer()
>>> map['/simple'] = SimpleApplication()
>>> map['/report'] = UploadProgressReporter(ups)
>>> serve(AuthBasicHandler(ups, realm, authfunc))
serving on...

.. note::

   This is experimental, and will change in the future.
iÿÿÿÿN(   t   catch_errorsi   i<   i   s   paste.bytes_receiveds   paste.request_starteds   paste.request_finishedt   _ProgressFilec           B   sA   e  Z d  Z d „  Z d „  Z d d „ Z d „  Z d d „ Z RS(   sy   
    This is the input-file wrapper used to record the number of
    ``paste.bytes_received`` for the given request.
    c         C   s:   | |  _  | |  _ | i |  _ | i |  _ | i |  _ d  S(   N(   t   _ProgressFile_environt   _ProgressFile_rfilet   flusht   writet
   writelines(   t   selft   environt   rfile(    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   __init__1   s
    		c            s4   |  i  ‰  t |  i ƒ ‰ ‡  ‡ f d †  } t | ƒ S(   Nc          3   s0   x) ˆ D]! }  ˆ  t  c t |  ƒ 7<|  Vq Wd  S(   N(   t   ENVIRON_RECEIVEDt   len(   t   chunk(   R   t   riter(    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   iterwrap;   s     (   R   t   iterR   (   R   R   (    (   R   R   s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   __iter__8   s    	iÿÿÿÿc         C   s/   |  i  i | ƒ } |  i t c t | ƒ 7<| S(   N(   R   t   readR   R   R   (   R   t   sizeR   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR   A   s    c         C   s,   |  i  i ƒ  } |  i t c t | ƒ 7<| S(   N(   R   t   readlineR   R   R   (   R   R   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR   F   s    c         C   s/   |  i  i | ƒ } |  i t c t | ƒ 7<| S(   N(   R   t	   readlinesR   R   R   (   R   t   hintR   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR   K   s    N(	   t   __name__t
   __module__t   __doc__R
   R   R   R   t   NoneR   (    (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR   +   s   				t   UploadProgressMonitorc           B   s/   e  Z d  Z d d d „ Z d „  Z d „  Z RS(   s<  
    monitors and reports on the status of uploads in progress

    Parameters:

        ``application``

            This is the next application in the WSGI stack.

        ``threshold``

            This is the size in bytes that is needed for the
            upload to be included in the monitor.

        ``timeout``

            This is the amount of time (in seconds) that a upload
            remains in the monitor after it has finished.

    Methods:

        ``uploads()``

            This returns a list of ``environ`` dict objects for each
            upload being currently monitored, or finished but whose time
            has not yet expired.

    For each request ``environ`` that is monitored, there are several
    variables that are stored:

        ``paste.bytes_received``

            This is the total number of bytes received for the given
            request; it can be compared with ``CONTENT_LENGTH`` to
            build a percentage complete.  This is an integer value.

        ``paste.request_started``

            This is the time (in seconds) when the request was started
            as obtained from ``time.time()``.  One would want to format
            this for presentation to the user, if necessary.

        ``paste.request_finished``

            This is the time (in seconds) when the request was finished,
            canceled, or otherwise disconnected.  This is None while
            the given upload is still in-progress.

    TODO: turn monitor into a queue and purge queue of finished
          requests that have passed the timeout period.
    c         C   s6   | |  _  | p t |  _ | p t |  _ g  |  _ d  S(   N(   t   applicationt   DEFAULT_THRESHOLDt	   thresholdt   DEFAULT_TIMEOUTt   timeoutt   monitor(   R   R   R   R    (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR
   „   s    	c            s¹   ˆ  i  d d ƒ } | o t | ƒ |  i j oz |  i i ˆ  ƒ d ˆ  t <t i ƒ  ˆ  t <d  ˆ  t	 <t
 ˆ  ˆ  d ƒ ˆ  d <d  ‡  f d † } t |  i ˆ  | | | ƒ Sn |  i ˆ  | ƒ S(   Nt   CONTENT_LENGTHi    s
   wsgi.inputc            s   t  i  ƒ  ˆ  t <d  S(   N(   t   timet   REQUEST_FINISHED(   t   exc_info(   R   (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt	   finalizer”   s    (   t   gett   intR   R!   t   appendR   R#   t   REQUEST_STARTEDR   R$   R   R    R   (   R   R   t   start_responset   lengthR&   (    (   R   s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   __call__Š   s    

c         C   s   |  i  S(   N(   R!   (   R   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   uploadsš   s    N(   R   R   R   R   R
   R-   R.   (    (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR   P   s   3	t   UploadProgressReporterc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sg  
    reports on the progress of uploads for a given user

    This reporter returns a JSON file (for use in AJAX) listing the
    uploads in progress for the given user.  By default, this reporter
    uses the ``REMOTE_USER`` environment to compare between the current
    request and uploads in-progress.  If they match, then a response
    record is formed.

        ``match()``

            This member function can be overriden to provide alternative
            matching criteria.  It takes two environments, the first
            is the current request, the second is a current upload.

        ``report()``

            This member function takes an environment and builds a
            ``dict`` that will be used to create a JSON mapping for
            the given upload.  By default, this just includes the
            percent complete and the request url.

    c         C   s   | |  _  d  S(   N(   R!   (   R   R!   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR
   µ   s    c         C   s1   | i  d d  ƒ | i  d d ƒ j o t Sn t S(   Nt   REMOTE_USERi    (   R'   R   t   Truet   False(   R   t   search_environt   upload_environ(    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   match¸   s    c         C   s²   h  t  i d t  i | t ƒ ƒ d <d d <| i d ƒ d <| t d <| i d d ƒ d	 <| i d
 d ƒ d <} | t } | o# t  i d t  i | ƒ ƒ | d <n | S(   Ns   %Y-%m-%d %H:%M:%St   startedt    t   finishedR"   t   content_lengtht   bytes_receivedt	   PATH_INFOt	   path_infot   QUERY_STRINGt   query_strings   %Y:%m:%d %H:%M:%S(   R#   t   strftimet   gmtimeR*   R'   R   R$   (   R   R   t   retvalR8   (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyt   report¾   s    	
	c   
   
   C   s  g  } xÊ g  } |  i  i ƒ  D]- } |  i | | ƒ o | |  i | ƒ q q ~ D]~ } g  } xU | i ƒ  D]G \ } }	 t |	 ƒ i d d ƒ i d d ƒ }	 | i d | |	 f ƒ qj W| i d d i | ƒ ƒ qQ Wd d i | ƒ } | d	 d d t	 | ƒ f g ƒ | g S(   Ns   \s   \\t   "s   \"s   %s: "%s"s   { %s }s   , s   [ %s ]s   200 OKs   Content-Types
   text/plains   Content-Length(   s   Content-Types
   text/plain(
   R!   R.   R5   RB   t   itemst   strt   replaceR)   t   joinR   (
   R   R   R+   t   bodyt   _[1]t   envt   mapt   partst   kt   v(    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR-   Ì   s    .  $	(   R   R   R   R
   R5   RB   R-   (    (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pyR/      s
   			t   __main__t   optionflagsi   i,  (   R   R#   t   paste.wsgilibR    R   R   R   R*   R$   t   objectR   R   R/   t   __all__R   t   doctestt   testmodt   ELLIPSIS(    (    (    s]   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/Paste-1.7.5.1-py2.5.egg/paste/progress.pys   <module>!   s   %M=