[wwwsearch-commits] r26905 - in wwwsearch/mechanize/trunk: attic
examples
jjlee at codespeak.net
jjlee at codespeak.net
Sat May 6 23:12:51 CEST 2006
Author: jjlee
Date: Sat May 6 23:12:50 2006
New Revision: 26905
Added:
wwwsearch/mechanize/trunk/attic/
wwwsearch/mechanize/trunk/attic/BSDDBCookieJar.py
- copied unchanged from r26864, wwwsearch/mechanize/trunk/examples/BSDDBCookieJar.py
wwwsearch/mechanize/trunk/attic/MSIEDBCookieJar.py
- copied unchanged from r26864, wwwsearch/mechanize/trunk/examples/MSIEDBCookieJar.py
Removed:
wwwsearch/mechanize/trunk/examples/BSDDBCookieJar.py
wwwsearch/mechanize/trunk/examples/MSIEDBCookieJar.py
Log:
Move crappy half-finished CookieJar modules out of examples dir
Deleted: /wwwsearch/mechanize/trunk/examples/BSDDBCookieJar.py
==============================================================================
--- /wwwsearch/mechanize/trunk/examples/BSDDBCookieJar.py Sat May 6 23:12:50 2006
+++ (empty file)
@@ -1,179 +0,0 @@
-"""Persistent CookieJar based on bsddb standard library module.
-
-Copyright 2003-2006 John J Lee <jjl at 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).
-
-**********************************************************************
-THIS WAS NEVER FULLY TESTED, AND IS NOT MAINTAINED!
-**********************************************************************
-
-"""
-
-from mechanize import CookieJar
-
-# this is importing from a private module: don't do this in your own code!
-from _ClientCookie import MappingIterator
-
-import bsddb
-import cPickle as pickle
-
-import logging
-debug = getLogger("mechanize").debug
-
-
-def CreateBSDDBCookieJar(filename, policy=None):
- """Return a BSDDBCookieJar given a BSDDB filename.
-
- Use this unless rather than directly using the BSDDBCookieJar constructor
- unless you know what you're doing.
-
- filename: filename for sleepycat BSDDB database; if the file doesn't exist,
- it will be created; otherwise, it will be opened
-
- **********************************************************************
- BSDDBCookieJar IS NOT FULLY TESTED!
- **********************************************************************
-
- """
- db = bsddb.db.DB()
- db.open(filename, bsddb.db.DB_HASH, bsddb.db.DB_CREATE, 0666)
- return BSDDBCookieJar(policy, db)
-
-class BSDDBIterator:
- # XXXX should this use thread lock?
- def __init__(self, cursor):
- iterator = None
- self._c = cursor
- self._i = iterator
- def __iter__(self): return self
- def close(self):
- if self._c is not None:
- self._c.close()
- self._c = self._i = self.next = self.__iter__ = None
- def next(self):
- while 1:
- if self._i is None:
- item = self._c.next()
- if item is None:
- self.close()
- raise StopIteration()
- domain, data = item
- self._i = MappingIterator(pickle.loads(data))
- try:
- return self._i.next()
- except StopIteration:
- self._i = None
- continue
- def __del__(self):
- # XXXX will this work?
- self.close()
-
-class BSDDBCookieJar(CookieJar):
- """CookieJar based on a BSDDB database, using the standard bsddb module.
-
- You should use CreateBSDDBCookieJar instead of the constructor, unless you
- know what you're doing.
-
- Note that session cookies ARE stored in the database (marked as session
- cookies), and will be written to disk if the database is file-based. In
- order to clear session cookies at the end of a session, you must call
- .clear_session_cookies().
-
- Call the .close() method after you've finished using an instance of this
- class.
-
- **********************************************************************
- THIS IS NOT FULLY TESTED!
- **********************************************************************
-
- """
- # XXX
- # use transactions to make multiple reader processes possible
- def __init__(self, policy=None, db=None):
- CookieJar.__init__(self, policy)
- del self._cookies
- if db is None:
- db = bsddb.db.DB()
- self._db = db
- def close(self):
- self._db.close()
- def __del__(self):
- # XXXX will this work?
- self.close()
- def clear(self, domain=None, path=None, name=None):
- if name is not None:
- if (domain is None) or (path is None):
- raise ValueError(
- "domain and path must be given to remove a cookie by name")
- elif path is not None:
- if domain is None:
- raise ValueError(
- "domain must be given to remove cookies by path")
-
- db = self._db
- self._cookies_lock.acquire()
- try:
- if domain is not None:
- data = db.get(domain)
- if data is not None:
- if path is name is None:
- db.delete(domain)
- else:
- c2 = pickle.loads(data)
- if name is None:
- del c2[path]
- else:
- del c2[path][name]
- else:
- raise KeyError("no domain '%s'" % domain)
- finally:
- self._cookies_lock.release()
- def set_cookie(self, cookie):
- db = self._db
- self._cookies_lock.acquire()
- try:
- # store 2-level dict under domain, like {path: {name: value}}
- data = db.get(cookie.domain)
- if data is None:
- c2 = {}
- else:
- c2 = pickle.loads(data)
- if not c2.has_key(cookie.path): c2[cookie.path] = {}
- c3 = c2[cookie.path]
- c3[cookie.name] = cookie
- db.put(cookie.domain, pickle.dumps(c2))
- finally:
- self._cookies_lock.release()
- def __iter__(self):
- return BSDDBIterator(self._db.cursor())
- def _cookies_for_request(self, request):
- """Return a list of cookies to be returned to server."""
- cookies = []
- for domain in self._db.keys():
- cookies.extend(self._cookies_for_domain(domain, request))
- return cookies
- def _cookies_for_domain(self, domain, request, unverifiable):
- debug("Checking %s for cookies to return", domain)
- if not self._policy.domain_return_ok(domain, request, unverifiable):
- return []
-
- data = self._db.get(domain)
- if data is None:
- return []
- cookies_by_path = pickle.loads(data)
-
- cookies = []
- for path in cookies_by_path.keys():
- if not self._policy.path_return_ok(path, request, unverifiable):
- continue
- for name, cookie in cookies_by_path[path].items():
- if not self._policy.return_ok(cookie, request, unverifiable):
- debug(" not returning cookie")
- continue
- debug(" it's a match")
- cookies.append(cookie)
-
- return cookies
Deleted: /wwwsearch/mechanize/trunk/examples/MSIEDBCookieJar.py
==============================================================================
--- /wwwsearch/mechanize/trunk/examples/MSIEDBCookieJar.py Sat May 6 23:12:50 2006
+++ (empty file)
@@ -1,140 +0,0 @@
-"""Persistent CookieJar based on MS Internet Explorer cookie database.
-
-Copyright 2003-2006 John J Lee <jjl at 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).
-
-**********************************************************************
-THIS DOESN'T WORK!
-
-It's just a sketch, to check the base class is OK.
-
-**********************************************************************
-
-"""
-
-from ClientCookie import MSIEBase, CookieJar
-from _Util import time2netscape
-
-def set_cookie_hdr_from_cookie(cookie):
- params = []
- if cookie.name is not None:
- params.append("%s=%s" % cookie.name, cookie.value)
- else:
- params.append(cookie.name)
- if cookie.expires:
- params.append("expires=" % time2netscape(cookie.expires))
- if cookie.domain_specified:
- params.append("Domain=%s" % cookie.domain)
- if cookie.path_specified:
- params.append("path=%s" % cookie.path)
- if cookie.port_specified:
- if cookie.port is None:
- params.append("Port")
- else:
- params.append("Port=%s" % cookie.port)
- if cookie.secure:
- params.append("secure")
-## if cookie.comment:
-## params.append("Comment=%s" % cookie.comment)
-## if cookie.comment_url:
-## params.append("CommentURL=%s" % cookie.comment_url)
- return "; ".join(params)
-
-class MSIEDBCookieJar(MSIEBase, CookieJar):
- """A CookieJar that relies on MS Internet Explorer's cookie database.
-
- XXX Require ctypes or write C extension? win32all probably requires
- latter.
-
- **********************************************************************
- THIS DOESN'T WORK!
-
- It's just a sketch, to check the base class is OK.
-
- **********************************************************************
-
- MSIEDBCookieJar, unlike MSIECookieJar, keeps no state for itself, but
- relies on the MS Internet Explorer's cookie database. It uses the win32
- API functions InternetGetCookie() and InternetSetCookie(), from the wininet
- library.
-
- Note that MSIE itself may impose additional conditions on cookie processing
- on top of that done by CookiePolicy. For cookie setting, the class tries
- to foil that by providing the request details and Set-Cookie header it
- thinks MSIE wants to see. For returning cookies to the server, it's up to
- MSIE.
-
- Note that session cookies ARE NOT written to disk and won't be accessible
- from other processes. .clear_session_cookies() has no effect.
-
- .clear_expired_cookies() has no effect: MSIE is responsible for this.
-
- .clear() will raise NotImplementedError unless all three arguments are
- given.
-
- """
- def __init__(self, policy=None):
- MSIEBase.__init__(self)
- FileCookieJar.__init__(self, policy)
- def clear_session_cookies(self): pass
- def clear_expired_cookies(self): pass
- def clear(self, domain=None, path=None, name=None):
- if None in [domain, path, name]:
- raise NotImplementedError()
- # XXXX
- url = self._fake_url(domain, path)
- hdr = "%s=; domain=%s; path=%s; max-age=0" % (name, domain, path)
- r = windll.InternetSetCookie(url, None, hdr)
- # XXX return value of InternetSetCookie?
- def _fake_url(self, domain, path):
- # to convince MSIE that Set-Cookie is OK
- return "http://%s%s" % (domain, path)
- def set_cookie(self, cookie):
- # XXXX
- url = self._fake_url(cookie.domain, cookie.path)
- r = windll.InternetSetCookie(
- url, None, set_cookie_hdr_from_cookie(cookie))
- # XXX return value of InternetSetCookie?
- def add_cookie_header(self, request, unverifiable=False):
- # XXXX
- cookie_header = windll.InternetGetCookie(request.get_full_url())
- # XXX return value of InternetGetCookie?
- request.add_unredirected_header(cookie_header)
- def __iter__(self):
- self._load_index_dat()
- return CookieJar.__iter__(self)
- def _cookies_for_request(self, request):
- raise NotImplementedError() # XXXX
- def _cookies_for_domain(self, domain, request):
- #raise NotImplementedError() # XXXX
- debug("Checking %s for cookies to return", domain)
- if not self._policy.domain_return_ok(domain, request):
- return []
-
- # XXXX separate out actual loading of cookie data, so only index.dat is
- # read in ._load_index_dat(), and ._really_load() calls that, then
- # ._delayload_domain for all domains if not self.delayload.
- # We then just call ._load_index_dat()
- self._delayload = False
- self._really_load()
-
- cookies_by_path = self._cookies.get(domain)
- if cookies_by_path is None:
- return []
-
- cookies = []
- for path in cookies_by_path.keys():
- if not self._policy.path_return_ok(path, request, unverifiable):
- continue
- for name, cookie in cookies_by_path[path].items():
- if not self._policy.return_ok(cookie, request, unverifiable):
- debug(" not returning cookie")
- continue
- debug(" it's a match")
- cookies.append(cookie)
-
- return cookies
-
More information about the wwwsearch-commits
mailing list