willip
e6857efe04
- move log-routine to pyf - add feature to check_update to properly check if the file exists
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
''' pyf - a simple python framework '''
|
|
|
|
import os
|
|
WORKDIR = os.getcwd()
|
|
|
|
|
|
|
|
TMP = '/tmp/quickos.log'
|
|
def log(message):
|
|
''' write to logfile TMP
|
|
@param message: the message which shall be written to the log file
|
|
'''
|
|
with open(TMP, 'a') as f:
|
|
f.write('{} {}\n'.format(time.ctime(), message))
|
|
|
|
|
|
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))
|
|
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)
|