Browse Source

upgraded download function

feature/plugin
harshadyeola 10 years ago
parent
commit
f97c7f7ea2
  1. 93
      ee/core/download.py

93
ee/core/download.py

@ -3,42 +3,69 @@ import urllib.request
import urllib.error
import os
from ee.core.logging import Log
from ee.core.variables import EEVariables
from ee.cli.main import app
class EEDownload():
"""Method to download using urllib"""
def __init__():
app = app
log = app.log
def __init__(self, package, url, out_file=None, out_path=None):
"""
Download packages
package : string, name of package to be downloaded
url : string, url of the package to be downloaded from
out_file : filename the package to be downloaded to
out_path : directory name the package to be downloaded to.
"""
self.package = package
self.url = url
self.out_file = out_file
self.out_path = out_path
self.default_path = EEVariables.ee_downloads
pass
def download(self, packages):
"""Download packages, packges must be list in format of
[url, path, package name]"""
for package in packages:
url = package[0]
filename = package[1]
pkg_name = package[2]
try:
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)
Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
urllib.request.urlretrieve(url, filename)
Log.info(self, "{0}".format("[" + Log.ENDC + "Done"
+ Log.OKBLUE + "]"))
except urllib.error.URLError as e:
print("I am here")
Log.debug(self, "[{err}]".format(err=str(e.reason)))
Log.error(self, "Unable to donwload file, {0}"
.format(filename))
return False
except urllib.error.HTTPError as e:
Log.error(self, "Package download failed. {0}"
.format(pkg_name))
Log.debug(self, "[{err}]".format(err=str(e.reason)))
return False
except urllib.error.ContentTooShortError as e:
Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
Log.error(self, "Package download failed. The amount of the"
" downloaded data is less than "
"the expected amount \{0} ".format(pkg_name))
return False
def download(self):
"""
Download packages
"""
try:
if not self.out_path:
self.out_path = self.default_path
directory = os.path.dirname(self.out_path)
if not os.path.exists(directory):
os.makedirs(directory)
if not self.out_file:
self.out_file = self.url.split('/')[-1]
outfilepath = self.out_path + self.out_file
# directory = os.path.dirname(filename)
# if not os.path.exists(directory):
# os.makedirs(directory)
print(self.package)
self.log.info("Downloading {0}, please wait...".format(self.package))
urllib.request.urlretrieve(self.url, outfilepath)
return outfilepath
except urllib.error.URLError as e:
self.log.debug("[{err}]".format(err=str(e.reason)))
self.log.error("Package download failed. {0}"
.format(self.package))
return False
except urllib.error.HTTPError as e:
self.log.error("Package download failed. {0}"
.format(self.package))
self.log.debug("[{err}]".format(err=str(e.reason)))
return False
except urllib.error.ContentTooShortError as e:
self.log.debug("{0}{1}".format(e.errno, e.strerror))
self.log.error("Package download failed. The amount of the"
" downloaded data is less than "
"the expected amount \{0} ".format(self.package))
return False

Loading…
Cancel
Save