forked from niess/python-appimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.py
More file actions
40 lines (32 loc) · 988 Bytes
/
Copy pathurl.py
File metadata and controls
40 lines (32 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
try:
from urllib.request import urlopen as _urlopen
except ImportError:
from urllib2 import urlopen as _urlopen
try:
from urllib.request import urlretrieve as _urlretrieve
except ImportError:
import urllib2
_urlretrieve = None
from .log import debug
__all__ = ['urlopen', 'urlretrieve']
def urlopen(url, *args, **kwargs):
'''Open a remote file
'''
baseurl, urlname = os.path.split(url)
debug('DOWNLOAD', '%s from %s', baseurl, urlname)
return _urlopen(url, *args, **kwargs)
def urlretrieve(url, filename=None):
'''Download a file to disk
'''
if filename is None:
filename = os.path.basename(url)
debug('DOWNLOAD', '%s from %s', name, os.path.dirname(url))
else:
debug('DOWNLOAD', '%s as %s', url, filename)
if _urlretrieve is None:
data = urllib2.urlopen(url).read()
with open(filename, 'w') as f:
f.write(data)
else:
_urlretrieve(url, filename)