ó
—ã=Pc           @   s’   d  Z  d d l Z d d l m Z d d l m Z d d l Td d l m Z d d l m	 Z	 d d l
 m Z e e ƒ Z d	 e f d
 „  ƒ  YZ d S(   sF   
Contains transport interface (classes) and reference implementation.
iÿÿÿÿN(   t
   gettempdir(   t   urlparse(   t   *(   t   datetime(   t	   timedelta(   t	   getLoggert	   FileCachec           B   s†   e  Z d  Z d Z d Z d Z d d	 „ Z d
 „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s|  
    A file-based URL cache.
    @cvar fnprefix: The file name prefix.
    @type fnprefix: str
    @cvar fnsuffix: The file name suffix.
    @type fnsuffix: str
    @ivar duration: The cached file duration which defines how
        long the file will be cached.
    @type duration: (unit, value)
    @ivar location: The directory for the cached files.
    @type location: str
    t   sudst   httpt   monthst   weekst   dayst   hourst   minutest   secondsc         K   sJ   | d k r' t j j t ƒ  d ƒ } n  | |  _ d |  _ |  j |   d S(   s^  
        @param location: The directory for the cached files.
        @type location: str
        @param duration: The cached file duration which defines how
            long the file will be cached.  A duration=0 means forever.
            The duration may be: (months|weeks|days|hours|minutes|seconds).
        @type duration: {unit:value}
        R   i    N(   Ni    (   t   Nonet   ost   patht   joint   tmpt   locationt   durationt   setduration(   t   selfR   R   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   __init__2   s
    			c         K   sa   t  | ƒ d k r] | j ƒ  d } | d |  j k rQ t d t |  j ƒ ƒ ‚ n  | |  _ n  |  S(   s_  
        Set the caching duration which defines how long the 
        file will be cached.
        @param duration: The cached file duration which defines how
            long the file will be cached.  A duration=0 means forever.
            The duration may be: (months|weeks|days|hours|minutes|seconds).
        @type duration: {unit:value}
        i   i    s   must be: %s(   t   lent   itemst   unitst	   Exceptiont   strR   (   R   R   t   arg(    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyR   A   s    	c         C   s   | |  _  d S(   s   
        Set the location (directory) for the cached files.
        @param location: The directory for the cached files.
        @type location: str
        N(   R   (   R   R   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   setlocationQ   s    c         C   sP   y, t  j j |  j ƒ s+ t  j |  j ƒ n  Wn t j |  j d d ƒn X|  S(   sM   
        Make the I{location} directory if it doesn't already exits.
        t   exc_infoi   (   R   R   t   isdirR   t   makedirst   logt   debug(   R   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   mktmpY   s    c         C   sn   yL |  j  | ƒ } |  j | d ƒ } | j | j ƒ  ƒ | j ƒ  t | ƒ SWn t j | d d ƒ| SXd  S(   Nt   wR    i   (   t   _FileCache__fnt   opent   writet   readt   closeR#   R$   (   R   t   urlt   fpt   fnt   f(    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   putd   s    
c         C   s;   y- |  j  | ƒ } |  j | ƒ |  j | ƒ SWn n Xd  S(   N(   R'   t   validateR(   (   R   R,   R.   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   geto   s    c         C   s“   |  j  d d k  r d St j t j j | ƒ ƒ } i |  j  d |  j  d 6} | t |   } | t j ƒ  k  r t j	 d | ƒ t j
 | ƒ n  d S(   sŠ   
        Validate that the file has not expired based on the I{duration}.
        @param fn: The file name.
        @type fn: str
        i   Ni    s   %s expired, deleted(   R   t   dtt   fromtimestampR   R   t   getctimeR   t   nowR#   R$   t   remove(   R   R.   t   createdt   dt   expired(    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyR1   w   s    c         C   s   xˆ t  j |  j ƒ D]t } t  j j | ƒ r1 q n  | j |  j ƒ r | j |  j ƒ r t	 j
 d | ƒ t  j t  j j |  j | ƒ ƒ q q Wd  S(   Ns   deleted: %s(   R   t   listdirR   R   R!   t
   startswitht   fnprefixt   endswitht   fnsuffixR#   R$   R7   R   (   R   R.   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   clear†   s    $c         G   s   |  j  ƒ  t | | Œ S(   sK   
        Open the cache file making sure the directory is created.
        (   R%   R(   (   R   R.   t   args(    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyR(   Ž   s    
c         C   sY   |  j  | ƒ r t d ƒ ‚ n  d |  j t t | ƒ ƒ |  j f } t j j |  j	 | ƒ S(   Ns   URL %s, ignoreds   %s-%s.%s(
   t   _FileCache__ignoredR   R=   t   abst   hashR?   R   R   R   R   (   R   R,   R.   (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   __fn•   s    %c         C   s   t  | ƒ d } | d k S(   s    ignore urls based on protocol i    t   file(   s   file(   R   (   R   R,   t   protocol(    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt	   __ignored›   s    (   s   monthss   weekss   dayss   hourss   minutess   secondsN(   t   __name__t
   __module__t   __doc__R=   R?   R   R   R   R   R   R%   R0   R2   R1   R@   R(   R'   RB   (    (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyR       s   									(   RK   R   t   tempfileR    R   R   t   suds.transportR   R3   R   t   loggingR   RI   R#   t   CacheR   (    (    (    s,   suds-0.3.7-py2.6.egg/suds/transport/cache.pyt   <module>   s   
