B a�N��@s�dZddlZddlmZdZdZdZdZdZGd d �d e �Z Gd d �d e �Z Gd d�de �Z d dgZ dekr�ddlZejejd�dS)a~ 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. �N)� catch_errorsii,zpaste.bytes_receivedzpaste.request_startedzpaste.request_finishedc@s<eZdZdZdd�Zdd�Zddd�Zd d �Zdd d �Zd S)� _ProgressFilezy This is the input-file wrapper used to record the number of ``paste.bytes_received`` for the given request. cCs(||_||_|j|_|j|_|j|_dS)N)�_ProgressFile_environ�_ProgressFile_rfile�flush�write� writelines)�self�environZrfile�r �?/opt/alt/python37/lib/python3.7/site-packages/paste/progress.py�__init__1s z_ProgressFile.__init__cs&|j�t|j����fdd�}t|�S)Nc3s,x&�D]}�tt|�7<|VqWdS)N)�ENVIRON_RECEIVED�len)�chunk)r �riterr r �iterwrap;s z(_ProgressFile.__iter__.<locals>.iterwrap)r�iterr)r rr )r rr �__iter__8s z_ProgressFile.__iter__���cCs&|j�|�}|jtt|�7<|S)N)r�readrrr)r �sizerr r r rAs z_ProgressFile.readcCs$|j��}|jtt|�7<|S)N)r�readlinerrr)r rr r r rFs z_ProgressFile.readlineNcCs&|j�|�}|jtt|�7<|S)N)r� readlinesrrr)r Zhintrr r r rKs z_ProgressFile.readlines)r)N) �__name__� __module__� __qualname__�__doc__r rrrrr r r r r+s  rc@s*eZdZdZd dd�Zdd�Zdd�ZdS) �UploadProgressMonitora< 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. NcCs$||_|p t|_|pt|_g|_dS)N)� application�DEFAULT_THRESHOLD� threshold�DEFAULT_TIMEOUT�timeout�monitor)r rr!r#r r r r �s  zUploadProgressMonitor.__init__cs���dd�}|rxt|�|jkrx|j���d�t<t���t<d�t<t ��d��d<d�fdd� }t |j �|||�S|� �|�S)N�CONTENT_LENGTHrz wsgi.inputcst���t<dS)N)�time�REQUEST_FINISHED)�exc_info)r r r � finalizer�sz1UploadProgressMonitor.__call__.<locals>.finalizer)N) �get�intr!r$�appendrr&�REQUEST_STARTEDr'rrr)r r �start_response�lengthr)r )r r �__call__�s    zUploadProgressMonitor.__call__cCs|jS)N)r$)r r r r �uploads�szUploadProgressMonitor.uploads)NN)rrrrr r0r1r r r r rPs3 rc@s0eZdZdZdd�Zdd�Zdd�Zdd �Zd S) �UploadProgressReporterag 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. cCs ||_dS)N)r$)r r$r r r r �szUploadProgressReporter.__init__cCs |�dd�|�dd�krdSdS)NZ REMOTE_USERrTF)r*)r Zsearch_environZupload_environr r r �match�s zUploadProgressReporter.matchc Csdt�dt�|t��d|�d�|t|�dd�|�dd�d�}|t}|r`t�dt�|��|d<|S) Nz%Y-%m-%d %H:%M:%S�r%Z PATH_INFOZ QUERY_STRING)�started�finishedZcontent_lengthZbytes_receivedZ path_infoZ query_stringz%Y:%m:%d %H:%M:%Sr6)r&�strftime�gmtimer-r*rr')r r �retvalr6r r r �report�s zUploadProgressReporter.reportcs�g}xz��fdd��j��D�D]\}g}x>|��D]2\}}t|��dd��dd�}|�d||f�q2W|�dd �|��q Wd d �|�}|d d d t|�fg�|gS)Ncs"g|]}���|�r��|��qSr )r3r:)�.0�env)r r r r � <listcomp>�sz3UploadProgressReporter.__call__.<locals>.<listcomp>�\z\\�"z\"z%s: "%s"z{ %s }z, z[ %s ]z200 OK)z Content-Typez text/plainzContent-Length)r$r1�items�str�replacer,�joinr)r r r.�body�map�parts�k�vr )r r r r0�s zUploadProgressReporter.__call__N)rrrrr r3r:r0r r r r r2�s r2�__main__)Z optionflags)rr&Z paste.wsgilibrr r"rr-r'�objectrrr2�__all__rZdoctestZtestmod�ELLIPSISr r r r �<module>!s %M=