[wwwsearch-commits] r43991 - in wwwsearch/mechanize/trunk: . mechanize
jjlee at codespeak.net
jjlee at codespeak.net
Sat Jun 2 14:23:54 CEST 2007
Author: jjlee
Date: Sat Jun 2 14:23:54 2007
New Revision: 43991
Modified:
wwwsearch/mechanize/trunk/functional_tests.py
wwwsearch/mechanize/trunk/mechanize/_mechanize.py
Log:
Add convenience method Browser.open_local_file(filename)
Modified: wwwsearch/mechanize/trunk/functional_tests.py
==============================================================================
--- wwwsearch/mechanize/trunk/functional_tests.py (original)
+++ wwwsearch/mechanize/trunk/functional_tests.py Sat Jun 2 14:23:54 2007
@@ -96,6 +96,14 @@
r = self.browser.open(url)
self.assert_("this string appears in this file ;-)" in r.read())
+ def test_open_local_file(self):
+ # Since the file: URL scheme is not well standardised, Browser has a
+ # special method to open files by name, for convenience:
+ br = mechanize.Browser()
+ response = br.open_local_file("mechanize/_mechanize.py")
+ self.assert_("def open_local_file(self, filename):" in
+ response.get_data())
+
def test_open_novisit(self):
def test_state(br):
self.assert_(br.request is None)
Modified: wwwsearch/mechanize/trunk/mechanize/_mechanize.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_mechanize.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_mechanize.py Sat Jun 2 14:23:54 2007
@@ -9,7 +9,8 @@
"""
-import urllib2, sys, copy, re
+import urllib2, sys, copy, re, os, urllib
+
from _useragent import UserAgentBase
from _html import DefaultFactory
@@ -24,6 +25,14 @@
class FormNotFoundError(Exception): pass
+def sanepathname2url(path):
+ urlpath = urllib.pathname2url(path)
+ if os.name == "nt" and urlpath.startswith("///"):
+ urlpath = urlpath[2:]
+ # XXX don't ask me about the mac...
+ return urlpath
+
+
class History:
"""
@@ -275,6 +284,11 @@
"""
return copy.copy(self._response)
+ def open_local_file(self, filename):
+ path = sanepathname2url(os.path.abspath(filename))
+ url = 'file://'+path
+ return self.open(url)
+
def set_response(self, response):
"""Replace current response with (a copy of) response.
More information about the wwwsearch-commits
mailing list