Support optional filename argument for retrieve() and urlretrieve(),

to specify where it should go (if specified, even local files will be
copied into the given file).
This commit is contained in:
Guido van Rossum 1996-06-11 00:16:27 +00:00
parent 34e1777807
commit a7e4b28422
1 changed files with 12 additions and 8 deletions

View File

@ -45,10 +45,13 @@ def urlopen(url):
if not _urlopener: if not _urlopener:
_urlopener = FancyURLopener() _urlopener = FancyURLopener()
return _urlopener.open(url) return _urlopener.open(url)
def urlretrieve(url): def urlretrieve(url, filename=None):
global _urlopener global _urlopener
if not _urlopener: if not _urlopener:
_urlopener = FancyURLopener() _urlopener = FancyURLopener()
if filename:
return _urlopener.retrieve(url, filename)
else:
return _urlopener.retrieve(url) return _urlopener.retrieve(url)
def urlcleanup(): def urlcleanup():
if _urlopener: if _urlopener:
@ -134,7 +137,7 @@ class URLopener:
# External interface # External interface
# retrieve(url) returns (filename, None) for a local object # retrieve(url) returns (filename, None) for a local object
# or (tempfilename, headers) for a remote object # or (tempfilename, headers) for a remote object
def retrieve(self, url): def retrieve(self, url, filename=None):
if self.tempcache and self.tempcache.has_key(url): if self.tempcache and self.tempcache.has_key(url):
return self.tempcache[url] return self.tempcache[url]
url1 = unwrap(url) url1 = unwrap(url)
@ -142,7 +145,7 @@ class URLopener:
self.tempcache[url] = self.tempcache[url1] self.tempcache[url] = self.tempcache[url1]
return self.tempcache[url1] return self.tempcache[url1]
type, url1 = splittype(url1) type, url1 = splittype(url1)
if not type or type == 'file': if not filename and (not type or type == 'file'):
try: try:
fp = self.open_local_file(url1) fp = self.open_local_file(url1)
del fp del fp
@ -151,12 +154,13 @@ class URLopener:
pass pass
fp = self.open(url) fp = self.open(url)
headers = fp.info() headers = fp.info()
if not filename:
import tempfile import tempfile
tfn = tempfile.mktemp() filename = tempfile.mktemp()
result = tfn, headers result = filename, headers
if self.tempcache is not None: if self.tempcache is not None:
self.tempcache[url] = result self.tempcache[url] = result
tfp = open(tfn, 'w') tfp = open(filename, 'w')
bs = 1024*8 bs = 1024*8
block = fp.read(bs) block = fp.read(bs)
while block: while block: