Skip to content

Commit 8ad915c

Browse files
committed
Replace gen.coroutine/yeild with modern async/await
1 parent e6cfbd6 commit 8ad915c

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

binderhub/registry.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
from urllib.parse import urlparse
88

9-
from tornado import gen, httpclient
9+
from tornado import httpclient
1010
from tornado.httputil import url_concat
1111
from traitlets.config import LoggingConfigurable
1212
from traitlets import Dict, Unicode, default
@@ -185,8 +185,7 @@ def _default_password(self):
185185
base64.b64decode(b64_auth.encode("utf-8")).decode("utf-8").split(":", 1)[1]
186186
)
187187

188-
@gen.coroutine
189-
def get_image_manifest(self, image, tag):
188+
async def get_image_manifest(self, image, tag):
190189
client = httpclient.AsyncHTTPClient()
191190
url = "{}/v2/{}/manifests/{}".format(self.url, image, tag)
192191
# first, get a token to perform the manifest request
@@ -196,7 +195,7 @@ def get_image_manifest(self, image, tag):
196195
auth_username=self.username,
197196
auth_password=self.password,
198197
)
199-
auth_resp = yield client.fetch(auth_req)
198+
auth_resp = await client.fetch(auth_req)
200199
response_body = json.loads(auth_resp.body.decode("utf-8", "replace"))
201200

202201
if "token" in response_body.keys():
@@ -215,7 +214,7 @@ def get_image_manifest(self, image, tag):
215214
)
216215

217216
try:
218-
resp = yield client.fetch(req)
217+
resp = await client.fetch(req)
219218
except httpclient.HTTPError as e:
220219
if e.code == 404:
221220
# 404 means it doesn't exist

binderhub/repoproviders.py

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import escapism
1919
from prometheus_client import Gauge
2020

21-
from tornado import gen
2221
from tornado.httpclient import AsyncHTTPClient, HTTPError, HTTPRequest
2322
from tornado.httputil import url_concat
2423

@@ -161,21 +160,18 @@ def repo_config(self, settings):
161160
repo_config.update(config)
162161
return repo_config
163162

164-
@gen.coroutine
165-
def get_resolved_ref(self):
163+
async def get_resolved_ref(self):
166164
raise NotImplementedError("Must be overridden in child class")
167165

168-
@gen.coroutine
169-
def get_resolved_spec(self):
166+
async def get_resolved_spec(self):
170167
"""Return the spec with resolved ref."""
171168
raise NotImplementedError("Must be overridden in child class")
172169

173170
def get_repo_url(self):
174171
"""Return the git clone-able repo URL"""
175172
raise NotImplementedError("Must be overridden in the child class")
176173

177-
@gen.coroutine
178-
def get_resolved_ref_url(self):
174+
async def get_resolved_ref_url(self):
179175
"""Return the URL of repository at this commit in history"""
180176
raise NotImplementedError("Must be overridden in child class")
181177

