From d8fa388d5e26f5b5527c012665fcff84d392b61b Mon Sep 17 00:00:00 2001 From: Paul Przybyszewski Date: Fri, 19 Aug 2022 15:39:32 +0200 Subject: [PATCH] codebase: misc fixes and suppression of NotFound in EmbedPages --- lib/api/github.py | 44 +++++++++--------------------------- lib/structs/discord/pages.py | 26 ++++++++++----------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/lib/api/github.py b/lib/api/github.py index 5fbc0b38..740f85a0 100644 --- a/lib/api/github.py +++ b/lib/api/github.py @@ -88,33 +88,23 @@ async def getitem(self, resource: str, default: Any = None) -> Any: @github_cached @validate_github_name('user') async def get_user_repos(self, user: GitHubUser) -> Optional[list[dict]]: - return list(r for r in await self.getitem(f'/users/{user}/repos') if r['private'] is False) + return list(r for r in await self.getitem(f'/users/{user}/repos', []) if r['private'] is False) @github_cached @validate_github_name('org') async def get_org(self, org: GitHubOrganization) -> Optional[dict]: - try: - return await self.gh.getitem(f'/orgs/{org}') - except BadRequest: - return None + return await self.getitem(f'/orgs/{org}') @github_cached @validate_github_name('org', default=[]) async def get_org_repos(self, org: GitHubOrganization) -> list[dict]: - try: - res = list(r for r in await self.gh.getitem(f'/orgs/{org}/repos') if r['private'] is False) - return res - except BadRequest: - return [] + return list(r for r in await self.getitem(f'/orgs/{org}/repos', []) if r['private'] is False) @normalize_repository async def get_repo_files(self, repo: GitHubRepository) -> list[dict]: if repo.count('/') != 1: return [] - try: - return await self.gh.getitem(f'/repos/{repo}/contents') - except BadRequest: - return [] + return await self.getitem(f'/repos/{repo}/contents', []) @normalize_repository async def get_tree_file(self, repo: GitHubRepository, path: str) -> dict | list | None: @@ -122,26 +112,21 @@ async def get_tree_file(self, repo: GitHubRepository, path: str) -> dict | list return None if path[0] == '/': path = path[1:] - try: - return await self.gh.getitem(f'/repos/{repo}/contents/{path}') - except BadRequest: - return None + return await self.getitem(f'/repos/{repo}/contents/{path}') @github_cached @validate_github_name('user', default=[]) async def get_user_orgs(self, user: GitHubUser) -> list[dict]: - try: - return list(await self.gh.getitem(f'/users/{user}/orgs')) - except BadRequest: - return [] + return await self.getitem(f'/users/{user}/orgs', []) @github_cached @validate_github_name('org', default=[]) async def get_org_members(self, org: GitHubOrganization) -> list[dict]: - try: - return list(await self.gh.getitem(f'/orgs/{org}/public_members')) - except BadRequest: - return [] + return await self.getitem(f'/orgs/{org}/public_members', []) + + @github_cached + async def get_gist(self, gist_id: str) -> Optional[dict]: + return await self.getitem(f'/gists/{gist_id}') @github_cached @validate_github_name('user') @@ -153,13 +138,6 @@ async def get_user_gists(self, user: GitHubUser): return data['user'] - @github_cached - async def get_gist(self, gist_id: str) -> Optional[dict]: - try: - return dict(await self.gh.getitem(f'/gists/{gist_id}')) - except BadRequest: - return None - @normalize_repository async def get_latest_commit(self, repo: GitHubRepository) -> Optional[dict] | Literal[False]: split: list = repo.split('/') diff --git a/lib/structs/discord/pages.py b/lib/structs/discord/pages.py index ba38b13e..c2ec9e8f 100644 --- a/lib/structs/discord/pages.py +++ b/lib/structs/discord/pages.py @@ -117,16 +117,17 @@ async def start(self, ctx: 'GitBotContext') -> None: :param ctx: The context to start the paginator in. """ - self._ensure_perms(ctx.channel) - self.start_time = time() - self.context: GitBotContext = ctx - self._edit_embed_footer(self.pages[self.current_page]) - message: discord.Message = await ctx.send(embed=self.pages[self.current_page]) - for embed in self.pages[1:]: - self._edit_embed_footer(embed) - self._set_initial_message_attrs(message) - await self._add_controls() - await self.__loop() + with contextlib.suppress(discord.errors.NotFound): + self._ensure_perms(ctx.channel) + self.start_time = time() + self.context: GitBotContext = ctx + self._edit_embed_footer(self.pages[self.current_page]) + message: discord.Message = await ctx.send(embed=self.pages[self.current_page]) + for embed in self.pages[1:]: + self._edit_embed_footer(embed) + self._set_initial_message_attrs(message) + await self._add_controls() + await self.__loop() async def edit(self, state: GitBotCommandState | int) -> None: """ @@ -181,9 +182,8 @@ def _set_initial_message_attrs(self, message: discord.Message): async def _add_controls(self): if self.message: - with contextlib.suppress(discord.errors.NotFound): - for control in EmbedPagesControl: - await self.message.add_reaction(control.value) # noqa + for control in EmbedPagesControl: + await self.message.add_reaction(control.value) # noqa async def __loop(self): while not self.should_die: