Skip to content

Commit b821655

Browse files
committed
Add experimental mtransfer class method
1 parent 2cf0f8f commit b821655

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

python/lsst/resources/_resourcePath.py

+53
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,59 @@ def _mexists(cls, uris: Iterable[ResourcePath]) -> dict[ResourcePath, bool]:
896896
results[uri] = exists
897897
return results
898898

899+
@classmethod
900+
def mtransfer(
901+
cls,
902+
transfer: str,
903+
*from_to: tuple[ResourcePath, ResourcePath],
904+
overwrite: bool = False,
905+
transaction: TransactionProtocol | None = None,
906+
) -> dict[ResourcePath, bool]:
907+
"""Transfer many files in bulk.
908+
909+
Parameters
910+
----------
911+
transfer : `str`
912+
Mode to use for transferring the resource. Generically there are
913+
many standard options: copy, link, symlink, hardlink, relsymlink.
914+
Not all URIs support all modes.
915+
*from_to : `tuple` [ `ResourcePath`, `ResourcePath` ]
916+
The source URI and the target URI.
917+
overwrite : `bool`, optional
918+
Allow an existing file to be overwritten. Defaults to `False`.
919+
transaction : `~lsst.resources.utils.TransactionProtocol`, optional
920+
A transaction object that can (depending on implementation)
921+
rollback transfers on error. Not guaranteed to be implemented.
922+
923+
Returns
924+
-------
925+
copy_status : `dict` [ `ResourcePath`, `bool` ]
926+
A dict of all the transfer attempts with a boolean indicating
927+
whether the transfer succeeded for the target URI.
928+
"""
929+
exists_executor = concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS)
930+
future_transfers = {
931+
exists_executor.submit(
932+
to_uri.transfer_from,
933+
from_uri,
934+
transfer=transfer,
935+
overwrite=overwrite,
936+
transaction=transaction,
937+
): to_uri
938+
for from_uri, to_uri in from_to
939+
}
940+
results: dict[ResourcePath, bool] = {}
941+
for future in concurrent.futures.as_completed(future_transfers):
942+
to_uri = future_transfers[future]
943+
try:
944+
future.result()
945+
except Exception:
946+
transferred = False
947+
else:
948+
transferred = True
949+
results[to_uri] = transferred
950+
return results
951+
899952
def remove(self) -> None:
900953
"""Remove the resource."""
901954
raise NotImplementedError()

0 commit comments

Comments
 (0)