quickos/pyf.py

32 lines
923 B
Python

''' pyf - a simple python framework '''
import os
WORKDIR = os.getcwd()
def prt(message):
''' print a message with style '''
print('::: {}'.format(message))
def download(url, file_name, progress=True):
''' download a given url to a file
@return the absolute filename
'''
from urllib.request import urlretrieve
if progress:
from tqdm import tqdm
prt('downloading {}'.format(url))
file_name = '{}/{}'.format(WORKDIR, url.split('/')[-1])
class TqdmUpTo(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)
with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=file_name) as t:
urlretrieve(url, filename=file_name, reporthook=t.update_to)
else:
urlretrieve(url, filename=file_name)