Skip to content

Commit 86f323a

Browse files
committed
more async
1 parent fe32ad8 commit 86f323a

File tree

7 files changed

+110
-181
lines changed

7 files changed

+110
-181
lines changed

pyunicore/aio/client.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,14 @@ async def get_transfers(self, offset=0, num=200, tags=[]) -> list[Transfer]:
407407
(for long lists, the server might not return all results!).
408408
Use the optional tag list to filter the results."""
409409
q_params = _url_params(offset, num, tags)
410-
urls = await self.transport.get(url=self.links["transfers"], params=q_params)["transfers"]
410+
url = (await self.links)["transfers"]
411+
urls = (await self.transport.get(url=url, params=q_params))["transfers"]
411412
return [Transfer(self.transport, url) for url in urls]
412413

413414
async def get_applications(self) -> list[Application]:
414415
apps = []
415416
for url in (await self.transport.get(url=(await self.links)["factories"]))["factories"]:
416-
for app in self.transport.get(url=url)["applications"]:
417+
for app in (await self.transport.get(url=url))["applications"]:
417418
apps.append(Application(self.transport, url + "/applications/" + app))
418419
return apps
419420

@@ -509,23 +510,19 @@ def __init__(
509510
self.submit_url = submit_url
510511

511512
@property
512-
def name(self):
513-
return self.properties["ApplicationName"]
513+
async def name(self):
514+
return (await self.properties)["ApplicationName"]
514515

515516
@property
516-
def version(self):
517-
return self.properties["ApplicationVersion"]
517+
async def version(self):
518+
return (await self.properties)["ApplicationVersion"]
518519

519520
@property
520-
def options(self):
521-
return self.properties["Options"]
521+
async def options(self):
522+
return (await self.properties)["Options"]
522523

523524
def __repr__(self):
524-
return "Application {} {} @ {}".format(
525-
self.name,
526-
self.version,
527-
self.submit_url,
528-
)
525+
return f"Application {self.resource_url}"
529526

530527
__str__ = __repr__
531528

@@ -936,16 +933,19 @@ def __init__(self, storage: Storage, path_url: str, name: str, cache_time=_DEFAU
936933
self.name = name
937934
self.storage = storage
938935

936+
@property
939937
def isdir(self):
940938
"""is a directory"""
941939
return False
942940

941+
@property
943942
def isfile(self):
944943
"""is a file"""
945944
return False
946945

946+
@property
947947
async def size(self):
948-
return (await self.properties)["size"]
948+
return int((await self.properties)["size"])
949949

950950
async def get_metadata(self, name: str = None):
951951
if name:
@@ -967,6 +967,7 @@ class PathDir(Path):
967967
def __init__(self, storage: Storage, path_url: str, name: str, cache_time=_DEFAULT_CACHE_TIME):
968968
super().__init__(storage, path_url, name, cache_time)
969969

970+
@property
970971
def isdir(self):
971972
return True
972973

@@ -1000,10 +1001,10 @@ async def download(self, file: str | typing.Any):
10001001
chunk_size = 10 * 1024
10011002
if isinstance(file, str):
10021003
with open(file, "wb") as fd:
1003-
for chunk in resp.iter_raw(chunk_size):
1004+
async for chunk in resp.aiter_raw(chunk_size):
10041005
fd.write(chunk)
10051006
else:
1006-
for chunk in resp.iter_raw(chunk_size):
1007+
async for chunk in resp.aiter_raw(chunk_size):
10071008
file.write(chunk)
10081009

10091010
@asynccontextmanager
@@ -1067,10 +1068,15 @@ async def status(self) -> TransferStatus:
10671068
async def is_running(self):
10681069
"""checks whether this transfer is still running"""
10691070
return (await self.status) not in (
1070-
"DONE",
1071-
"FAILED",
1071+
TransferStatus.DONE,
1072+
TransferStatus.FAILED,
10721073
)
10731074

1075+
@property
1076+
async def transferred_bytes(self):
1077+
"""gets the number of transferred bytes"""
1078+
return int((await self.properties)["transferredBytes"])
1079+
10741080
async def abort(self):
10751081
"""abort this transfer"""
10761082
url = (await self.links)["action:abort"]

tests/integration/test_async_basic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ async def test_issue_auth_token(self):
3434
token = await client.issue_auth_token(lifetime=600, limited=True)
3535
print("token: %s" % token)
3636

37+
async def test_list_application(self):
38+
print("*** test_list_application")
39+
async with self.get_client() as client:
40+
for a in await client.get_applications():
41+
print(f"{a} {await a.name} {await a.version} {await a.options}")
42+
3743

3844
if __name__ == "__main__":
3945
unittest.main()

tests/integration/test_async_storage.py

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import os
22
import unittest
33
from io import BytesIO
4-
from time import sleep
4+
5+
import aiofiles
56

67
import pyunicore.aio.client as uc_client
78
import pyunicore.credentials as uc_credentials
89

910

10-
class TestBasic(unittest.IsolatedAsyncioTestCase):
11+
class TestAsyncStorage(unittest.IsolatedAsyncioTestCase):
1112
def setUp(self):
1213
pass
1314

@@ -36,57 +37,66 @@ async def test_list_storages(self):
3637
home = s
3738
break
3839
self.assertIsNotNone(home)
39-
home.listdir()
40-
home.listdir(".")
41-
home.listdir("/")
40+
await home.listdir()
41+
await home.listdir(".")
42+
await home.listdir("/")
4243

43-
def x_test_upload_download(self):
44+
async def test_upload_download(self):
4445
print("*** test_upload_download")
45-
home = self.get_home_storage()
46-
_path = "tests/integration/files/script.sh"
47-
_length = os.stat(_path).st_size
48-
with open(_path, "rb") as f:
49-
home.put(f, "script.sh")
50-
remote_file = home.stat("script.sh")
51-
self.assertEqual(_length, int(remote_file.properties["size"]))
52-
_out = BytesIO()
53-
remote_file.download(_out)
54-
self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8")))
46+
async with self.get_home_storage() as home:
47+
_path = "tests/integration/files/script.sh"
48+
_length = os.stat(_path).st_size
49+
async with aiofiles.open(_path, "rb") as f:
50+
await home.put(f, "script.sh")
51+
remote_file = await home.stat("script.sh")
52+
self.assertEqual(_length, await remote_file.size)
53+
_out = BytesIO()
54+
await remote_file.download(_out)
55+
self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8")))
5556

56-
def x_test_upload_download_data(self):
57+
async def test_upload_download_data(self):
5758
print("*** test_upload_download_data")
58-
home = self.get_home_storage()
59-
_data = "this is some test data"
60-
_length = len(_data)
61-
home.put(_data, "test.txt")
62-
remote_file = home.stat("test.txt")
63-
self.assertEqual(_length, int(remote_file.properties["size"]))
64-
_out = BytesIO()
65-
remote_file.download(_out)
66-
self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8")))
59+
async with self.get_home_storage() as home:
60+
_data = "this is some test data"
61+
_length = len(_data)
62+
await home.put(_data, "test.txt")
63+
remote_file = await home.stat("test.txt")
64+
self.assertEqual(_length, await remote_file.size)
65+
_out = BytesIO()
66+
await remote_file.download(_out)
67+
self.assertEqual(_length, len(str(_out.getvalue(), "UTF-8")))
6768

68-
def x_test_transfer(self):
69+
async def test_transfer(self):
6970
print("*** test_transfer")
70-
storage1 = self.get_home_storage()
71-
_path = "tests/integration/files/script.sh"
72-
_length = os.stat(_path).st_size
73-
with open(_path, "rb") as f:
74-
storage1.put(f, "script.sh")
75-
site_client = self.get_client()
76-
storage2 = site_client.new_job({}).working_dir
77-
transfer = storage2.receive_file(storage1.resource_url + "/files/script.sh", "script.sh")
78-
print(transfer)
79-
while transfer.is_running():
80-
sleep(2)
81-
print("Transferred bytes: %s" % transfer.properties["transferredBytes"])
82-
self.assertEqual(_length, int(transfer.properties["transferredBytes"]))
83-
transfer2 = storage1.send_file("script.sh", storage2.resource_url + "/files/script2.sh")
84-
print(transfer2)
85-
transfer2.poll()
86-
print("Transferred bytes: %s" % transfer2.properties["transferredBytes"])
87-
self.assertEqual(_length, int(transfer2.properties["transferredBytes"]))
88-
for t in site_client.get_transfers():
89-
print(t)
71+
async with self.get_home_storage() as storage1:
72+
_path = "tests/integration/files/script.sh"
73+
_length = os.stat(_path).st_size
74+
async with aiofiles.open(_path, "rb") as f:
75+
await storage1.put(f, "script.sh")
76+
async with self.get_client() as site_client:
77+
j = await site_client.new_job({})
78+
storage2 = await j.working_dir
79+
await storage2._wait_until_ready()
80+
transfer = await storage2.receive_file(
81+
storage1.resource_url + "/files/script.sh", "script.sh"
82+
)
83+
print(transfer)
84+
await transfer.poll()
85+
self.assertFalse(await transfer.is_running)
86+
n = await transfer.transferred_bytes
87+
print("Transferred bytes: %s" % n)
88+
self.assertEqual(_length, n)
89+
90+
transfer2 = await storage1.send_file(
91+
"script.sh", storage2.resource_url + "/files/script2.sh"
92+
)
93+
print(transfer2)
94+
await transfer2.poll()
95+
n2 = await transfer2.transferred_bytes
96+
print("Transferred bytes: %s" % n2)
97+
self.assertEqual(_length, n2)
98+
for t in await site_client.get_transfers():
99+
print(t)
90100

91101

92102
if __name__ == "__main__":

tests/testing/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/testing/contexts.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/testing/pyunicore.py

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)