quickos/pyf.py

44 lines
1.3 KiB
Python
Raw Permalink Normal View History

''' pyf - a simple python framework '''
def log(message, log_file):
''' write @message to @log_file '''
with open(TMP, 'a') as f:
f.write('{} {}\n'.format(time.ctime(), message))
def prt(message):
2019-03-08 21:11:20 +00:00
''' print @message with style '''
print('::: {}'.format(message))
def download(url, file_name, progress=True):
2019-03-08 21:11:20 +00:00
#TODO check if donwload was successful, if not do stuff
''' download @url to @file_name '''
from urllib.request import urlretrieve
if progress:
from tqdm import tqdm
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)
def sha256(file_name):
''' return @string sha256sum of @file_name '''
import hashlib
block = 65536
hasher = hashlib.sha256()
with open(file_name, 'rb') as f:
buf = f.read(block)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(block)
return hasher.hexdigest()