Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating pbar and a couple other things #87

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https:/python/black
rev: 19.10b0
rev: 20.8b1
hooks:
- id: black
pass_filenames: true
Expand Down
45 changes: 28 additions & 17 deletions bg_atlasapi/list_atlases.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rich.table import Table, box, Style
from rich.table import Table
from rich import print as rprint
from rich.panel import Panel

from bg_atlasapi import config, utils, descriptors
from bg_atlasapi.bg_atlas import BrainGlobeAtlas
Expand Down Expand Up @@ -109,8 +110,8 @@ def show_atlases(show_local_path=False):
if atlas not in atlases.keys():
atlases[str(atlas)] = dict(
downloaded=False,
local="[red]---[/red]",
version="[red]---[/red]",
local="",
version="",
latest_version=str(available_atlases[atlas]),
updated=None,
)
Expand All @@ -119,13 +120,14 @@ def show_atlases(show_local_path=False):
table = Table(
show_header=True,
header_style="bold green",
title="\n\nBrainglobe Atlases",
show_lines=True,
expand=False,
box=box.ROUNDED,
box=None,
)

table.add_column("Name", no_wrap=True, width=32)
table.add_column("Downloaded", justify="center")
table.add_column("Updated", justify="center")
table.add_column("Local version", justify="center")
table.add_column("Latest version", justify="center")
if show_local_path:
Expand All @@ -134,25 +136,34 @@ def show_atlases(show_local_path=False):
for n, (atlas, info) in enumerate(atlases.items()):
if info["downloaded"]:
downloaded = "[green]:heavy_check_mark:[/green]"

if info["version"] == info["latest_version"]:
updated = "[green]:heavy_check_mark:[/green]"
else:
updated = "[red dim]x"

else:
downloaded = "[red]---[/red]"
downloaded = ""
updated = ""

row = [
"[b]" + atlas + "[/b]",
"[bold]" + atlas,
downloaded,
info["version"],
info["latest_version"],
updated,
"[#c4c4c4]" + info["version"]
if "-" not in info["version"]
else "",
"[#c4c4c4]" + info["latest_version"],
]

if show_local_path:
row.append(info["local"])

table.add_row(*row)

if info["updated"] is not None:
if not info["updated"]:
table.row_styles.append(
Style(color="black", bgcolor="magenta2")
)

rprint(table)
rprint(
Panel.fit(
table,
width=88,
title="Brainglobe Atlases",
)
)
43 changes: 33 additions & 10 deletions bg_atlasapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import requests
import logging
import configparser
from tqdm.auto import tqdm
from rich.progress import (
BarColumn,
DownloadColumn,
TextColumn,
TransferSpeedColumn,
TimeRemainingColumn,
Progress,
)

logging.getLogger("urllib3").setLevel(logging.WARNING)

Expand Down Expand Up @@ -79,19 +86,35 @@ def retrieve_over_http(url, output_file_path):
Full file destination for download.

"""
# Make Rich progress bar
progress = Progress(
TextColumn("[bold]Downloading...", justify="right"),
BarColumn(bar_width=None),
"{task.percentage:>3.1f}%",
"•",
DownloadColumn(),
"• speed:",
TransferSpeedColumn(),
"• ETA:",
TimeRemainingColumn(),
)

CHUNK_SIZE = 4096
response = requests.get(url, stream=True)

try:
with tqdm.wrapattr(
open(output_file_path, "wb"),
"write",
miniters=1,
total=int(response.headers.get("content-length", 0)),
desc=output_file_path.name,
) as fout:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
fout.write(chunk)
with progress:
task_id = progress.add_task(
"download",
filename=output_file_path.name,
start=True,
total=int(response.headers.get("content-length", 0)),
)

with open(output_file_path, "wb") as fout:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
fout.write(chunk)
progress.update(task_id, advance=len(chunk), refresh=True)

except requests.exceptions.ConnectionError:
output_file_path.unlink()
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ pandas
requests
meshio
click
rich
tqdm>=4.46.1
rich>=8.0.0
bg-space>=0.5.0
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ search = __version__ = "{current_version}"
replace = __version__ = "{new_version}"

[flake8]
ignore = E203,W503,E501,E731,C901,W291,W293,E741,F401
ignore = E203,W503,E501,E731,C901,W291,W293,E741
max-line-length = 79
max-complexity = 18
exclude = __init__.py