home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2028 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.0 KB  |  210 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import logging
  5. import time
  6. import sqlite3
  7. from _clientcookie import CookieJar, Cookie, MappingIterator
  8. from _util import isstringlike, experimental
  9. debug = logging.getLogger('mechanize.cookies').debug
  10.  
  11. class Firefox3CookieJar(CookieJar):
  12.     
  13.     def __init__(self, filename, autoconnect = True, policy = None):
  14.         experimental('Firefox3CookieJar is experimental code')
  15.         CookieJar.__init__(self, policy)
  16.         if filename is not None and not isstringlike(filename):
  17.             raise ValueError('filename must be string-like')
  18.         not isstringlike(filename)
  19.         self.filename = filename
  20.         self._conn = None
  21.         if autoconnect:
  22.             self.connect()
  23.         
  24.  
  25.     
  26.     def connect(self):
  27.         self._conn = sqlite3.connect(self.filename)
  28.         self._conn.isolation_level = 'DEFERRED'
  29.         self._create_table_if_necessary()
  30.  
  31.     
  32.     def close(self):
  33.         self._conn.close()
  34.  
  35.     
  36.     def _transaction(self, func):
  37.         
  38.         try:
  39.             cur = self._conn.cursor()
  40.             
  41.             try:
  42.                 result = func(cur)
  43.             finally:
  44.                 cur.close()
  45.  
  46.         except:
  47.             self._conn.rollback()
  48.             raise 
  49.  
  50.         self._conn.commit()
  51.         return result
  52.  
  53.     
  54.     def _execute(self, query, params = ()):
  55.         return (None, self._transaction)((lambda cur: cur.execute(query, params)))
  56.  
  57.     
  58.     def _query(self, query, params = ()):
  59.         cur = self._conn.cursor()
  60.         
  61.         try:
  62.             cur.execute(query, params)
  63.             for row in cur.fetchall():
  64.                 yield row
  65.         finally:
  66.             cur.close()
  67.  
  68.  
  69.     
  70.     def _create_table_if_necessary(self):
  71.         self._execute('CREATE TABLE IF NOT EXISTS moz_cookies (id INTEGER PRIMARY KEY, name TEXT,\n    value TEXT, host TEXT, path TEXT,expiry INTEGER,\n    lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER)')
  72.  
  73.     
  74.     def _cookie_from_row(self, row):
  75.         (pk, name, value, domain, path, expires, last_accessed, secure, http_only) = row
  76.         version = 0
  77.         domain = domain.encode('ascii', 'ignore')
  78.         path = path.encode('ascii', 'ignore')
  79.         name = name.encode('ascii', 'ignore')
  80.         value = value.encode('ascii', 'ignore')
  81.         secure = bool(secure)
  82.         rest = { }
  83.         if http_only:
  84.             rest['HttpOnly'] = None
  85.         
  86.         if name == '':
  87.             name = value
  88.             value = None
  89.         
  90.         initial_dot = domain.startswith('.')
  91.         domain_specified = initial_dot
  92.         discard = False
  93.         if expires == '':
  94.             expires = None
  95.             discard = True
  96.         
  97.         return Cookie(version, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, rest)
  98.  
  99.     
  100.     def clear(self, domain = None, path = None, name = None):
  101.         CookieJar.clear(self, domain, path, name)
  102.         where_parts = []
  103.         sql_params = []
  104.         if domain is not None:
  105.             where_parts.append('host = ?')
  106.             sql_params.append(domain)
  107.             if path is not None:
  108.                 where_parts.append('path = ?')
  109.                 sql_params.append(path)
  110.                 if name is not None:
  111.                     where_parts.append('name = ?')
  112.                     sql_params.append(name)
  113.                 
  114.             
  115.         
  116.         where = ' AND '.join(where_parts)
  117.         if where:
  118.             where = ' WHERE ' + where
  119.         
  120.         
  121.         def clear(cur):
  122.             cur.execute('DELETE FROM moz_cookies%s' % where, tuple(sql_params))
  123.  
  124.         self._transaction(clear)
  125.  
  126.     
  127.     def _row_from_cookie(self, cookie, cur):
  128.         expires = cookie.expires
  129.         if cookie.discard:
  130.             expires = ''
  131.         
  132.         domain = unicode(cookie.domain)
  133.         path = unicode(cookie.path)
  134.         name = unicode(cookie.name)
  135.         value = unicode(cookie.value)
  136.         secure = bool(int(cookie.secure))
  137.         if value is None:
  138.             value = name
  139.             name = ''
  140.         
  141.         last_accessed = int(time.time())
  142.         http_only = cookie.has_nonstandard_attr('HttpOnly')
  143.         query = cur.execute('SELECT MAX(id) + 1 from moz_cookies')
  144.         pk = query.fetchone()[0]
  145.         if pk is None:
  146.             pk = 1
  147.         
  148.         return (pk, name, value, domain, path, expires, last_accessed, secure, http_only)
  149.  
  150.     
  151.     def set_cookie(self, cookie):
  152.         if cookie.discard:
  153.             CookieJar.set_cookie(self, cookie)
  154.             return None
  155.         
  156.         def set_cookie(cur):
  157.             row = self._row_from_cookie(cookie, cur)
  158.             (name, unused, domain, path) = row[1:5]
  159.             cur.execute('DELETE FROM moz_cookies WHERE host = ? AND path = ? AND name = ?', (domain, path, name))
  160.             cur.execute('INSERT INTO moz_cookies VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\n', row)
  161.  
  162.         self._transaction(set_cookie)
  163.  
  164.     
  165.     def __iter__(self):
  166.         for cookie in MappingIterator(self._cookies):
  167.             yield cookie
  168.         
  169.         for row in self._query('SELECT * FROM moz_cookies ORDER BY name, path, host'):
  170.             yield self._cookie_from_row(row)
  171.         
  172.  
  173.     
  174.     def _cookies_for_request(self, request):
  175.         session_cookies = CookieJar._cookies_for_request(self, request)
  176.         
  177.         def get_cookies(cur):
  178.             query = cur.execute('SELECT host from moz_cookies')
  179.             domains = [ row[0] for row in query.fetchmany() ]
  180.             cookies = []
  181.             for domain in domains:
  182.                 cookies += self._persistent_cookies_for_domain(domain, request, cur)
  183.             
  184.             return cookies
  185.  
  186.         persistent_coookies = self._transaction(get_cookies)
  187.         return session_cookies + persistent_coookies
  188.  
  189.     
  190.     def _persistent_cookies_for_domain(self, domain, request, cur):
  191.         cookies = []
  192.         if not self._policy.domain_return_ok(domain, request):
  193.             return []
  194.         debug('Checking %s for cookies to return', domain)
  195.         query = cur.execute('SELECT * from moz_cookies WHERE host = ? ORDER BY path', (domain,))
  196.         cookies = [ self._cookie_from_row(row) for row in query.fetchmany() ]
  197.         last_path = None
  198.         r = []
  199.         for cookie in cookies:
  200.             if not self._policy.return_ok(cookie, request):
  201.                 debug('   not returning cookie')
  202.                 continue
  203.             
  204.             debug("   it's a match")
  205.             r.append(cookie)
  206.         
  207.         return r
  208.  
  209.  
  210.