@@ -216,12 +212,11 @@ class ZenodoProvider(RepoProvider):
216212
"""
217213
name = Unicode("Zenodo")
218214

219-
@gen.coroutine
220-
def get_resolved_ref(self):
215+
async def get_resolved_ref(self):
221216
client = AsyncHTTPClient()
222217
req = HTTPRequest("https://doi.org/{}".format(self.spec),
223218
user_agent="BinderHub")
224-
r = yield client.fetch(req)
219+
r = await client.fetch(req)
225220
self.record_id = r.effective_url.rsplit("/", maxsplit=1)[1]
226221
return self.record_id
227222

@@ -256,12 +251,11 @@ class FigshareProvider(RepoProvider):
256251
name = Unicode("Figshare")
257252
url_regex = re.compile(r"(.*)/articles/([^/]+)/(\d+)(/)?(\d+)?")
258253

259-
@gen.coroutine
260-
def get_resolved_ref(self):
254+
async def get_resolved_ref(self):
261255
client = AsyncHTTPClient()
262256
req = HTTPRequest("https://doi.org/{}".format(self.spec),
263257
user_agent="BinderHub")
264-
r = yield client.fetch(req)
258+
r = await client.fetch(req)
265259

266260
match = self.url_regex.match(r.effective_url)
267261
article_id = match.groups()[2]
@@ -298,20 +292,19 @@ def get_build_slug(self):
298292
class DataverseProvider(RepoProvider):
299293
name = Unicode("Dataverse")
300294

301-
@gen.coroutine
302-
def get_resolved_ref(self):
295+
async def get_resolved_ref(self):
303296
client = AsyncHTTPClient()
304297
req = HTTPRequest("https://doi.org/{}".format(self.spec),
305298
user_agent="BinderHub")
306-
r = yield client.fetch(req)
299+
r = await client.fetch(req)
307300

308301
search_url = urllib.parse.urlunparse(
309302
urllib.parse.urlparse(r.effective_url)._replace(
310303
path="/api/datasets/:persistentId"
311304
)
312305
)
313306
req = HTTPRequest(search_url, user_agent="BinderHub")
314-
r = yield client.fetch(req)
307+
r = await client.fetch(req)
315308
resp = json.loads(r.body)
316309

317310
assert resp["status"] == "OK"
@@ -365,13 +358,12 @@ def _parse_resource_id(self, spec):
365358
resource_id = match.groups()[0]
366359
return resource_id
367360

368-
@gen.coroutine
369-
def get_resolved_ref(self):
361+
async def get_resolved_ref(self):
370362
client = AsyncHTTPClient()
371363
self.resource_id = self._parse_resource_id(self.spec)
372364
req = HTTPRequest("https://www.hydroshare.org/hsapi/resource/{}/scimeta/elements".format(self.resource_id),
373365
user_agent="BinderHub")
374-
r = yield client.fetch(req)
366+
r = await client.fetch(req)
375367

376368
def parse_date(json_body):
377369
json_response = json.loads(json_body)
@@ -430,8 +422,7 @@ def __init__(self, *args, **kwargs):
430422
if not self.unresolved_ref:
431423
raise ValueError("`unresolved_ref` must be specified as a query parameter for the basic git provider")
432424

433-
@gen.coroutine
434-
def get_resolved_ref(self):
425+
async def get_resolved_ref(self):
435426
if hasattr(self, 'resolved_ref'):
436427
return self.resolved_ref
437428

@@ -542,8 +533,7 @@ def __init__(self, *args, **kwargs):
542533
if not self.unresolved_ref:
543534
raise ValueError("An unresolved ref is required")
544535

545-
@gen.coroutine
546-
def get_resolved_ref(self):
536+
async def get_resolved_ref(self):
547537
if hasattr(self, 'resolved_ref'):
548538
return self.resolved_ref
549539

@@ -561,7 +551,7 @@ def get_resolved_ref(self):
561551
api_url = url_concat(api_url, self.auth)
562552

563553
try:
564-
resp = yield client.fetch(api_url, user_agent="BinderHub")
554+
resp = await client.fetch(api_url, user_agent="BinderHub")
565555
except HTTPError as e:
566556
if e.code == 404:
567557
return None
@@ -680,8 +670,7 @@ async def get_resolved_ref_url(self):
680670
self.resolved_ref = await self.get_resolved_ref()
681671
return f"https://{self.hostname}/{self.user}/{self.repo}/tree/{self.resolved_ref}"
682672

683-
@gen.coroutine
684-
def github_api_request(self, api_url, etag=None):
673+
async def github_api_request(self, api_url, etag=None):
685674
client = AsyncHTTPClient()
686675

687676
request_kwargs = {}
@@ -702,7 +691,7 @@ def github_api_request(self, api_url, etag=None):
702691
)
703692

704693
try:
705-
resp = yield client.fetch(req)
694+
resp = await client.fetch(req)
706695
except HTTPError as e:
707696
if e.code == 304:
708697
resp = e.response
@@ -760,8 +749,7 @@ def github_api_request(self, api_url, etag=None):
760749

761750
return resp
762751

763-
@gen.coroutine
764-
def get_resolved_ref(self):
752+
async def get_resolved_ref(self):
765753
if hasattr(self, 'resolved_ref'):
766754
return self.resolved_ref
767755

@@ -781,7 +769,7 @@ def get_resolved_ref(self):
781769
return None
782770
etag = None
783771

784-
resp = yield self.github_api_request(api_url, etag=etag)
772+
resp = await self.github_api_request(api_url, etag=etag)
785773
if resp is None:
786774
self.log.debug("Caching 404 on %s", api_url)
787775
self.cache_404.set(api_url, True)
@@ -862,15 +850,14 @@ async def get_resolved_ref_url(self):
862850
self.resolved_ref = await self.get_resolved_ref()
863851
return f'https://{self.hostname}/{self.user}/{self.gist_id}/{self.resolved_ref}'
864852

865-
@gen.coroutine
866-
def get_resolved_ref(self):
853+
async def get_resolved_ref(self):
867854
if hasattr(self, 'resolved_ref'):
868855
return self.resolved_ref
869856

870857
api_url = f"https://api.github.com/gists/{self.gist_id}"
871858
self.log.debug("Fetching %s", api_url)
872859

873-
resp = yield self.github_api_request(api_url)
860+
resp = await self.github_api_request(api_url)
874861
if resp is None:
875862
return None
876863

0 commit comments

Comments
 (0)