ó
BÜXQc           @   s|   d  Z  d d l Z d d l Z d d l m Z m Z m Z d d l m Z m	 Z	 e j
 d ƒ j Z d e f d „  ƒ  YZ d S(   s  Firefox 3 "cookies.sqlite" cookie persistence.

Copyright 2008 John J Lee <jjl@pobox.com>

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	   CookieJart   Cookiet   MappingIterator(   t   isstringliket   experimentals   mechanize.cookiest   Firefox3CookieJarc           B   s¡   e  Z d  Z e d d „ Z d „  Z d „  Z d „  Z d d „ Z	 d d „ Z
 d „  Z d „  Z d d d d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   sˆ  Firefox 3 cookie jar.

    The cookies are stored in Firefox 3's "cookies.sqlite" format.

    Constructor arguments:

    filename: filename of cookies.sqlite (typically found at the top level
     of a firefox profile directory)
    autoconnect: as a convenience, connect to the SQLite cookies database at
     Firefox3CookieJar construction time (default True)
    policy: an object satisfying the mechanize.CookiePolicy interface

    Note that this is NOT a FileCookieJar, and there are no .load(),
    .save() or .restore() methods.  The database is in sync with the
    cookiejar object's state after each public method call.

    Following Firefox's own behaviour, session cookies are never saved to
    the database.

    The file is created, and an sqlite database written to it, if it does
    not already exist. The moz_cookies database table is created if it does
    not already exist.
    c         C   sk   t  d ƒ t j |  | ƒ | d  k	 rB t | ƒ rB t d ƒ ‚ n  | |  _ d  |  _ | rg |  j ƒ  n  d  S(   Ns&   Firefox3CookieJar is experimental codes   filename must be string-like(	   R   R    t   __init__t   NoneR   t
   ValueErrort   filenamet   _connt   connect(   t   selfR	   t   autoconnectt   policy(    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR   1   s    
		c         C   s;   d d  l  } | j |  j ƒ |  _ d |  j _ |  j ƒ  d  S(   Niÿÿÿÿt   DEFERRED(   t   sqlite3R   R	   R
   t   isolation_levelt   _create_table_if_necessary(   R   R   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR   ;   s    c         C   s   |  j  j ƒ  d  S(   N(   R
   t   close(   R   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR   A   s    c         C   s\   y1 |  j  j ƒ  } z | | ƒ } Wd  | j ƒ  XWn |  j  j ƒ  ‚  n X|  j  j ƒ  | S(   N(   R
   t   cursorR   t   rollbackt   commit(   R   t   funct   curt   result(    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   _transactionD   s    c            s   |  j  ‡  ‡ f d †  ƒ S(   Nc            s   |  j  ˆ  ˆ ƒ S(   N(   t   execute(   R   (   t   queryt   params(    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   <lambda>S   s    (   R   (   R   R   R   (    (   R   R   sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   _executeR   s    c         C   s?   |  j  j ƒ  } z | j | | ƒ | j ƒ  SWd  | j ƒ  Xd  S(   N(   R
   R   R   t   fetchallR   (   R   R   R   R   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   _queryU   s
    c         C   s   |  j  d ƒ d  S(   Ns¿   CREATE TABLE IF NOT EXISTS moz_cookies (id INTEGER PRIMARY KEY, name TEXT,
    value TEXT, host TEXT, path TEXT,expiry INTEGER,
    lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER)(   R   (   R   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR   ^   s    c         C   s  | \	 } } } } } } } }	 }
 d } | j  d d ƒ } | j  d d ƒ } | j  d d ƒ } | j  d d ƒ } t |	 ƒ }	 i  } |
 r” d  | d <n  | d k r¯ | } d  } n  | j d ƒ } | } t } | d k rå d  } t } n  t | | | d  t | | | | t |	 | | d  d  | ƒ S(   Ni    t   asciit   ignoret   HttpOnlyt    t   .(   t   encodet   boolR   t
   startswitht   Falset   TrueR   (   R   t   rowt   pkt   namet   valuet   domaint   patht   expirest   last_accessedt   securet	   http_onlyt   versiont   restt   initial_dott   domain_specifiedt   discard(    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   _cookie_from_rowd   s:    !			c            sâ   t  j |  | | | ƒ g  } g  ‰ | d  k	 r | j d ƒ ˆ j | ƒ | d  k	 r | j d ƒ ˆ j | ƒ | d  k	 rš | j d ƒ ˆ j | ƒ qš q n  d j | ƒ ‰  ˆ  r¿ d ˆ  ‰  n  ‡ ‡  f d †  } |  j | ƒ d  S(   Ns   host = ?s   path = ?s   name = ?s    AND s    WHERE c            s   |  j  d ˆ t ˆ  ƒ ƒ d  S(   Ns   DELETE FROM moz_cookies%s(   R   t   tuple(   R   (   t
   sql_paramst   where(    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   clear›   s    (   R    R?   R   t   appendt   joinR   (   R   R0   R1   R.   t   where_partsR?   (    (   R>   R=   sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR?   ‹   s"    c      	   C   sû   | j  } | j r d } n  t | j ƒ } t | j ƒ } t | j ƒ } t | j ƒ } t t | j	 ƒ ƒ } | d  k r‡ | } d } n  t t j ƒ  ƒ }	 | j d ƒ }
 | j d ƒ } | j ƒ  d } | d  k rÜ d } n  | | | | | | |	 | |
 f	 S(   NR%   R$   s#   SELECT MAX(id) + 1 from moz_cookiesi    i   (   R2   R:   t   unicodeR0   R1   R.   R/   R(   t   intR4   R   t   timet   has_nonstandard_attrR   t   fetchone(   R   t   cookieR   R2   R0   R1   R.   R/   R4   R3   R5   R   R-   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   _row_from_cookie    s&    					c            s@   ˆ j  r t j ˆ  ˆ ƒ d  S‡  ‡ f d †  } ˆ  j | ƒ d  S(   Nc            sX   ˆ  j  ˆ |  ƒ } | d d !\ } } } } |  j d | | | f ƒ |  j d | ƒ d  S(   Ni   i   s@   DELETE FROM moz_cookies WHERE host = ? AND path = ? AND name = ?s;   INSERT INTO moz_cookies VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
(   RI   R   (   R   R,   R.   t   unusedR0   R1   (   R   RH   (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt
   set_cookie¿   s    (   R:   R    RK   R   (   R   RH   RK   (    (   R   RH   sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyRK   º   s
    	c         c   sK   x t  |  j ƒ D] } | Vq Wx% |  j d ƒ D] } |  j | ƒ Vq/ Wd  S(   Ns3   SELECT * FROM moz_cookies ORDER BY name, path, host(   R   t   _cookiesR!   R;   (   R   RH   R,   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   __iter__Í   s
    		c            s;   t  j ˆ  ˆ ƒ } ‡  ‡ f d †  } ˆ  j | ƒ } | | S(   Nc            sf   |  j  d ƒ } g  | j ƒ  D] } | d ^ q } g  } x' | D] } | ˆ  j | ˆ |  ƒ 7} q? W| S(   Ns   SELECT host from moz_cookiesi    (   R   R    t   _persistent_cookies_for_domain(   R   R   R,   t   domainst   cookiesR0   (   R   t   request(    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   get_cookiesØ   s    #(   R    t   _cookies_for_requestR   (   R   RQ   t   session_cookiesRR   t   persistent_coookies(    (   R   RQ   sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyRS   Ö   s    c   
      C   sý   g  } |  j  j | | ƒ s g  St d | ƒ | j d | f ƒ } g  | j ƒ  D] } |  j | ƒ ^ qN } d  } g  } x | D]y }	 |	 j | k r¹ |  j  j |	 j | ƒ r¹ |	 j } q| n  |  j  j	 |	 | ƒ sÞ t d ƒ q| n  t d ƒ | j
 |	 ƒ q| W| S(   Ns!   Checking %s for cookies to returns6   SELECT * from moz_cookies WHERE host = ? ORDER BY paths      not returning cookies      it's a match(   t   _policyt   domain_return_okt   debugR   R    R;   R   R1   t   path_return_okt	   return_okR@   (
   R   R0   RQ   R   RP   R   R,   t	   last_patht   rRH   (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyRN   ã   s*    (	

N(    (    (   t   __name__t
   __module__t   __doc__R+   R   R   R   R   R   R   R!   R   R;   R?   RI   RK   RM   RS   RN   (    (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyR      s   
						'					(   R_   t   loggingRE   t   _clientcookieR    R   R   t   _utilR   R   t	   getLoggerRX   R   (    (    (    sA   /scratch/rashmi/Condor_Script/src/mechanize/_firefox3cookiejar.pyt   <module>	   s   