ó
è¾bc           @   s   d  Z  d d l Z d d l Z d d l m Z m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z d „  Z d e f d „  ƒ  YZ d S(   sµ  Load / save to libwww-perl (LWP) format files.

Actually, the format is slightly extended from that used by LWP's
(libwww-perl's) HTTP::Cookies, to avoid losing some RFC 2965 information
not recorded by LWP.

It uses the version string "2.0", though really there isn't an LWP Cookies
2.0 format.  This indicates that there is extra information in here
(domain_dot and # port_spec) while still being compatible with
libwww-perl, I hope.

iÿÿÿÿN(	   t   _warn_unhandled_exceptiont   FileCookieJart	   LoadErrort   Cookiet   MISSING_FILENAME_TEXTt   join_header_wordst   split_header_wordst   iso2timet	   time2isozc         C   s·  |  j  |  j f d |  j f d |  j f g } |  j d k	 rU | j d |  j f ƒ n  |  j rn | j d ƒ n  |  j r‡ | j d ƒ n  |  j	 r  | j d ƒ n  |  j
 r¹ | j d ƒ n  |  j rç | j d t t |  j ƒ ƒ f ƒ n  |  j r | j d ƒ n  |  j r"| j d
 |  j f ƒ n  |  j rD| j d |  j f ƒ n  |  j j ƒ  } | j ƒ  x. | D]& } | j | t |  j | ƒ f ƒ qdW| j d t |  j ƒ f ƒ t | g ƒ S(   s   Return string representation of Cookie in the LWP cookie file format.

    Actually, the format is extended a bit -- see module docstring.

    t   patht   domaint   portt	   path_spect	   port_spect
   domain_dott   securet   expirest   discardt   commentt
   commenturlt   versionN(   R   N(   R   N(   R   N(   R   N(   R   N(   t   namet   valueR	   R
   R   t   Nonet   appendt   path_specifiedt   port_specifiedt   domain_initial_dotR   R   R   t   floatR   R   t   comment_urlt   _restt   keyst   sortt   strR   R   (   t   cookiet   hR   t   k(    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyt   lwp_cookie_str   s8     	 	 	 	 	 		 	 	 
$t   LWPCookieJarc           B   s8   e  Z d  Z e e d „ Z d e e d „ Z d „  Z RS(   s[  
    The LWPCookieJar saves a sequence of "Set-Cookie3" lines.
    "Set-Cookie3" is the format used by the libwww-perl library, not known
    to be compatible with any browser, but which is easy to read and
    doesn't lose information about RFC 2965 cookies.

    Additional methods

    as_lwp_str(ignore_discard=True, ignore_expired=True)

    c         C   s€   t  j  ƒ  } g  } xW |  D]O } | r5 | j r5 q n  | rQ | j | ƒ rQ q n  | j d t | ƒ ƒ q Wd j | d g ƒ S(   sž   Return cookies as a string of "\n"-separated "Set-Cookie3" headers.

        ignore_discard and ignore_expires: see docstring for FileCookieJar.save

        s   Set-Cookie3: %ss   
t    (   t   timeR   t
   is_expiredR   R%   t   join(   t   selft   ignore_discardt   ignore_expirest   nowt   rR"   (    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyt
   as_lwp_str>   s    c         C   s   | d  k r6 |  j d  k	 r' |  j } q6 t t ƒ ‚ n  t | d ƒ } z* | j d ƒ | j |  j | | ƒ ƒ Wd  | j ƒ  Xd  S(   Nt   ws   #LWP-Cookies-2.0
(   R   t   filenamet
   ValueErrorR   t   opent   writeR0   t   close(   R+   R2   R,   R-   t   f(    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyt   saveN   s     c         C   s  | j  ƒ  } t j |  j | ƒ s: d | } t | ƒ ‚ n  t j ƒ  } d } d }	 d }
 ykxd| j  ƒ  } | d k rz Pn  | j | ƒ s q^ n  | t | ƒ j ƒ  } xt	 | g ƒ D]} | d \ } } i  } i  } x |	 D] } t
 | | <qÞ Wx« | d D]Ÿ \ } } | d  k	 r$| j ƒ  } n d  } | |
 k sB| |	 k rK| } n  | |	 k ry| d  k rlt } n  | | | <qý | |
 k r’| | | <qý | | | <qý W| j } | d ƒ } | d ƒ } | d  k	 rÜt | ƒ } n  | d  k rñt } n  | d ƒ } | j d ƒ } t | d ƒ | | | d	 ƒ | d ƒ | | | d ƒ | d
 ƒ | d ƒ | d ƒ | | | d ƒ | d ƒ | ƒ } | r‘| j r‘qµ n  | r­| j | ƒ r­qµ n  |  j | ƒ qµ Wq^ WWnA t k
 rÙ‚  n. t k
 rt ƒ  t d | | f ƒ ‚ n Xd  S(   Ns5   %r does not look like a Set-Cookie3 (LWP) format files   Set-Cookie3:R   R   R   R   R   R   R   R	   R
   R   R   R   R'   i    i   t   .s&   invalid Set-Cookie3 format file %r: %r(   R   R   R   R   R   (   R   R   R	   R
   R   R   R   (   t   readlinet   ret   searcht   magic_reR   R(   t
   startswitht   lent   stripR   t   FalseR   t   lowert   Truet   getR   R   R   R)   t
   set_cookiet   IOErrort	   ExceptionR    (   R+   R7   R2   R,   R-   t   magict   msgR.   t   headert   boolean_attrst   value_attrst   linet   dataR   R   t   standardt   restR$   t   vt   lcR#   R   R   R
   t   domain_specifiedt   c(    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyt   _really_load]   sŒ         	 							N(	   t   __name__t
   __module__t   __doc__RC   R0   R   RA   R8   RU   (    (    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyR&   1   s   (   RX   R(   R;   t	   cookielibR    R   R   R   R   R   R   R   R   R%   R&   (    (    (    s#   /usr/lib/python2.7/_LWPCookieJar.pyt   <module>   s   @	