[wwwsearch-commits] r27558 - in wwwsearch/mechanize/trunk: . mechanize
jjlee at codespeak.net
jjlee at codespeak.net
Mon May 22 00:02:38 CEST 2006
Author: jjlee
Date: Mon May 22 00:02:36 2006
New Revision: 27558
Modified:
wwwsearch/mechanize/trunk/README.html.in
wwwsearch/mechanize/trunk/functional_tests.py
wwwsearch/mechanize/trunk/mechanize/__init__.py
wwwsearch/mechanize/trunk/mechanize/_pullparser.py
wwwsearch/mechanize/trunk/mechanize/_util.py
wwwsearch/mechanize/trunk/test.py
Log:
Show in web page examples how to munge responses; Run more doctests
Modified: wwwsearch/mechanize/trunk/README.html.in
==============================================================================
--- wwwsearch/mechanize/trunk/README.html.in (original)
+++ wwwsearch/mechanize/trunk/README.html.in Mon May 22 00:02:36 2006
@@ -152,6 +152,14 @@
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
+
+# Sometimes it's useful to process bad headers or bad HTML:
+response = br.response() # this is a copy of response
+headers = response.info() # currently, this is a mimetools.Message
+del headers["Content-type"] # get rid of (possibly multiple) existing headers
+headers["Content-type"] = "text/html; charset=utf-8"
+response.set_data(response.get_data().replace("<!---", "<!--"))
+br.set_response(response)
""")}
<p>mechanize exports the complete interface of <code>urllib2</code>:
@@ -275,6 +283,7 @@
<em>This is <strong>very</strong> roughly in order of priority</em>
<ul>
+ <li>Add .get_method() to Request.
<li>Test <code>.any_response()</code> two handlers case: ordering.
<li>Test referer bugs (frags and don't add in redirect unless orig
req had Referer)
@@ -282,6 +291,7 @@
<li>Proper XHTML support!
<li>Make encoding_finder public, I guess (but probably improve it first).
(For example: support Mark Pilgrim's universal encoding detector?)
+ <li>Continue with the de-crufting enabled by requirement for Python 2.3.
<li>Fix BeautifulSoup support to use a single BeautifulSoup instance
per page.
<li>Test BeautifulSoup support better / fix encoding issue.
Modified: wwwsearch/mechanize/trunk/functional_tests.py
==============================================================================
--- wwwsearch/mechanize/trunk/functional_tests.py (original)
+++ wwwsearch/mechanize/trunk/functional_tests.py Mon May 22 00:02:36 2006
@@ -71,8 +71,7 @@
class ResponseTests(TestCase):
def test_seek(self):
- from mechanize import Browser
- br = Browser()
+ br = mechanize.Browser()
r = br.open("http://wwwsearch.sourceforge.net/")
html = r.read()
r.seek(0)
@@ -90,8 +89,7 @@
"Hello ClientCookie functional test suite.\n")
def test_set_response(self):
- from mechanize import Browser
- br = Browser()
+ br = mechanize.Browser()
r = br.open("http://wwwsearch.sourceforge.net/")
html = r.read()
self.assertEqual(br.title(), "Python bits")
@@ -109,7 +107,20 @@
self.assertEqual(br.response().read(), newhtml)
self.assertEqual(list(br.links())[0].url, "spam")
- def test_close_pickle_load(self):
+ def test_new_response(self):
+ br = mechanize.Browser()
+ data = "<html><head><title>Test</title></head><body><p>Hello.</p></body></html>"
+ response = mechanize.make_response(
+ data,
+ [("Content-type", "text/html")],
+ "http://example.com/",
+ 200,
+ "OK"
+ )
+ br.set_response(response)
+ self.assertEqual(br.response().get_data(), data)
+
+ def hidden_test_close_pickle_load(self):
print ("Test test_close_pickle_load is expected to fail unless Python "
"standard library patch http://python.org/sf/1144636 has been "
"applied")
Modified: wwwsearch/mechanize/trunk/mechanize/__init__.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/__init__.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/__init__.py Mon May 22 00:02:36 2006
@@ -18,8 +18,7 @@
# misc
from _util import http2time as str2time
-# XXXXX sort out what people should be using!
-from _util import response_seek_wrapper
+from _util import response_seek_wrapper, make_response
from _urllib2_support import HeadParser
try:
from _urllib2_support import XHTMLCompatibleHeadParser
Modified: wwwsearch/mechanize/trunk/mechanize/_pullparser.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_pullparser.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_pullparser.py Mon May 22 00:02:36 2006
@@ -50,7 +50,7 @@
>>> t = Token("starttag", "a", [("href", "http://www.python.org/")])
>>> t == ("starttag", "a", [("href", "http://www.python.org/")])
True
- >>> t.type, t.data == "starttag", "a"
+ >>> (t.type, t.data) == ("starttag", "a")
True
>>> t.attrs == [("href", "http://www.python.org/")]
True
@@ -324,3 +324,11 @@
self._tokenstack.append(Token("starttag", tag, attrs))
def unknown_endtag(self, tag):
self._tokenstack.append(Token("endtag", tag))
+
+
+def _test():
+ import doctest, _pullparser
+ return doctest.testmod(_pullparser)
+
+if __name__ == "__main__":
+ _test()
Modified: wwwsearch/mechanize/trunk/mechanize/_util.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_util.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_util.py Mon May 22 00:02:36 2006
@@ -8,7 +8,7 @@
"""
-import re, string, time, copy, urllib
+import re, string, time, copy, urllib, mimetools
from types import TupleType
from cStringIO import StringIO
@@ -631,3 +631,20 @@
self._url, self._headers, self.code, self.msg)
state["wrapped"] = new_wrapped
return state
+
+def make_response(data, headers, url, code, msg):
+ """Convenient factory for objects implementing response interface.
+
+ data: string containing response body data
+ headers: sequence of (name, value) pairs
+ url: URL of response
+ code: integer response code (e.g. 200)
+ msg: string response code message (e.g. "OK")
+
+ """
+ hdr_text = []
+ for name_value in headers:
+ hdr_text.append("%s: %s" % name_value)
+ mime_headers = mimetools.Message(StringIO("\n".join(hdr_text)))
+ r = closeable_response(StringIO(data), mime_headers, url, code, msg)
+ return response_seek_wrapper(r)
Modified: wwwsearch/mechanize/trunk/test.py
==============================================================================
--- wwwsearch/mechanize/trunk/test.py (original)
+++ wwwsearch/mechanize/trunk/test.py Mon May 22 00:02:36 2006
@@ -105,9 +105,14 @@
# XXX temporary stop-gap to run doctests
assert os.path.isdir('test')
sys.path.insert(0, 'test')
- import test_mechanize
import doctest
+ import test_mechanize
doctest.testmod(test_mechanize)
+ from mechanize import _headersutil, _auth, _clientcookie, _pullparser
+ doctest.testmod(_headersutil)
+ doctest.testmod(_auth)
+ doctest.testmod(_clientcookie)
+ doctest.testmod(_pullparser)
import unittest
test_path = os.path.join(os.path.dirname(sys.argv[0]), "test")
More information about the wwwsearch-commits
mailing list