Skip to content

Commit b1ac8d1

Browse files
committed
light refactoring
1 parent ef60977 commit b1ac8d1

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

synapseclient/models/download_list.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
)
2525
from synapseclient.models.table_components import CsvTableDescriptor
2626

27+
_ID_COLUMN = "ID"
28+
_VERSION_COLUMN = "versionNumber"
2729
_PATH_COLUMN = "path"
2830
_ERROR_COLUMN = "error"
2931

@@ -279,7 +281,7 @@ async def download_files_async(
279281
columns = DownloadList._validate_and_extend_columns(columns)
280282

281283
# 3. Download each file in the manifest
282-
downloaded_files = await DownloadList._download_all_rows(
284+
downloaded_files = await DownloadList._download_all_manifest_files(
283285
rows=rows,
284286
download_location=download_location,
285287
parallel=parallel,
@@ -366,42 +368,19 @@ def _validate_and_extend_columns(
366368
return list(columns) + [_PATH_COLUMN, _ERROR_COLUMN]
367369

368370
@staticmethod
369-
def _write_result_manifest(
370-
path: str,
371-
columns: list[str],
372-
rows: list[dict[str, Any]],
373-
) -> None:
374-
"""
375-
Write the annotated result rows to the output manifest CSV.
376-
Intended to be called via asyncio.to_thread to avoid blocking the
377-
event loop on synchronous file I/O.
378-
379-
Arguments:
380-
path: Destination path for the output manifest CSV.
381-
columns: Field names for the CSV header, including "path" and
382-
"error".
383-
rows: List of row dicts, each mutated by _download_row to
384-
include "path" and "error" values.
385-
"""
386-
with open(path, "w", newline="") as f:
387-
writer = csv.DictWriter(f, fieldnames=columns, extrasaction="ignore")
388-
writer.writeheader()
389-
writer.writerows(rows)
390-
391-
@staticmethod
392-
async def _download_all_rows(
371+
async def _download_all_manifest_files(
393372
rows: list[dict[str, Any]],
394373
download_location: Optional[str],
395374
parallel: bool = False,
396375
max_concurrent: int = 10,
397376
*,
398377
synapse_client: Optional["Synapse"] = None,
399378
) -> list[DownloadListItem]:
400-
"""Download all rows from the manifest, either sequentially or concurrently.
379+
"""Download all files from the manifest, either sequentially or concurrently.
401380
402381
Arguments:
403382
rows: List of row dicts from the manifest. Each row is mutated in
404-
place by _download_row to include "path" and
383+
place by _download_manifest_file to include "path" and
405384
"error" values.
406385
download_location: Directory to download files to.
407386
parallel: If True, rows are downloaded concurrently (bounded by
@@ -435,7 +414,7 @@ async def bounded_download(
435414
row: dict[str, Any],
436415
) -> Optional[DownloadListItem]:
437416
async with sem:
438-
return await DownloadList._download_row(
417+
return await DownloadList._download_manifest_file(
439418
row,
440419
download_location=download_location,
441420
synapse_client=synapse_client,
@@ -446,7 +425,7 @@ async def bounded_download(
446425
else:
447426
downloaded: list[DownloadListItem] = []
448427
for row in rows:
449-
item = await DownloadList._download_row(
428+
item = await DownloadList._download_manifest_file(
450429
row,
451430
download_location=download_location,
452431
synapse_client=synapse_client,
@@ -456,7 +435,7 @@ async def bounded_download(
456435
return downloaded
457436

458437
@staticmethod
459-
async def _download_row(
438+
async def _download_manifest_file(
460439
row: dict[str, Any],
461440
download_location: Optional[str] = None,
462441
*,
@@ -483,8 +462,8 @@ async def _download_row(
483462
from synapseclient.models.file import File
484463

485464
client = Synapse.get_client(synapse_client=synapse_client)
486-
entity_id = row["ID"]
487-
version_str = row.get("versionNumber")
465+
entity_id = row[_ID_COLUMN]
466+
version_str = row.get(_VERSION_COLUMN)
488467
version_number = int(version_str) if version_str else None
489468

490469
if version_number is None:
@@ -521,7 +500,7 @@ async def _save_result_manifest(
521500
"""Write the annotated rows to a new result manifest CSV and return its path.
522501
523502
Arguments:
524-
rows: List of row dicts, each mutated by _download_row to
503+
rows: List of row dicts, each mutated by _download_manifest_file to
525504
include "path" and "error" values.
526505
columns: Field names for the CSV header, including "path" and
527506
"error".
@@ -546,6 +525,29 @@ async def _save_result_manifest(
546525
)
547526
return path
548527

528+
@staticmethod
529+
def _write_result_manifest(
530+
path: str,
531+
columns: list[str],
532+
rows: list[dict[str, Any]],
533+
) -> None:
534+
"""
535+
Write the annotated result rows to the output manifest CSV.
536+
Intended to be called via asyncio.to_thread to avoid blocking the
537+
event loop on synchronous file I/O.
538+
539+
Arguments:
540+
path: Destination path for the output manifest CSV.
541+
columns: Field names for the CSV header, including "path" and
542+
"error".
543+
rows: List of row dicts, each mutated by _download_manifest_file to
544+
include "path" and "error" values.
545+
"""
546+
with open(path, "w", newline="") as f:
547+
writer = csv.DictWriter(f, fieldnames=columns, extrasaction="ignore")
548+
writer.writeheader()
549+
writer.writerows(rows)
550+
549551
@staticmethod
550552
async def get_manifest_async(
551553
*,

0 commit comments

Comments
 (0)