diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 73b679a1..35aaac5f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/python/black - rev: 19.10b0 + rev: 20.8b1 hooks: - id: black pass_filenames: true diff --git a/bg_atlasapi/list_atlases.py b/bg_atlasapi/list_atlases.py index 340749b3..0b0d255a 100644 --- a/bg_atlasapi/list_atlases.py +++ b/bg_atlasapi/list_atlases.py @@ -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 @@ -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, ) @@ -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: @@ -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", + ) + ) diff --git a/bg_atlasapi/utils.py b/bg_atlasapi/utils.py index cf698a7e..ef4960f5 100644 --- a/bg_atlasapi/utils.py +++ b/bg_atlasapi/utils.py @@ -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) @@ -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() diff --git a/requirements.txt b/requirements.txt index 66cc3457..f2c6c883 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,5 @@ pandas requests meshio click -rich -tqdm>=4.46.1 +rich>=8.0.0 bg-space>=0.5.0 diff --git a/setup.cfg b/setup.cfg index 317a65ae..dc196d16 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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