ó
`¾Tc           @   sŠ  d  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 d d l m	 Z	 d d l
 m Z m Z m Z d d l m Z d d	 l m Z m Z d d
 l m Z d d l m Z d d l m Z m Z e j d ƒ Z d „  Z d d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e! f d „  ƒ  YZ" d Z# d e" f d „  ƒ  YZ$ d e! f d „  ƒ  YZ% d S(   s5  
    werkzeug.contrib.sessions
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    This module contains some helper classes that help one to add session
    support to a python WSGI application.  For full client-side session
    storage see :mod:`~werkzeug.contrib.securecookie` which implements a
    secure, client-side session storage.


    Application Integration
    =======================

    ::

        from werkzeug.contrib.sessions import SessionMiddleware, \
             FilesystemSessionStore

        app = SessionMiddleware(app, FilesystemSessionStore())

    The current session will then appear in the WSGI environment as
    `werkzeug.session`.  However it's recommended to not use the middleware
    but the stores directly in the application.  However for very simple
    scripts a middleware for sessions could be sufficient.

    This module does not implement methods or ways to check if a session is
    expired.  That should be done by a cronjob and storage specific.  For
    example to prune unused filesystem sessions one could check the modified
    time of the files.  It sessions are stored in the database the new()
    method should add an expiration timestamp for the session.

    For better flexibility it's recommended to not use the middleware but the
    store and session object directly in the application dispatching::

        session_store = FilesystemSessionStore()

        def application(environ, start_response):
            request = Request(environ)
            sid = request.cookies.get('cookie_name')
            if sid is None:
                request.session = session_store.new()
            else:
                request.session = session_store.get(sid)
            response = get_the_response_object(request)
            if request.session.should_save:
                session_store.save(request.session)
                response.set_cookie('cookie_name', request.session.sid)
            return response(environ, start_response)

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   path(   t   time(   t   random(   t   sha1(   t   dumpt   loadt   HIGHEST_PROTOCOL(   t   CallbackDict(   t   dump_cookiet   parse_cookie(   t   ClosingIterator(   t   rename(   t   PY2t	   text_types   ^[a-f0-9]{40}$c           C   s2   t  t d ƒ r t j d ƒ St t ƒ  ƒ j d ƒ S(   Nt   urandomi   t   ascii(   t   hasattrt   osR   R   R   t   encode(    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   _urandomJ   s    c         C   s[   |  d  k r$ t |  ƒ j d ƒ }  n  t d j |  t t ƒ  ƒ j d ƒ t ƒ  g ƒ ƒ j ƒ  S(   NR   t    (	   t   Nonet   reprR   R   t   joint   strR   R   t	   hexdigest(   t   salt(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   generate_keyP   s    	t   ModificationTrackingDictc           B   s)   e  Z d Z d „  Z d „  Z d „  Z RS(   t   modifiedc         O   s<   d „  } t  |  _ t j |  d | ƒt j |  | | Ž d  S(   Nc         S   s   t  |  _ d  S(   N(   t   TrueR   (   t   self(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt	   on_update^   s    R    (   t   FalseR   R   t   __init__t   dictt   update(   R   t   argst   kwargsR    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR"   ]   s    		c         C   sd   t  ƒ  } t  j |  j ƒ } xB |  j D]7 } t |  | | ƒ } | | k	 r% t | | | ƒ q% q% W| S(   s   Create a flat copy of the dict.(   t   objectt   __new__t	   __class__t	   __slots__t   getattrt   setattr(   R   t   missingt   resultt   namet   val(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   copyd   s    	c         C   s
   |  j  ƒ  S(   N(   R1   (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   __copy__n   s    (   s   modified(   t   __name__t
   __module__R*   R"   R1   R2   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR   Z   s   		
t   Sessionc           B   s?   e  Z d  Z e j d Z e d „ Z d „  Z e d „  ƒ Z	 RS(   s²   Subclass of a dict that keeps track of direct object changes.  Changes
    in mutable structures are not tracked, for those you have to set
    `modified` to `True` by hand.
    t   sidt   newc         C   s&   t  j |  | ƒ | |  _ | |  _ d  S(   N(   R   R"   R6   R7   (   R   t   dataR6   R7   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR"   y   s    	c         C   s/   d |  j  j t j |  ƒ |  j r' d p* d f S(   Ns	   <%s %s%s>t   *R   (   R)   R3   R#   t   __repr__t   should_save(   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR:   ~   s    	c         C   s   |  j  S(   sÎ   True if the session should be saved.

        .. versionchanged:: 0.6
           By default the session is now only saved if the session is
           modified, not if it is new like it was before.
        (   R   (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR;   …   s    (   s   sids   new(
   R3   R4   t   __doc__R   R*   R!   R"   R:   t   propertyR;   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR5   r   s
   	t   SessionStorec           B   s\   e  Z d  Z d	 d „ Z d „  Z d	 d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(
   s>  Baseclass for all session stores.  The Werkzeug contrib module does not
    implement any useful stores besides the filesystem store, application
    developers are encouraged to create their own stores.

    :param session_class: The session class to use.  Defaults to
                          :class:`Session`.
    c         C   s"   | d  k r t } n  | |  _ d  S(   N(   R   R5   t   session_class(   R   R?   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR"   ™   s    	c         C   s   t  j | ƒ d k	 S(   s&   Check if a key has the correct format.N(   t   _sha1_ret   matchR   (   R   t   key(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   is_valid_keyž   s    c         C   s
   t  | ƒ S(   s1   Simple function that generates a new session key.(   R   (   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR   ¢   s    c         C   s   |  j  i  |  j ƒ  t ƒ S(   s   Generate a new session.(   R?   R   R   (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR7   ¦   s    c         C   s   d S(   s   Save a session.N(    (   R   t   session(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   saveª   s    c         C   s   | j  r |  j | ƒ n  d S(   s(   Save if a session class wants an update.N(   R;   RE   (   R   RD   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   save_if_modified­   s    	c         C   s   d S(   s   Delete a session.N(    (   R   RD   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   delete²   s    c         C   s   |  j  i  | t ƒ S(   s´   Get a session for this sid or a new session object.  This method
        has to check if the session key is valid and create a new session if
        that wasn't the case.
        (   R?   R   (   R   R6   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   getµ   s    N(   R3   R4   R<   R   R"   RC   R   R7   RE   RF   RG   RH   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR>      s   					s
   .__wz_sesst   FilesystemSessionStorec           B   sS   e  Z d  Z d	 d d	 e d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(
   sÐ  Simple example session store that saves sessions on the filesystem.
    This store works best on POSIX systems and Windows Vista / Windows
    Server 2008 and newer.

    .. versionchanged:: 0.6
       `renew_missing` was added.  Previously this was considered `True`,
       now the default changed to `False` and it can be explicitly
       deactivated.

    :param path: the path to the folder used for storing the sessions.
                 If not provided the default temporary directory is used.
    :param filename_template: a string template used to give the session
                              a filename.  ``%s`` is replaced with the
                              session id.
    :param session_class: The session class to use.  Defaults to
                          :class:`Session`.
    :param renew_missing: set to `True` if you want the store to
                          give the user a new sid if the session was
                          not yet saved.
    s   werkzeug_%s.sessi¤  c         C   s¦   t  j |  | ƒ | d  k r+ t j ƒ  } n  | |  _ t | t ƒ rg t rg | j	 t
 j ƒ  p^ d ƒ } n  | j t ƒ s‡ t d t ƒ ‚ | |  _ | |  _ | |  _ d  S(   Ns   utf-8s&   filename templates may not end with %s(   R>   R"   R   t   tempfilet
   gettempdirR    t
   isinstanceR   R   R   t   syst   getfilesystemencodingt   endswitht   _fs_transaction_suffixt   AssertionErrort   filename_templatet   renew_missingt   mode(   R   R    RR   R?   RS   RT   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR"   ×   s    			c         C   sM   t  | t ƒ r3 t r3 | j t j ƒ  p* d ƒ } n  t j |  j |  j | ƒ S(   Ns   utf-8(	   RL   R   R   R   RM   RN   R    R   RR   (   R   R6   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   get_session_filenameæ   s    c         C   s¯   |  j  | j ƒ } t j d t d |  j ƒ \ } } t j | d ƒ } z t t	 | ƒ | t
 ƒ Wd  | j ƒ  Xy$ t | | ƒ t j | |  j ƒ Wn t t f k
 rª n Xd  S(   Nt   suffixt   dirt   wb(   RU   R6   RJ   t   mkstempRP   R    R   t   fdopenR   R#   R   t   closeR   t   chmodRT   t   IOErrort   OSError(   R   RD   t   fnt   fdt   tmpt   f(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyRE   î   s    c         C   s;   |  j  | j ƒ } y t j | ƒ Wn t k
 r6 n Xd  S(   N(   RU   R6   R   t   unlinkR^   (   R   RD   R_   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyRG   ý   s
    c         C   s±   |  j  | ƒ s |  j ƒ  Sy t |  j | ƒ d ƒ } Wn* t k
 ra |  j rX |  j ƒ  Si  } n= Xz. y t | ƒ } Wn t k
 rŽ i  } n XWd  | j ƒ  X|  j	 | | t
 ƒ S(   Nt   rb(   RC   R7   t   openRU   R]   RS   R   t	   ExceptionR[   R?   R!   (   R   R6   Rb   R8   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyRH     s    
	

c         C   s¶   |  j  j d d ƒ \ } } t j d t j | ƒ t j | ƒ f ƒ } g  } xc t j |  j ƒ D]O } | j t	 ƒ rz q_ n  | j
 | ƒ } | d k	 r_ | j | j d ƒ ƒ q_ q_ W| S(   sH   Lists all sessions in the store.

        .. versionadded:: 0.6
        s   %si   s   %s(.{5,})%s$N(   RR   t   splitt   ret   compilet   escapeR   t   listdirR    RO   RP   RA   R   t   appendt   group(   R   t   beforet   aftert   filename_reR.   t   filenameRA   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   list  s    N(   R3   R4   R<   R   R!   R"   RU   RE   RG   RH   Rr   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyRI   Á   s   				t   SessionMiddlewarec        	   B   s8   e  Z d  Z d d d d d d e d d „ Z d „  Z RS(   sŠ  A simple middleware that puts the session object of a store provided
    into the WSGI environ.  It automatically sets cookies and restores
    sessions.

    However a middleware is not the preferred solution because it won't be as
    fast as sessions managed by the application itself and will put a key into
    the WSGI environment only relevant for the application which is against
    the concept of WSGI.

    The cookie parameters are the same as for the :func:`~dump_cookie`
    function just prefixed with ``cookie_``.  Additionally `max_age` is
    called `cookie_age` and not `cookie_max_age` because of backwards
    compatibility.
    t
   session_idt   /s   werkzeug.sessionc         C   s^   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _	 d  S(   N(
   t   appt   storet   cookie_namet
   cookie_aget   cookie_expirest   cookie_patht   cookie_domaint   cookie_securet   cookie_httponlyt   environ_key(   R   Rv   Rw   Rx   Ry   Rz   R{   R|   R}   R~   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyR"   :  s    									c            s§   t  | j d d ƒ ƒ } | j ˆ j d  ƒ } | d  k rK ˆ j j ƒ  ‰ n ˆ j j | ƒ ‰ ˆ | ˆ j <d  ‡  ‡ ‡ f d † } t ˆ j | | ƒ ‡ ‡ f d †  ƒ S(   Nt   HTTP_COOKIER   c            sr   ˆ j  rb ˆ j j ˆ ƒ | j d t ˆ j ˆ j ˆ j ˆ j ˆ j	 ˆ j
 ˆ j ˆ j ƒ f ƒ n  ˆ  |  | | ƒ S(   Ns
   Set-Cookie(   R;   Rw   RE   Rl   R   Rx   R6   Ry   Rz   R{   R|   R}   R~   (   t   statust   headerst   exc_info(   t   start_responseR   RD   (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   injecting_start_responseR  s    	c              s   ˆ  j  j ˆ ƒ S(   N(   Rw   RF   (    (   R   RD   (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   <lambda>\  s    (	   R	   RH   Rx   R   Rw   R7   R   R
   Rv   (   R   t   environR„   t   cookieR6   R…   (    (   R„   R   RD   s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   __call__I  s    	N(   R3   R4   R<   R   R!   R"   R‰   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyRs   *  s   	(&   R<   Rh   R   RM   RJ   R    R   R   t   hashlibR   t   pickleR   R   R   t   werkzeug.datastructuresR   t   werkzeug.utilsR   R	   t   werkzeug.wsgiR
   t   werkzeug.posixemulationR   t   werkzeug._compatR   R   Ri   R@   R   R   R   R   R5   R'   R>   RP   RI   Rs   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/sessions.pyt   <module>5   s.   	
.i