ó
BÜXQc           @   s³   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z m Z m Z m	 Z	 d d l
 m Z m Z d d l m Z m Z e j d ƒ j 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.

Copyright 2002-2006 John J Lee <jjl@pobox.com>
Copyright 1997-1999 Gisle Aas (original libwww-perl code)

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD or ZPL 2.1 licenses (see the file
COPYING.txt included with the distribution).

iÿÿÿÿN(   t   reraise_unmasked_exceptionst   FileCookieJart   Cookiet   MISSING_FILENAME_TEXTt	   LoadError(   t   join_header_wordst   split_header_words(   t   iso2timet	   time2isozt	   mechanizec         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 r]| j d ƒ n  |  j ƒ  } | j ƒ  x0 | D]( } | j | t |  j | ƒ ƒ f ƒ qzW| j d t |  j ƒ f ƒ t | g ƒ S(   s“   Return string representation of Cookie in an 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   rfc2109t   versionN(   R   N(   R   N(   R   N(   s   secureN(   s   discardN(   s   rfc2109N(   t   namet   valueR
   R   R   t   Nonet   appendt   path_specifiedt   port_specifiedt   domain_initial_dotR   R   R   t   floatR   R   t   comment_urlR   t   nonstandard_attr_keyst   sortt   strt   get_nonstandard_attrR   R   (   t   cookiet   ht   keyst   k(    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyt   lwp_cookie_str   s<     	 	 	 	 	 		 	 	 	 
&t   LWPCookieJarc           B   s>   e  Z d  Z d Z e e d „ Z d e e d „ Z d „  Z	 RS(   sY  
    The LWPCookieJar saves a sequence of"Set-Cookie3" lines.
    "Set-Cookie3" is the format used by the libwww-perl libary, 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)

    s   ^\#LWP-Cookies-(\d+\.\d+)c         C   s    t  j  ƒ  } g  } xw |  D]o } | rE | j rE t d | j ƒ q n  | rq | j | ƒ rq t d | j ƒ q n  | j d t | ƒ ƒ q Wd j | d g ƒ S(   s   Return cookies as a string of "
"-separated "Set-Cookie3" headers.

        ignore_discard and ignore_expires: see docstring for FileCookieJar.save

        s$      Not saving %s: marked for discards      Not saving %s: expireds   Set-Cookie3: %ss   
t    (   t   timeR   t   debugR   t
   is_expiredR   R(   t   join(   t   selft   ignore_discardt   ignore_expirest   nowt   rR$   (    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyt
   as_lwp_strL   s    c         C   s‹   | d  k r6 |  j d  k	 r' |  j } q6 t t ƒ ‚ n  t | d ƒ } z4 t d ƒ | j d ƒ | j |  j | | ƒ ƒ Wd  | j ƒ  Xd  S(   Nt   ws   Saving LWP cookies files   #LWP-Cookies-2.0
(	   R   t   filenamet
   ValueErrorR   t   openR,   t   writeR4   t   close(   R/   R6   R0   R1   t   f(    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyt   save^   s     
c         C   sö  | j  ƒ  } t j |  j | ƒ s: d | } t | ƒ ‚ n  t j ƒ  } d } d }	 d }
 ysxl| 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 ƒ | | d ƒ ƒ } | rš| j ršqµ n  | r¶| j | ƒ r¶qµ n  |  j | ƒ qµ Wq^ Wn$ t t f ƒ t d | ƒ ‚ n Xd  S(   Ns#   %s does not seem to contain cookiess   Set-Cookie3:R   R   R   R   R   R   R   R   R
   R   R   R   R   R*   i    i   t   .s"   invalid Set-Cookie3 format file %s(   s	   port_specs	   path_specs
   domain_dots   secures   discards   rfc2109(   s   versions   ports   paths   domains   expiress   comments
   commenturl(   t   readlinet   ret   searcht   magic_reR   R+   t
   startswitht   lent   stripR   t   FalseR   t   lowert   Truet   getR   R   R   R-   t
   set_cookieR    t   IOError(   R/   R;   R6   R0   R1   t   magict   msgR2   t   headert   boolean_attrst   value_attrst   linet   dataR   R   t   standardt   restR'   t   vt   lcR%   R   R   R   t   domain_specifiedt   c(    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyt   _really_loadn   s†    
     	 						N(
   t   __name__t
   __module__t   __doc__RA   RG   R4   R   RE   R<   RX   (    (    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyR)   =   s
   (   R[   R+   R?   t   loggingt   _clientcookieR    R   R   R   R   t   _headersutilR   R   t   _utilR   R   t	   getLoggerR,   R(   R)   (    (    (    s<   /scratch/rashmi/Condor_Script/src/mechanize/_lwpcookiejar.pyt   <module>   s   $(	