-
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
Open
bb010g
wants to merge
9
commits into
lilydjwg:master
Choose a base branch
from
bb010g:github-use_commit_name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b92932
defensively fix reStructuredText syntax
bb010g 15020df
switch from setup.py to PEP 517 & setup.cfg
bb010g a62866a
tests: spell test_htmlparser correctly
bb010g 7cf3b06
introduce tox support
bb010g 45b5593
tests: pytest-asyncio `asyncio_mode` -> `strict`
bb010g d422205
github: partial `use_commit_name` support
bb010g cdd31a0
github: factor out GitHub API querying
bb010g 03a83d8
github: full `use_commit_name` support
bb010g 4d5c102
github: full GraphQL API support
bb010g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's not obvious what this function does. It needs a docstring or a better name.