Skip to content

Commit

Permalink
slight optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Dec 30, 2020
1 parent f25e6ac commit a355320
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions niftypet/ninst/install_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def askdir(title, initialdir):
ncpu = multiprocessing.cpu_count()

LOG_FORMAT = "%(levelname)s:%(asctime)s:%(name)s:%(funcName)s\n> %(message)s"
CHUNK_SIZE = 2 ** 15 # 32 kiB


class LogHandler(logging.StreamHandler):
Expand Down Expand Up @@ -130,36 +129,30 @@ def urlopen_cached(url, outdir, fname=None, mode="rb"):
fout = outdir / fname
cache = outdir / f"{fname}.url"
if not fout.is_file() or not cache.is_file() or cache.read_text().strip() != url:
req = request.Request(url=url)
with request.urlopen(req) as raw:
with tqdm.wrapattr(raw, "read", total=getattr(raw, "length", None)) as fd:
with fout.open("wb") as fo:
i = fd.read(CHUNK_SIZE)
while i:
fo.write(i)
i = fd.read(CHUNK_SIZE)
fi = request.urlopen(url)
with fout.open("wb") as raw:
with tqdm.wrapattr(raw, "write", total=getattr(fi, "length", None)) as fo:
copyfileobj(fi, fo)
cache.write_text(url)
return fout.open(mode)


def extractall(fzip, dest, desc="Extracting"):
"""zipfile.Zipfile(fzip).extractall(dest) with progress"""
dest = Path(dest).expanduser()
with ZipFile(fzip) as zipf:
with tqdm(
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
if not getattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
with zipf.open(i) as fi:
with open(fspath(dest / i.filename), "wb") as fo:
copyfileobj(CallbackIOWrapper(pbar.update, fi), fo)
with ZipFile(fzip) as zipf, tqdm(
desc=desc,
unit="B",
unit_scale=True,
unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
if not getattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
with zipf.open(i) as fi, open(fspath(dest / i.filename), "wb") as fo:
copyfileobj(CallbackIOWrapper(pbar.update, fi), fo)


def query_yesno(question):
Expand Down

0 comments on commit a355320

Please sign in to comment.