-
Notifications
You must be signed in to change notification settings - Fork 77
GitHub: use_commit_name
option
#213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8b92932
15020df
a62866a
7cf3b06
45b5593
d422205
cdd31a0
03a83d8
4d5c102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
*.egg-info/ | ||
__pycache__/ | ||
/build/ | ||
/dist/ | ||
.cache/ | ||
.eggs/ | ||
*.pyc | ||
*.pyo | ||
.travis.pub | ||
.pytest_cache/ | ||
.tox/ | ||
keyfile.toml |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,14 @@ async def run(self) -> None: | |
'''Run the `tasks`. Subclasses should implement this method.''' | ||
raise NotImplementedError | ||
|
||
def _normalize(x: Any) -> Any: | ||
if isinstance(x, list): | ||
return tuple(sorted(_normalize(y) for y in x)) | ||
elif isinstance(x, dict): | ||
return tuple(sorted((_normalize(k), _normalize(v)) for k, v in x.items())) | ||
else: | ||
return x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not obvious what this function does. It needs a docstring or a better name. |
||
|
||
class AsyncCache: | ||
'''A cache for use with async functions.''' | ||
cache: Dict[Hashable, Any] | ||
|
@@ -156,28 +164,32 @@ def __init__(self) -> None: | |
self.lock = asyncio.Lock() | ||
|
||
async def _get_json( | ||
self, key: Tuple[str, str, Tuple[Tuple[str, str], ...]], | ||
self, key: Tuple[str, str, Tuple[Tuple[str, str], ...], object], extra: Any, | ||
) -> Any: | ||
_, url, headers = key | ||
res = await session.get(url, headers=dict(headers)) | ||
_, url, headers, json = key | ||
json = extra # denormalizing json would be a pain, so we sneak it through | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about passing json as a |
||
res = await (session.get(url=url, headers=dict(headers)) if json is None \ | ||
else session.post(url=url, headers=dict(headers), json=json)) | ||
return res.json() | ||
|
||
async def get_json( | ||
self, url: str, *, | ||
headers: Dict[str, str] = {}, | ||
json: Optional[object] = None, | ||
) -> Any: | ||
'''Get specified ``url`` and return the response content as JSON. | ||
|
||
The returned data will be cached for reuse. | ||
''' | ||
key = '_jsonurl', url, tuple(sorted(headers.items())) | ||
key = '_jsonurl', url, _normalize(headers), _normalize(json) | ||
return await self.get( | ||
key , self._get_json) # type: ignore | ||
key, self._get_json, extra=json) # type: ignore | ||
|
||
async def get( | ||
self, | ||
key: Hashable, | ||
func: Callable[[Hashable], Coroutine[Any, Any, Any]], | ||
func: Callable[[Hashable, Optional[Any]], Coroutine[Any, Any, Any]], | ||
extra: Optional[Any] = None, | ||
) -> Any: | ||
'''Run async ``func`` and cache its return value by ``key``. | ||
|
||
|
@@ -189,7 +201,7 @@ async def get( | |
async with self.lock: | ||
cached = self.cache.get(key) | ||
if cached is None: | ||
coro = func(key) | ||
coro = func(key, extra) | ||
fu = asyncio.create_task(coro) | ||
self.cache[key] = fu | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
use_commit_hash
? It's not obvious what a commit name is.