codecombat/scripts/devSetup/downloader.py
2014-01-03 10:32:13 -08:00

36 lines
1.4 KiB
Python

__author__ = 'schmatz'
from configuration import Configuration
import urllib
from dependency import Dependency
class Downloader:
def __init__(self,dependency):
assert isinstance(dependency, Dependency)
self.dependency = dependency
@property
def download_directory(self):
raise NotImplementedError
def download(self):
raise NotImplementedError
def download_file(self,url,filePath):
urllib.urlretrieve(url,filePath,self.__progress_bar_reporthook)
def decompress(self):
raise NotImplementedError
def check_download(self):
raise NotImplementedError
def __progress_bar_reporthook(self,blocknum,blocksize,totalsize):
#http://stackoverflow.com/a/13895723/1928667
#http://stackoverflow.com/a/3173331/1928667
bars_to_display = 70
amount_of_data_downloaded_so_far = blocknum * blocksize
if totalsize > 0:
progress_fraction = float(amount_of_data_downloaded_so_far) / float(totalsize)
progress_percentage = progress_fraction * 1e2
stringToDisplay = '\r[{0}] {1:.1f}%'.format('#'*int(bars_to_display*progress_fraction),progress_percentage)
print stringToDisplay,
if amount_of_data_downloaded_so_far >= totalsize:
print "\n",
else:
stringToDisplay = '\r File size unknown. Read {0} bytes.'.format(amount_of_data_downloaded_so_far)
print stringToDisplay,