ó
BÜXQc           @   sô   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z e j d k r` d d l Z n  d d l m	 Z	 m
 Z
 m Z m Z m Z e j d ƒ j Z d „  Z d Z d „  Z d	 „  Z d
 „  Z d d d „  ƒ  YZ d e e	 f d „  ƒ  YZ d S(   sp  Microsoft Internet Explorer cookie loading on Windows.

Copyright 2002-2003 Johnny Lee <typo_pl@hotmail.com> (MSIE Perl code)
Copyright 2002-2006 John J Lee <jjl@pobox.com> (The Python port)

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ÿÿÿÿNt   nt(   t   FileCookieJart	   CookieJart   Cookiet   MISSING_FILENAME_TEXTt	   LoadErrort	   mechanizec         C   sV   t  j t  j |  d t  j ƒ } y t  j | | ƒ d } Wn t k
 rQ d  } n X| S(   Ni    (   t   _winregt   OpenKeyt   HKEY_CURRENT_USERt   KEY_ALL_ACCESSt   QueryValueExt   WindowsErrort   None(   t   patht   leaft   keyt   value(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   regload   s    
l     }*{Gíc         C   s:   |  t  k  r% t d |  t  f ƒ ‚ n  t |  t  d ƒ d S(   s#  Convert from win32 filetime to seconds-since-epoch value.

    MSIE stores create and expire times as Win32 FILETIME, which is 64
    bits of 100 nanosecond intervals since Jan 01 1601.

    mechanize expects time in 32-bit value expressed in seconds since the
    epoch (Jan 01 1970).

    s"   filetime (%d) is before epoch (%d)l   €1i    (   t   WIN32_EPOCHt
   ValueErrort   divmod(   t   filetime(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt%   epoch_time_offset_from_win32_filetime#   s    
c         C   s   d t  |  ƒ S(   Ns   %02X(   t   ord(   t   c(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   binary_to_char3   s    c         C   s   d j  t t t |  ƒ ƒ ƒ S(   Nt    (   t   joint   mapR   t   list(   t   d(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   binary_to_str4   s    t   MSIEBasec           B   sŒ   e  Z e j d  ƒ Z d Z e j d ƒ Z e j d ƒ Z d Z d Z	 d „  Z
 d „  Z d „  Z e e d	 „ Z e e d d
 „ Z d „  Z RS(   s    Client UrlCache MMF Ver \d\.\d.*s   ð­s   ^([^/]+)(/.*)$s(   Cookie\:.+\@([!-ÿ]+).*?(.+\@[!-ÿ]+\.txt)s@   software\microsoft\windows\currentversion\explorer\shell folderst   Cookiesc         C   s   i  |  _  d  S(   N(   t   _delayload_domains(   t   self(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   __init__C   s    c         C   s|   |  j  j | ƒ } | d  k	 rx | \ } } } y |  j | | | ƒ Wn$ t t f k
 rj t d | ƒ qx X|  j  | =n  d  S(   Ns'   error reading cookie file, skipping: %s(   R#   t   getR   t   load_cookie_dataR   t   IOErrort   debug(   R$   t   domaint   delayload_infot   cookie_filet   ignore_discardt   ignore_expires(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   _delayload_domainF   s    	c      
   C   sˆ  t  d | ƒ g  } t | ƒ } zWxP| j ƒ  } | d k rA Pn  | j } | d „ } | d „ } | j ƒ  } | ƒ  } | ƒ  }	 | ƒ  }
 | ƒ  } | ƒ  } | ƒ  } | ƒ  } | ƒ  } d | | |	 |
 | | | | | f	 k sé | d k rí Pn  |  j j |	 ƒ } | r% | j d ƒ } | j d ƒ } | j i	 | d 6| d	 6| d
 6| d 6|
 d 6| d 6| d 6| d 6| d 6ƒ q% q% Wd  | j ƒ  X| S(   Ns   Loading MSIE cookies file: %sR   c         S   s   t  |  ƒ  j ƒ  ƒ S(   N(   t   longt   rstrip(   t   rl(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   getlong_   s    c         S   s   |  ƒ  j  ƒ  S(   N(   R1   (   R2   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   getstr`   s    t   *i   i   t   KEYt   VALUEt   DOMAINt   PATHt   FLAGSt   HIXPt   LOXPt   HICREATEt   LOCREATE(	   R)   t   opent   readlineR1   t   msie_domain_ret   searcht   groupt   appendt   close(   R$   t   filenamet   cookiest
   cookies_fhR   R2   R3   R4   R   t   domain_patht   flagst	   lo_expiret	   hi_expiret	   lo_createt	   hi_createt   sept   mR*   R   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   _load_cookies_from_fileS   sD     									c         C   sI  t  t j ƒ  ƒ } |  j | ƒ } x!| D]} | d } | d @d k } | d d >| d }	 t |	 ƒ }
 |
 | k  r t } n t } | d } | j d ƒ } | r­ t } n t } t d | d	 | d
 d t | | | | d t | |
 | d d i | d 6ƒ } | r| j	 rq( n  | r1| j
 | ƒ r1q( n  t j |  | ƒ q( Wd S(   sµ  Load cookies from file containing actual cookie data.

        Old cookies are kept unless overwritten by newly loaded ones.

        You should not call this method if the delayload attribute is set.

        I think each of these files contain all cookies for one user, domain,
        and path.

        filename: file containing cookies -- usually found in a file like
         C:\WINNT\Profiles\joe\Cookies\joe@blah[1].txt

        R:   i    i    R;   i    R<   R8   t   .R6   R7   R9   RJ   N(   t   intt   timeRQ   R   t   Truet   Falset
   startswithR   R   t   discardt
   is_expiredR   t
   set_cookie(   R$   RF   R-   R.   t   nowt   cookie_datat   cookieRJ   t   secureR   t   expiresRX   R*   t   initial_dott   domain_specifiedR   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyR'      s>    
	
	
	
c         C   sP   t  |  j |  j ƒ } t j j t j j | d ƒ ƒ } |  j | | | | ƒ d S(   s3   
        username: only required on win9x

        s	   INDEX.DATN(   R   t   reg_patht   reg_keyt   osR   t   normpathR   t   load(   R$   R-   R.   t   usernamet   cookies_dirRF   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   load_from_registry½   s    !c         C   sh  t  t j ƒ  ƒ } | d  k r4 t j d j ƒ  } n  t j j | ƒ } | j d ƒ } t	 | ƒ d k rz t
 d | ƒ ‚ n  | d  | d d !| d }	 }
 } t j d |
 ƒ d }
 |  j j |	 ƒ sÒ |
 d k rô t
 d	 t | ƒ |	 |
 f ƒ ‚ n  | j |
 d ƒ d
 } xWd } d | } | j | ƒ } t	 | ƒ | k rBPn  | | } | d  | d d !| d }	 }
 } t j d |
 ƒ d }
 |
 d | } |	 d k r;|	 d d |  j d f k sÍt d t |	 ƒ ‚ |	 d k rÝPn  |	 |  j k ròqn  | d k st ‚ |
 d k r| d k s"t ‚ | j | d ƒ qqn  |
 d k ry| j | ƒ } t	 | ƒ | k rlPn  | | } n  d | d | } t j | | t j ƒ } | rt j j | | j d ƒ ƒ } |  j sy |  j | | | ƒ Wq^t
 t f k
 rt d | ƒ q^Xqa| j d ƒ } | j d ƒ } | d k rH| |  } n  | | | f |  j | <qqd  S(   Nt   USERNAMEi   s   %s file is too shorti    i$   s   <Li    i @  s-   %s ['%s' %s] does not seem to contain cookiesi€   R   i   i   i   s   URL t   HASHt   LEAKt       s&   unrecognized MSIE index.dat record: %si   s   Cookie\:%s\@([!-ÿ]+).*?s   (%s\@[!-ÿ]+\.txt)s'   error reading cookie file, skipping: %st   /iÿÿÿÿ(   RS   RT   R   Rd   t   environt   lowerR   t   dirnamet   readt   lenR   t   structt   unpackt   magic_ret   matcht   strt   seekt   paddingt   AssertionErrorR    t   reRB   t   IR   RC   t	   delayloadR'   R(   R)   t   findR#   (   R$   t   indexRF   R-   R.   Rg   R[   t
   cookie_dirt   datat   sigt   sizet   sectort   to_readR   t	   more_datat	   cookie_reRP   R,   R*   t   i(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   _really_loadÇ   sx    #

#	 		N(   t   __name__t
   __module__R|   t   compileRv   Rz   RA   Rˆ   Rb   Rc   R%   R/   RQ   RV   R'   R   Ri   RŠ   (    (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyR!   6   s   			-=	t   MSIECookieJarc           B   sV   e  Z d  Z d e d d „ Z d „  Z d „  Z d „  Z d „  Z	 e e d d „ Z
 RS(   sk  FileCookieJar that reads from the Windows MSIE cookies database.

    MSIECookieJar can read the cookie files of Microsoft Internet Explorer
    (MSIE) for Windows version 5 on Windows NT and version 6 on Windows XP and
    Windows 98.  Other configurations may also work, but are untested.  Saving
    cookies in MSIE format is NOT supported.  If you save cookies, they'll be
    in the usual Set-Cookie3 format, which you can read back in using an
    instance of the plain old CookieJar class.  Don't save using the same
    filename that you loaded cookies from, because you may succeed in
    clobbering your MSIE cookies index file!

    You should be able to have LWP share Internet Explorer's cookies like
    this (note you need to supply a username to load_from_registry if you're on
    Windows 9x or Windows ME):

    cj = MSIECookieJar(delayload=1)
    # find cookies index file in registry and load cookies from it
    cj.load_from_registry()
    opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj))
    response = opener.open("http://example.com/")

    Iterating over a delayloaded MSIECookieJar instance will not cause any
    cookies to be read from disk.  To force reading of all cookies from disk,
    call read_all_cookies.  Note that the following methods iterate over self:
    clear_temporary_cookies, clear_expired_cookies, __len__, __repr__, __str__
    and as_string.

    Additional methods:

    load_from_registry(ignore_discard=False, ignore_expires=False,
                       username=None)
    load_cookie_data(filename, ignore_discard=False, ignore_expires=False)
    read_all_cookies()

    c         C   s'   t  j |  ƒ t j |  | | | ƒ d  S(   N(   R!   R%   R   (   R$   RF   R~   t   policy(    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyR%   P  s    c         C   s0   |  j  r |  j | j ƒ n  t j |  | ƒ d  S(   N(   R~   R/   R*   R   RZ   (   R$   R]   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyRZ   T  s    	c         C   s_   |  j  j ƒ  } | j |  j ƒ | j ƒ  } g  } x' | D] } | j |  j | | ƒ ƒ q8 W| S(   s2   Return a list of cookies to be returned to server.(   t   _cookiest   copyt   updateR#   t   keyst   extendt   _cookies_for_domain(   R$   t   requestt   domainsRG   R*   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   _cookies_for_requestY  s    c         C   sR   |  j  j | | ƒ s g  St d | ƒ |  j r? |  j | ƒ n  t j |  | | ƒ S(   Ns!   Checking %s for cookies to return(   t   _policyt   domain_return_okR)   R~   R/   R   R•   (   R$   R*   R–   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyR•   d  s    	c         C   s7   |  j  r3 x' |  j j ƒ  D] } |  j | ƒ q Wn  d S(   s   Eagerly read in all cookies.N(   R~   R#   R“   R/   (   R$   R*   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   read_all_cookiesl  s    	c         C   st   | d k r6 |  j d k	 r' |  j } q6 t t ƒ ‚ n  t | d ƒ } z |  j | | | | | ƒ Wd | j ƒ  Xd S(   sž   Load cookies from an MSIE 'index.dat' cookies index file.

        filename: full path to cookie index file
        username: only required on win9x

        t   rbN(   R   RF   R   R   R?   RŠ   RE   (   R$   RF   R-   R.   Rg   R€   (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyRf   r  s     N(   R‹   RŒ   t   __doc__R   RV   R%   RZ   R˜   R•   R›   Rf   (    (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyRŽ   ,  s   #				(    (   R   Rd   R|   RT   Rt   t   loggingt   nameR   t   _clientcookieR   R   R   R   R   t	   getLoggerR)   R   R   R   R   R    R!   RŽ   (    (    (    s=   /scratch/rashmi/Condor_Script/src/mechanize/_msiecookiejar.pyt   <module>
   s   <(					ö