
    9h                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
Jr   " S S\R                  R                  5      rg)z8Utilities for using Motor with Tornado web applications.    N)_hash_gridoutc                   D    \ rS rSrSrSS jrS rSS jrS rS r	S r
S	rg
)GridFSHandler&   aF  A handler that can serve content from GridFS, very similar to
:class:`tornado.web.StaticFileHandler`.

.. code-block:: python

    db = motor.MotorClient().my_database
    application = web.Application(
        [
            (r"/static/(.*)", web.GridFSHandler, {"database": db}),
        ]
    )

By default, requests' If-Modified-Since headers are honored, but no
specific cache-control timeout is sent to clients. Thus each request for
a GridFS file requires a quick check of the file's ``uploadDate`` in
MongoDB. Override :meth:`get_cache_time` in a subclass to customize this.
c                     Xl         X l        g )Ndatabaseroot_collection)selfr	   r
   s      NC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\motor/web.py
initializeGridFSHandler.initialize9   s     .    c                 $    UR                  U5      $ )aS  Overridable method to choose a GridFS file to serve at a URL.

By default, if a URL pattern like ``"/static/(.*)"`` is mapped to this
``GridFSHandler``, then the trailing portion of the URL is used as the
filename, so a request for "/static/image.png" results in a call to
:meth:`MotorGridFSBucket.open_download_stream_by_name` with "image.png"
as the ``filename`` argument. To customize the mapping of path to
GridFS file, override ``get_gridfs_file`` and return a Future
:class:`~motor.MotorGridOut` from it.

For example, to retrieve the file by ``_id`` instead of filename::

    class CustomGridFSHandler(motor.web.GridFSHandler):
        def get_gridfs_file(self, bucket, filename, request):
            # Path is interpreted as _id instead of name.
            # Return a Future MotorGridOut.
            return fs.open_download_stream(file_id=ObjectId(path))

:Parameters:
  - `bucket`: A :class:`~motor.motor_tornado.MotorGridFSBucket`
  - `filename`: A string, the matched group of the URL pattern
  - `request`: An :class:`tornado.httputil.HTTPServerRequest`

.. versionchanged:: 1.0
  **BREAKING CHANGE**: Now takes a
  :class:`~motor.motor_tornado.MotorGridFSBucket`, not a
  ``MotorGridFS``.
  Also takes an additional ``request`` parameter.

.. versionchanged:: 0.2
   ``get_gridfs_file`` no longer accepts a callback, instead returns
   a Future.
)open_download_stream_by_name)r   bucketfilenamerequests       r   get_gridfs_fileGridFSHandler.get_gridfs_file=   s    D 228<<r   c                 H  #    [         R                  " U R                  U R                  5      n U R	                  X1U R
                  5      I S h  vN nUR                  R                  SS9nU R                  SU5        [        U5      nU R                  SSU-  5        UR                  nUc  [         R"                  " U5      u  pxU(       a  U R                  SU5        U R%                  XU5      n	U	S:  a  U R                  S[&        R&                  R)                  [&        R*                  R,                  5      R                  S S	9[&        R.                  " U	S
9-   5        U R                  SS[1        U	5      -   5        OU R                  SS5        U R3                  X5        U R
                  R4                  R7                  S5      n
U
b  [8        R:                  R=                  U
5      n[&        R&                  R?                  [@        RB                  " U5      5      R                  URD                  S	9nX:  a  U RG                  S5        g U R
                  R4                  R7                  S5      nUb'  URI                  S5      U:X  a  U RG                  S5        g U R                  SURJ                  5        U(       a  URM                  U 5      I S h  vN   U RO                  5         g  GN! [        R                   a!    [        R                  R                  S5      S ef = f NP7f)Ni  r   )microsecondzLast-ModifiedEtagz"%s"zContent-TypeExpires)tzinfo)secondszCache-Controlzmax-age=publiczIf-Modified-Sincei0  zIf-None-Match"zContent-Length)(motorMotorGridFSBucketr	   r
   r   r   gridfsNoFiletornadoweb	HTTPErrorupload_datereplace
set_headerr   content_type	mimetypes
guess_typeget_cache_timedatetimenowtimezoneutc	timedeltastrset_extra_headersheadersgetemailutils	parsedatefromtimestamptimemktimer   
set_statusstriplengthstream_to_handlerfinish)r   pathinclude_bodyfsgridoutmodifiedchecksum	mime_typeencoding
cache_time	ims_value
date_tupleif_sinceetags                 r   r5   GridFSHandler.geta   s    $$T]]D4H4HI	7 004<<HHG
 &&..1.=2 !) 12((	 "+"6"6t"<I OONI6((C
>OO!!%%h&7&7&;&;<DDDDQ$$Z89
 OOOZ#j/-IJOOOX6t- LL((,,-@A	 ..y9J  ((66t{{:7NOWW X H #$ ||##''8

38 ;OOC ('..9++D111
 	E I}} 	7++'',$6	7x 2s;   ,L"K( K%K( I<L"L L"%K( (5LL"c                 "    U R                  USS9$ )NF)rB   )r5   )r   rA   s     r   headGridFSHandler.head   s    xx5x11r   c                     g)zOverride to customize cache control behavior.

Return a positive number of seconds to trigger aggressive caching or 0
to mark resource as cacheable, only. 0 is the default.
r    )r   rA   rE   rG   s       r   r,   GridFSHandler.get_cache_time   s     r   c                     g)z1For subclass to add extra headers to the responseNrS   )r   rA   rD   s      r   r3   GridFSHandler.set_extra_headers   s    r   r   N)rC   )T)__name__
__module____qualname____firstlineno____doc__r   r   r5   rP   r,   r3   __static_attributes__rS   r   r   r   r   &   s)    $/"=HFP2@r   r   )r[   r-   email.utilsr6   r*   r:   r!   tornado.webr#   r   motor.motor_gridfsr   r$   RequestHandlerr   rS   r   r   <module>ra      s<    ?        ,P@GKK.. P@r   