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

Download show filename #37

Merged
merged 3 commits into from
Sep 12, 2024
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
32 changes: 24 additions & 8 deletions esgpull/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import annotations

import logging
from collections.abc import Container, Iterator, Mapping
from collections.abc import Iterator, Mapping
from enum import Enum, auto
from pathlib import Path
from typing import Any, cast

import tomlkit
from attrs import Factory, define, field
from attrs import Factory, define, field, fields
from attrs import has as attrs_has
from cattrs import Converter
from cattrs.gen import make_dict_unstructure_fn, override
from tomlkit import TOMLDocument
Expand Down Expand Up @@ -98,6 +99,7 @@ class Download:
max_concurrent: int = 5
disable_ssl: bool = False
disable_checksum: bool = False
show_filename: bool = False


@define
Expand Down Expand Up @@ -298,15 +300,29 @@ def update_item(
doc.setdefault(part, {})
doc = doc[part]
obj = getattr(obj, part)
value_type = getattr(fields(type(obj)), last).type
old_value = getattr(obj, last)
if isinstance(old_value, str):
...
elif isinstance(old_value, Container):
if attrs_has(value_type):
raise KeyError(key)
try:
value = int(value)
except ValueError:
elif value_type is str:
...
elif value_type is int:
try:
value = value_type(value)
except Exception:
...
elif value_type is bool:
if isinstance(value, bool):
...
elif isinstance(value, str):
if value.lower() in ["on", "true"]:
value = True
elif value.lower() in ["off", "false"]:
value = False
else:
raise ValueError(value)
else:
raise TypeError(value)
setattr(obj, last, value)
doc[last] = value
return old_value
Expand Down
19 changes: 17 additions & 2 deletions esgpull/esgpull.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DownloadColumn,
MofNCompleteColumn,
Progress,
ProgressColumn,
SpinnerColumn,
TaskID,
TextColumn,
Expand Down Expand Up @@ -337,7 +338,10 @@ async def iter_results(
data_node = (
f"[blue]{task.fields['data_node']}[/]"
)
msg = " · ".join([sha, size, speed, data_node])
parts = [sha, size, speed, data_node]
if self.config.download.show_filename:
parts.append(task.fields["filename"])
msg = " · ".join(parts)
logger.info(msg)
live.console.print(msg)
yield result
Expand Down Expand Up @@ -366,7 +370,7 @@ async def download(
MofNCompleteColumn(),
TimeRemainingColumn(compact=True, elapsed_when_finished=True),
)
file_progress = self.ui.make_progress(
file_columns: list[str | ProgressColumn] = [
TextColumn("[cyan][{task.id}] [b blue]{task.fields[sha]}"),
"[progress.percentage]{task.percentage:>3.0f}%",
BarColumn(),
Expand All @@ -376,6 +380,16 @@ async def download(
TransferSpeedColumn(),
"·",
TextColumn("[blue]{task.fields[data_node]}"),
]
if self.config.download.show_filename:
file_columns.extend(
[
"·",
TextColumn("{task.fields[filename]}"),
]
)
file_progress = self.ui.make_progress(
*file_columns,
transient=True,
)
file_task_shas = {}
Expand All @@ -387,6 +401,7 @@ async def download(
visible=False,
start=False,
sha=short_sha(file.sha),
filename=file.filename,
data_node=file.data_node,
)
callback = partial(file_progress.start_task, task_id)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ def test_deprecated_search_api_error(root, config_path):
raise
finally:
config_path.unlink()


def test_update_config(root, config_path):
root.mkdir(parents=True)
with open(config_path, "w") as f:
tomlkit.dump({}, f)
config = Config.load(root)
config.download.disable_ssl = True
config.update_item("download.disable_ssl", "false")
assert config.download.disable_ssl is False
config.update_item("download.disable_ssl", "true")
assert config.download.disable_ssl is True
config.update_item("download.disable_ssl", False)
assert config.download.disable_ssl is False
with pytest.raises(ValueError):
config.update_item("download.disable_ssl", "bad_value")
Loading