Skip to content

Commit a01ae4a

Browse files
committed
fix: new_task usage
1 parent afd2c34 commit a01ae4a

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/mega/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import argparse
22
import asyncio
3+
import logging
34
from pathlib import Path
45
from pprint import pprint
56

67
from mega import env
78
from mega.client import MegaNzClient
89
from mega.utils import setup_logger
910

11+
logger = logging.getLogger(__name__)
12+
1013

1114
async def run() -> None:
1215
setup_logger(__name__)
@@ -29,8 +32,9 @@ async def run() -> None:
2932
async with MegaNzClient() as mega:
3033
await mega.login(env.EMAIL, env.PASSWORD)
3134
stats = await mega.get_account_stats()
32-
pprint(stats) # noqa: T203
33-
public_handle, public_key = mega.parse_file_url(args.url)
35+
pprint(stats.dump()) # noqa: T203
36+
public_handle, public_key = mega.parse_folder_url(args.url)
37+
logger.info(f"Downloading {args.url}")
3438
with mega.show_progress_bar():
3539
await mega.download_public_folder(public_handle, public_key, args.output_dir)
3640

src/mega/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def _upload(self, file_path: str | PathLike[str], dest_node_id: NodeID) ->
140140
file_path = Path(file_path)
141141
file_size = file_path.stat().st_size
142142

143-
with progress.new_task(file_path.name, file_size):
143+
with progress.new_task(file_path.name, file_size, "UP"):
144144
file_id, crypto = await upload.upload(self._api, file_path, file_size)
145145
return await upload.finish_upload(
146146
self._api,
@@ -168,7 +168,7 @@ async def _download_file(
168168
name = output_name or Attributes.parse(decrypt_attr(b64_url_decode(file_info._at), crypto.key)).name
169169
output_path = Path(output_folder or Path()) / name
170170

171-
with progress.new_task(output_path.name, file_info.size):
171+
with progress.new_task(output_path.name, file_info.size, "DOWN"):
172172
async with self._api.download(file_info.url) as response:
173173
return await download.stream_download(
174174
response.content,

src/mega/progress.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ def __call__(self, description: str, total: float, kind: Literal["UP", "DOWN"])
2828
current_hook: ContextVar[ProgressHook] = ContextVar("current_hook", default=lambda _: None)
2929

3030

31+
def _truncate_desc(desc: str, length: int = 30, placeholder: str = "...") -> str:
32+
if len(desc) < length:
33+
return desc
34+
35+
return f"{desc[: length - len(placeholder)]}{placeholder}"
36+
37+
3138
@contextlib.contextmanager
3239
def new_task(description: str, total: float, kind: Literal["UP", "DOWN"]) -> Generator[None]:
3340
factory = _PROGRESS_HOOK_FACTORY.get()
3441
if factory is None:
3542
yield
3643
return
3744

38-
with factory(description, total, kind) as progress_hook:
45+
with factory(_truncate_desc(description), total, kind) as progress_hook:
3946
token = current_hook.set(progress_hook)
4047
try:
4148
yield
@@ -69,7 +76,7 @@ async def task(name: str) -> None:
6976
total = 1e6 * random.randint(200, 2000)
7077
max_step = int(total / 20)
7178
kind = random.choice(("UP", "DOWN"))
72-
with new_task(name, total, kind=kind):
79+
with new_task(name, total, kind):
7380
advance = current_hook.get()
7481
done = 0
7582
while done < total:

0 commit comments

Comments
 (0)