From 45fcc00cdbce521de94f5383c40d280ae6200ae1 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 7 Jan 2024 19:27:19 +0000 Subject: [PATCH] fix(updatechecker): more pylint fixes --- updatechecker/updatechecker.py | 331 ++++++++++++++++----------------- 1 file changed, 162 insertions(+), 169 deletions(-) diff --git a/updatechecker/updatechecker.py b/updatechecker/updatechecker.py index 31027b6..1182383 100644 --- a/updatechecker/updatechecker.py +++ b/updatechecker/updatechecker.py @@ -75,171 +75,169 @@ class UpdateChecker(commands.Cog): @tasks.loop(minutes=1) async def bg_task(self): - while True: - cog = self.bot.get_cog("Downloader") - if cog is not None: - data = await self.conf.all() - repos = data["repos"] - auto = data["auto"] - channel = data["gochannel"] - use_embed = data["embed"] - whitelist = data["whitelist"] - blacklist = data["blacklist"] - if channel: - channel = self.bot.get_channel(channel) - if channel is None: + cog = self.bot.get_cog("Downloader") + if cog is not None: + data = await self.conf.all() + repos = data["repos"] + channel = data["gochannel"] + use_embed = data["embed"] + whitelist = data["whitelist"] + blacklist = data["blacklist"] + if channel: + channel = self.bot.get_channel(channel) + if channel is None: + await self.bot.send_to_owners( + "[Update Checker] It appears that I am no longer allowed to send messages to the designated update channel. " + "From now on, it will DM you." + ) + await self.conf.gochannel.set(0) + send = self.bot.send_to_owners + else: + use_embed = ( + use_embed and channel.permissions_for(channel.guild.me).embed_links + ) + send = channel.send + else: + send = self.bot.send_to_owners + + all_repos = cog._repo_manager.get_all_repo_names() # pylint: disable=protected-access + for repo in all_repos: + if not (repo in list(repos.keys())): + repos[repo] = "--default--" + await self.conf.repos.set(repos) + + saving_dict = {k: v for k, v in repos.items() if k in all_repos} + for repo_name, commit_saved in saving_dict.items(): + repo = cog._repo_manager.get_repo(repo_name) # pylint: disable=protected-access + if not repo: + continue + parsed_url = urlparse(repo.url) + if parsed_url.netloc == "github.com": + url = repo.url + r"/commits/" + repo.branch + ".atom" + response = await self.fetch_feed(url) + try: + commit = response.entries[0]["id"][33:] + chash = "[" + commit + "](" + response.entries[0]["link"] + ")" + cn = response.entries[0]["title"] + " - " + response.entries[0]["author"] + image = response.entries[0]["media_thumbnail"][0]["url"].split("?")[0] + except AttributeError: + continue + else: + url = repo.url + r"/rss/branch/" + repo.branch + response = await self.fetch_feed(url) + try: + commit = response.entries[0]["id"][33:] + chash = "[" + commit + "](" + response.entries[0]["link"] + ")" + cn = response.entries[0]["title"] + " - " + response.entries[0]["author"] + image = await self.fetch_gitea_thumbnail(parsed_url.scheme + "://" + parsed_url.netloc + "/api/v1/repos" + parsed_url.path) + except AttributeError: + continue + saving_dict[repo_name] = commit + if whitelist: + if repo_name not in whitelist: + continue + if repo_name in blacklist: + continue + # CN is used here for backwards compatability, don't want people to get an + # update for each and every one of their cogs when updating this cog + if commit_saved not in (commit, cn, '--default--'): + if use_embed: + e = discord.Embed( + title="Update Checker", + description=f"Update available for repo: {repo.name}", + timestamp=datetime.utcnow(), + color=0x00FF00, + ) + e.add_field(name="URL", value=repo.url) + e.add_field(name="Branch", value=repo.branch) + e.add_field(name="Commit", value=cn) + e.add_field(name="Hash", value=chash) + if image is not None: + e.set_thumbnail(url=image) + else: + e = box( + "[Update Checker]" + f" Repo: {repo.name}\n" + f" URL: {repo.url}\n" + f" Commit: {cn}\n" + f" Hash: {commit}\n" + f" Time: {datetime.utcnow()}", + 'css' + ) + try: + if use_embed: + await send(embed=e) + else: + await send(e) + except discord.Forbidden: + # send_to_owners suppresses Forbidden, logging it to console. + # As a result, this will only happen if a channel was set. await self.bot.send_to_owners( "[Update Checker] It appears that I am no longer allowed to send messages to the designated update channel. " "From now on, it will DM you." ) - await self.conf.gochannel.set(0) - send = self.bot.send_to_owners - else: - use_embed = ( - use_embed and channel.permissions_for(channel.guild.me).embed_links - ) - send = channel.send - else: - send = self.bot.send_to_owners - - all_repos = cog._repo_manager.get_all_repo_names() # pylint: disable=protected-access - for repo in all_repos: - if not (repo in list(repos.keys())): - repos[repo] = "--default--" - await self.conf.repos.set(repos) - - saving_dict = {k: v for k, v in repos.items() if k in all_repos} - for repo_name, commit_saved in saving_dict.items(): - repo = cog._repo_manager.get_repo(repo_name) # pylint: disable=protected-access - if not repo: - continue - parsed_url = urlparse(repo.url) - if parsed_url.netloc == "github.com": - url = repo.url + r"/commits/" + repo.branch + ".atom" - response = await self.fetch_feed(url) - try: - commit = response.entries[0]["id"][33:] - chash = "[" + commit + "](" + response.entries[0]["link"] + ")" - cn = response.entries[0]["title"] + " - " + response.entries[0]["author"] - image = response.entries[0]["media_thumbnail"][0]["url"].split("?")[0] - except AttributeError: - continue - else: - url = repo.url + r"/rss/branch/" + repo.branch - response = await self.fetch_feed(url) - try: - commit = response.entries[0]["id"][33:] - chash = "[" + commit + "](" + response.entries[0]["link"] + ")" - cn = response.entries[0]["title"] + " - " + response.entries[0]["author"] - image = await self.fetch_gitea_thumbnail(parsed_url.scheme + "://" + parsed_url.netloc + "/api/v1/repos" + parsed_url.path) - except AttributeError: - continue - saving_dict[repo_name] = commit - if whitelist: - if repo_name not in whitelist: - continue - if repo_name in blacklist: - continue - # CN is used here for backwards compatability, don't want people to get an - # update for each and every one of their cogs when updating this cog - if commit_saved not in (commit, cn, '--default--'): if use_embed: - e = discord.Embed( - title="Update Checker", - description=f"Update available for repo: {repo.name}", - timestamp=datetime.utcnow(), - color=0x00FF00, - ) - e.add_field(name="URL", value=repo.url) - e.add_field(name="Branch", value=repo.branch) - e.add_field(name="Commit", value=cn) - e.add_field(name="Hash", value=chash) - if image is not None: - e.set_thumbnail(url=image) + await self.bot.send_to_owners(embed=e) else: - e = box( - "[Update Checker]" - f" Repo: {repo.name}\n" - f" URL: {repo.url}\n" - f" Commit: {cn}\n" - f" Hash: {commit}\n" - f" Time: {datetime.utcnow()}", - 'css' - ) - try: - if use_embed: - await send(embed=e) - else: - await send(e) - except discord.Forbidden: - # send_to_owners suppresses Forbidden, logging it to console. - # As a result, this will only happen if a channel was set. - await self.bot.send_to_owners( - "[Update Checker] It appears that I am no longer allowed to send messages to the designated update channel. " - "From now on, it will DM you." - ) - if use_embed: - await self.bot.send_to_owners(embed=e) - else: - await self.bot.send_to_owners(e) - await self.conf.gochannel.set(0) - # Was already inaccessible before I got here, so I'm just gonna leave it and look at it later -- Sea - # try: - # await channel.send( - # f"[Update Checker] Update found for repo: {repo.name}. Updating repos..." - # ) - # except AttributeError: - # owner = (await self.bot.application_info()).owner - # await owner.send( - # "[Update Checker] It appears that the channel for this cog has been deleted. From now on, it will DM you." - # ) - # channel = owner - # await self.conf.gochannel.set(0) - # except discord.errors.Forbidden: - # owner = (await self.bot.application_info()).owner - # await owner.send( - # "[Update Checker] It appears that I am no longer allowed to send messages to the designated update channel. From now on, it will DM you." - # ) - # channel = owner - # await self.conf.gochannel.set(0) - # # Just a copy of `[p]cog update`, but without using ctx things - # try: - # installed_cogs = set(await cog.installed_cogs()) - # updated = await cog._repo_manager.update_all_repos() - # updated_cogs = set( - # cog for repo in updated for cog in repo.available_cogs - # ) - # installed_and_updated = updated_cogs & installed_cogs - # if installed_and_updated: - # await cog._reinstall_requirements(installed_and_updated) - # await cog._reinstall_cogs(installed_and_updated) - # await cog._reinstall_libraries(installed_and_updated) - # cognames = {c.name for c in installed_and_updated} - # message = humanize_list(tuple(map(inline, cognames))) - # except Exception as error: - # exception_log = ( - # "Exception while updating repos in Update Checker \n" - # ) - # exception_log += "".join( - # traceback.format_exception( - # type(error), error, error.__traceback__ - # ) - # ) - # try: - # await channel.send( - # f"[Update Checker]: Error while updating repos.\n\n{exception_log}" - # ) - # except discord.errors.Forbidden: - # pass - # else: - # try: - # await channel.send( - # f"[Update Checker]: Ran cog update. Updated cogs: {message}" - # ) - # except discord.errors.Forbidden: - # pass - await asyncio.sleep(1) - await self.conf.repos.set(saving_dict) + await self.bot.send_to_owners(e) + await self.conf.gochannel.set(0) + # Was already inaccessible before I got here, so I'm just gonna leave it and look at it later -- Sea + # try: + # await channel.send( + # f"[Update Checker] Update found for repo: {repo.name}. Updating repos..." + # ) + # except AttributeError: + # owner = (await self.bot.application_info()).owner + # await owner.send( + # "[Update Checker] It appears that the channel for this cog has been deleted. From now on, it will DM you." + # ) + # channel = owner + # await self.conf.gochannel.set(0) + # except discord.errors.Forbidden: + # owner = (await self.bot.application_info()).owner + # await owner.send( + # "[Update Checker] It appears that I am no longer allowed to send messages to the designated update channel. From now on, it will DM you." + # ) + # channel = owner + # await self.conf.gochannel.set(0) + # # Just a copy of `[p]cog update`, but without using ctx things + # try: + # installed_cogs = set(await cog.installed_cogs()) + # updated = await cog._repo_manager.update_all_repos() + # updated_cogs = set( + # cog for repo in updated for cog in repo.available_cogs + # ) + # installed_and_updated = updated_cogs & installed_cogs + # if installed_and_updated: + # await cog._reinstall_requirements(installed_and_updated) + # await cog._reinstall_cogs(installed_and_updated) + # await cog._reinstall_libraries(installed_and_updated) + # cognames = {c.name for c in installed_and_updated} + # message = humanize_list(tuple(map(inline, cognames))) + # except Exception as error: + # exception_log = ( + # "Exception while updating repos in Update Checker \n" + # ) + # exception_log += "".join( + # traceback.format_exception( + # type(error), error, error.__traceback__ + # ) + # ) + # try: + # await channel.send( + # f"[Update Checker]: Error while updating repos.\n\n{exception_log}" + # ) + # except discord.errors.Forbidden: + # pass + # else: + # try: + # await channel.send( + # f"[Update Checker]: Ran cog update. Updated cogs: {message}" + # ) + # except discord.errors.Forbidden: + # pass + await asyncio.sleep(1) + await self.conf.repos.set(saving_dict) async def fetch_feed(self, url: str): # Thank's to Sinbad's rss cog after which I copied this @@ -315,9 +313,8 @@ class UpdateChecker(commands.Cog): if channel == 0: channel = "Direct Messages" else: - try: - channel = self.bot.get_channel(channel).name - except: + channel = self.bot.get_channel(channel).name + if channel is None: channel = "Unknown" e.add_field(name="Update Channel", value=channel) await ctx.send(embed=e) @@ -325,9 +322,8 @@ class UpdateChecker(commands.Cog): if channel == 0: channel = "Direct Messages" else: - try: - channel = self.bot.get_channel(channel).name - except: + channel = self.bot.get_channel(channel).name + if channel is None: channel = "Unknown" message = ( "```css\n" @@ -363,7 +359,6 @@ class UpdateChecker(commands.Cog): @whiteblacklist.group() async def whitelist(self, ctx): """Whitelist certain repos from which to receive updates.""" - pass @whitelist.command(name="add") async def whitelistadd(self, ctx, *repos: Repo): @@ -396,7 +391,6 @@ class UpdateChecker(commands.Cog): @whiteblacklist.group() async def blacklist(self, ctx): """Blacklist certain repos from which to receive updates.""" - pass @blacklist.command(name="add") async def blacklistadd(self, ctx, *repos: Repo): @@ -430,7 +424,6 @@ class UpdateChecker(commands.Cog): @update.group(name="task") async def _group_update_task(self, ctx): """View the status of the task (the one checking for updates).""" - pass @_group_update_task.command() async def status(self, ctx): @@ -462,5 +455,5 @@ class UpdateChecker(commands.Cog): message = "No error has been encountered." else: ex = traceback.format_exception(type(e), e, e.__traceback__) - message = "An error has been encountered: ```py\n" + "".join(ex) + "```" + message = "An error has been encountered:" + box("".join(ex), "py") await ctx.send(message)