ó
ú2ec           @   sµ  d  Z  y d d l m Z Wn! e k
 r= d d l m Z n Xd d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z y d d l Z Wn e k
 rÁ d d l Z n Xy d d l m Z Wn! e k
 rù d d l m Z n Xd d l m Z d d l m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d a e a e j d d ƒ a d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z e e e e e d „ Z d S(   s²  
Creates a session object in your WSGI environment.

Use like:

..code-block:: Python

    environ['paste.session.factory']()

This will return a dictionary.  The contents of this dictionary will
be saved to disk when the request is completed.  The session will be
created when you first fetch the session dictionary, and a cookie will
be sent in that case.  There's current no way to use sessions without
cookies, and there's no way to delete a session except to clear its
data.

@@: This doesn't do any locking, and may cause problems when a single
session is accessed concurrently.  Also, it loads and saves the
session for each request, with no caching.  Also, sessions aren't
expired.
iÿÿÿÿ(   t   SimpleCookieN(   t   md5(   t   wsgilib(   t   requestt   SessionMiddlewarec           B   s   e  Z d d  „ Z d „  Z RS(   c         K   s   | |  _  | |  _ d  S(   N(   t   applicationt
   factory_kw(   t   selfR   t   global_confR   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   __init__5   s    	c            sƒ   t  | |  j  ‰ ˆ | d <g  ‰  d  ‡  ‡ ‡ f d † } |  j | | ƒ } ‡  ‡ ‡ f d †  } ‡ f d †  } t j | | | ƒ S(   Ns   paste.session.factoryc            sF   ˆ j  s# |  | g ˆ  (ˆ |  | ƒ S| j ˆ j ƒ  ƒ ˆ |  | | ƒ S(   N(   t   createdt   appendt   set_cookie_header(   t   statust   headerst   exc_info(   t   remember_headerst   session_factoryt   start_response(    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   session_start_response>   s
    	c             s]   ˆ j  rY ˆ  rY ˆ  \ }  } | j ˆ j ƒ  ƒ t d ƒ } ˆ |  | | j | d  f ƒ n  d  S(   NsL   You cannot get the session after content from the app_iter has been returned(   R
   R   R   t
   ValueErrort	   __class__t   None(   R   R   t   exc(   R   R   R   (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   startF   s    	c              s   ˆ  j  r ˆ  j ƒ  n  d  S(   N(   t   usedt   close(    (   R   (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   O   s    	(   t   SessionFactoryR   R   R   R   t   add_start_close(   R   t   environR   R   t   app_iterR   R   (    (   R   R   R   s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   __call__9   s    
	N(   t   __name__t
   __module__R   R	   R   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   3   s   R   c           B   sS   e  Z d  d
 d d „ Z d „  Z d „  Z d „  Z d
 d „ Z d „  Z d	 „  Z	 RS(   t   _SID_i<   i   c         K   sR   t  |  _ t  |  _ | |  _ | |  _ d  |  _ | p6 t |  _ | |  _	 | |  _
 d  S(   N(   t   FalseR
   R   R   t   cookie_nameR   t   sessiont   FileSessiont   session_classt   session_class_kwt
   expiration(   R   R   R$   R'   t   session_expirationR(   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR	   X   s    						c         C   sú   t  |  _ |  j d  k	 r% |  j j ƒ  St j |  j ƒ } d  } |  j | k r˜ | |  j j	 |  _
 y" |  j |  j
 d t |  j } Wq˜ t k
 r” q˜ Xn  | d  k rÝ t  |  _ |  j ƒ  |  _
 |  j |  j
 d t  |  j } n  | j ƒ  | |  _ | j ƒ  S(   Nt   create(   t   TrueR   R%   R   t   dataR   t   get_cookiesR   R$   t   valuet   sidR'   R#   R(   t   KeyErrorR
   t   make_sidt   clean_up(   R   t   cookiesR%   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   g   s(    		
	c         C   s?   |  j  d  k	 r t St j |  j ƒ } | j |  j ƒ r; t St S(   N(	   R%   R   R,   R   R.   R   t   has_keyR$   R#   (   R   R4   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   has_session~   s    c         C   sE   d j  g  t j t j ƒ  ƒ d  D] } d | ^ q  ƒ d |  j ƒ  S(   Nt    i   s   %02dt   -(   t   joint   timet	   localtimet	   unique_id(   R   t   x(    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR2   †   s    c         C   sö   t  j  ƒ  t j ƒ  g } t t d ƒ r= | j t j ƒ  ƒ n  | d k	 r_ | j t | ƒ ƒ n  t | ƒ } t	 j
 r† | j d ƒ } n  t | ƒ } y | j ƒ  SWnO t k
 rñ g  } x+ | j ƒ  D] } | j d t | ƒ ƒ qÃ Wd j | ƒ SXd S(   s   
        Generates an opaque, identifier string that is practically
        guaranteed to be unique.  If an object is passed, then its
        id() is incorporated into the generation.  Relies on md5 and
        returns a 32 character long string.
        t   timest   utf8s   %02xR7   N(   R:   t   randomt   hasattrt   osR   R>   R   t   idt   strt   sixt   PY3t   encodeR   t	   hexdigestt   AttributeErrort   digestt   ordR9   (   R   t
   for_objectt   rt   contentt   md5_hashRH   t   char(    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR<   ‹   s     	c         C   s   t  ƒ  } |  j | |  j <d | |  j d <t j t j ƒ  |  j d ƒ } t j d | ƒ | |  j d <t | ƒ j d d ƒ \ } } | | f S(   Nt   /t   pathi<   s   %a, %d-%b-%Y %H:%M:%S GMTt   expiress   : i   (	   R    R0   R$   R:   t   gmtimeR)   t   strftimeRD   t   split(   R   t   ct   gmt_expiration_timet   nameR/   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   ¥   s    	 c         C   s#   |  j  d  k	 r |  j  j ƒ  n  d  S(   N(   R%   R   R   (   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   °   s    NiÐ  (
   R    R!   R   R	   R   R6   R2   R<   R   R   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   U   s   				t   secondsi   i<   R&   c           B   sY   e  Z e e j ƒ  d d  d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(	   i@  c         C   s   | r3 t  | t j t j f ƒ r3 t | d ƒ } n  | |  _ | sK t ‚ n  | |  _ | |  _ | s‡ t	 j
 j |  j ƒ  ƒ s‡ t ‚ q‡ n  d  |  _ | |  _ d  S(   Ni   (   t
   isinstanceRE   t   binary_typet	   text_typet   intt   chmodR1   t   session_file_pathR0   RB   RR   t   existst   filenameR   t   _dataR)   (   R   R0   R+   R`   R_   R)   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR	   »   s    !					c         C   s   t  j j |  j |  j ƒ S(   N(   RB   RR   R9   R`   R0   (   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyRb   Ï   s    c         C   sr   |  j  d  k	 r |  j  St j j |  j ƒ  ƒ rb t |  j ƒ  d ƒ } t j | ƒ |  _  | j	 ƒ  n	 i  |  _  |  j  S(   Nt   rb(
   Rc   R   RB   RR   Ra   Rb   t   opent   cPicklet   loadR   (   R   t   f(    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR-   Ò   s    	c         C   s¨   |  j  d  k	 r¤ |  j ƒ  } t j j | ƒ } |  j  sO | r¡ t j | ƒ q¡ q¤ t | d ƒ } t j	 |  j  | ƒ | j
 ƒ  | r¤ |  j r¤ t j | |  j ƒ q¤ n  d  S(   Nt   wb(   Rc   R   Rb   RB   RR   Ra   t   unlinkRe   Rf   t   dumpR   R_   (   R   Rb   Ra   Rh   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR   Ý   s    	
c         C   sŠ   z| t  j d |  j d ƒ } t  j  j ƒ  } xM t j |  j ƒ D]9 \ } } } x' | D] } |  j | d | d | ƒqT Wq> WWd  t a	 Xd  S(   NRZ   i<   t   exp_timet   now(
   t   datetimet	   timedeltaR)   Rm   RB   t   walkR`   t   _clean_up_fileR#   t   cleaning_up(   R   Rl   Rm   t   roott   dirst   filesRh   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt	   _clean_upë   s    "%c      
   C   sê   | j  d ƒ } t | ƒ d k r% d  S| d } yp t j t | d d !ƒ t | d d !ƒ t | d d !ƒ t | d d !ƒ t | d d !ƒ t | d d	 !ƒ ƒ } Wn t k
 r³ d  SX| | | k  ræ t j t j j |  j	 | ƒ ƒ n  d  S(
   NR8   i   i    i   i   i   i
   i   i   (
   RV   t   lenRn   R^   R   RB   t   removeRR   R9   R`   (   R   Rh   Rl   Rm   t   tt	   sess_time(    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyRq   ø   s     
c         C   s‚   t  j  j ƒ  } t r d  St s0 t t | k  r~ t s~ t a y) | a t j d |  j ƒ } | j	 ƒ  Wq{ t
 a ‚  q{ Xq~ n  d  S(   Nt   target(   Rn   Rm   Rr   t   last_cleanupt   cleanup_cycleR,   t	   threadingt   ThreadRv   R   R#   (   R   Rm   Ry   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR3     s    N(   R    R!   R#   t   tempfilet
   gettempdirR   R	   Rb   R-   R   Rv   Rq   R3   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR&   ¹   s   					t
   _NoDefaultc           B   s   e  Z d  „  Z RS(   c         C   s   d S(   Ns   <dynamic default>(    (   R   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   __repr__"  s    (   R    R!   Rƒ   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyR‚   !  s   c         C   så   | t  k r! | j d d ƒ } n  t | ƒ } | t  k rN | j d d ƒ } n  t | ƒ } | t  k r{ | j d d ƒ } n  | t  k rœ | j d d	 ƒ } n  | t  k r½ | j d
 d ƒ } n  t |  d | d | d | d | d | ƒS(   s  
    Adds a middleware that handles sessions for your applications.
    The session is a peristent dictionary.  To get this dictionary
    in your application, use ``environ['paste.session.factory']()``
    which returns this persistent dictionary.

    Configuration:

      session_expiration:
          The time each session lives, in minutes.  This controls
          the cookie expiration.  Default 12 hours.

      expiration:
          The time each session lives on disk.  Old sessions are
          culled from disk based on this.  Default 48 hours.

      cookie_name:
          The cookie name used to track the session.  Use different
          names to avoid session clashes.

      session_file_path:
          Sessions are put in this location, default /tmp.

      chmod:
          The octal chmod you want to apply to new sessions (e.g., 660
          to make the sessions group readable/writable)

    Each of these also takes from the global configuration.  cookie_name
    and chmod take from session_cookie_name and session_chmod
    R*   i<   i   R)   i0   t   session_cookie_nameR"   R`   s   /tmpt   session_chmodR$   R_   iÐ  i@  N(   t	   NoDefaultt   getR^   R   R   (   t   appR   R*   R)   R$   R`   R_   (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   make_session_middleware&  s     %i„  (    t   __doc__t   http.cookiesR    t   ImportErrort   CookieR:   R@   RB   Rn   RE   R~   R€   Rf   t   picklet   hashlibR   t   pasteR   R   t   objectR   R   R   R|   R#   Rr   Ro   R}   R&   R‚   R†   R‰   (    (    (    s7   /usr/local/lib/python2.7/dist-packages/paste/session.pyt   <module>   sD   "`h	