From 64fba7fec6be6fed14dbaf243a2ed5e45c9db3d1 Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 20 Apr 2021 17:12:38 +0200 Subject: [PATCH] style --- .idea/tuxbot_bot.iml | 1 + .idea/webResources.xml | 14 +++++++ Makefile | 2 +- tuxbot/cogs/Admin/admin.py | 2 +- tuxbot/cogs/Crypto/crypto.py | 2 +- tuxbot/cogs/Crypto/functions/extractor.py | 4 +- tuxbot/cogs/Crypto/functions/parser.py | 3 +- tuxbot/cogs/Custom/custom.py | 14 +++---- tuxbot/cogs/Dev/dev.py | 4 +- tuxbot/cogs/Logs/logs.py | 13 ++++--- tuxbot/cogs/Network/functions/utils.py | 2 +- tuxbot/cogs/Network/network.py | 6 +-- tuxbot/cogs/Polls/functions/listeners.py | 27 ++++++------- tuxbot/cogs/Polls/polls.py | 32 +++++++-------- tuxbot/cogs/Utils/utils.py | 2 +- tuxbot/core/bot.py | 2 +- tuxbot/core/i18n.py | 6 ++- tuxbot/logging.py | 47 ++++++++++++----------- tuxbot/setup.py | 2 +- 19 files changed, 101 insertions(+), 84 deletions(-) create mode 100644 .idea/webResources.xml diff --git a/.idea/tuxbot_bot.iml b/.idea/tuxbot_bot.iml index 84404e0..e7c88fd 100644 --- a/.idea/tuxbot_bot.iml +++ b/.idea/tuxbot_bot.iml @@ -5,6 +5,7 @@ + diff --git a/.idea/webResources.xml b/.idea/webResources.xml new file mode 100644 index 0000000..717d9d6 --- /dev/null +++ b/.idea/webResources.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Makefile b/Makefile index 3b5b866..baa1f07 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ update-all: $(VIRTUAL_ENV)/bin/pip install --upgrade --force-reinstall . .PHONY: dev -dev: black update +dev: black type update tuxbot # Docker diff --git a/tuxbot/cogs/Admin/admin.py b/tuxbot/cogs/Admin/admin.py index feb4285..b771043 100644 --- a/tuxbot/cogs/Admin/admin.py +++ b/tuxbot/cogs/Admin/admin.py @@ -23,7 +23,7 @@ log = logging.getLogger("tuxbot.cogs.Admin") _ = Translator("Admin", __file__) -class Admin(commands.Cog, name="Admin"): +class Admin(commands.Cog): def __init__(self, bot: Tux): self.bot = bot diff --git a/tuxbot/cogs/Crypto/crypto.py b/tuxbot/cogs/Crypto/crypto.py index 23b4ce2..1df6fa9 100644 --- a/tuxbot/cogs/Crypto/crypto.py +++ b/tuxbot/cogs/Crypto/crypto.py @@ -16,7 +16,7 @@ log = logging.getLogger("tuxbot.cogs.Crypto") _ = Translator("Crypto", __file__) -class Crypto(commands.Cog, name="Crypto"): +class Crypto(commands.Cog): def __init__(self, bot: Tux): self.bot = bot diff --git a/tuxbot/cogs/Crypto/functions/extractor.py b/tuxbot/cogs/Crypto/functions/extractor.py index 3503f14..58c978a 100644 --- a/tuxbot/cogs/Crypto/functions/extractor.py +++ b/tuxbot/cogs/Crypto/functions/extractor.py @@ -5,8 +5,8 @@ from tuxbot.cogs.Crypto.functions.parser import data_parser async def extract( - attachments: list[Optional[Attachment]], data: str, max_size: int -) -> Union[dict, NoReturn]: + attachments: list[Optional[Attachment]], data: Optional[str], max_size: int +) -> dict: if not data and len(attachments) == 0: raise ValueError diff --git a/tuxbot/cogs/Crypto/functions/parser.py b/tuxbot/cogs/Crypto/functions/parser.py index 8c00b2d..e0d89b1 100644 --- a/tuxbot/cogs/Crypto/functions/parser.py +++ b/tuxbot/cogs/Crypto/functions/parser.py @@ -1,7 +1,8 @@ import re +from typing import Optional -def data_parser(data: str) -> dict: +def data_parser(data: Optional[str]) -> dict: output = { "message": "", "compressed": False, diff --git a/tuxbot/cogs/Custom/custom.py b/tuxbot/cogs/Custom/custom.py index fef9037..d316cf8 100644 --- a/tuxbot/cogs/Custom/custom.py +++ b/tuxbot/cogs/Custom/custom.py @@ -22,7 +22,7 @@ log = logging.getLogger("tuxbot.cogs.Custom") _ = Translator("Custom", __file__) -class Custom(commands.Cog, name="Custom"): +class Custom(commands.Cog): def __init__(self, bot: Tux): self.bot = bot @@ -76,10 +76,10 @@ class Custom(commands.Cog, name="Custom"): @_custom.command(name="alias", aliases=["aliases"]) async def _custom_alias(self, ctx: ContextPlus, *, alias: AliasConvertor): - args = str(alias).split(" | ") + args: list[str] = str(alias).split(" | ") command = args[0] - alias = args[1] + custom = args[1] user_aliases = await self._get_aliases(ctx) @@ -87,17 +87,17 @@ class Custom(commands.Cog, name="Custom"): set_if_none(self.bot.config.Users, ctx.author.id, Config.User) user_aliases = await self._get_aliases(ctx) - if alias in user_aliases.keys(): + if custom in user_aliases.keys(): return await ctx.send( _( "The alias `{alias}` is already defined " "for the command `{command}`", ctx, self.bot.config, - ).format(alias=alias, command=user_aliases.get(alias)) + ).format(alias=custom, command=user_aliases.get(custom)) ) - user_aliases[alias] = command + user_aliases[custom] = command await self._save_alias(ctx, user_aliases) @@ -107,5 +107,5 @@ class Custom(commands.Cog, name="Custom"): "was successfully created", ctx, self.bot.config, - ).format(alias=alias, command=command) + ).format(alias=custom, command=command) ) diff --git a/tuxbot/cogs/Dev/dev.py b/tuxbot/cogs/Dev/dev.py index e955fad..ba698d1 100644 --- a/tuxbot/cogs/Dev/dev.py +++ b/tuxbot/cogs/Dev/dev.py @@ -12,7 +12,7 @@ log = logging.getLogger("tuxbot.cogs.Dev") _ = Translator("Dev", __file__) -class Dev(commands.Cog, name="Dev"): +class Dev(commands.Cog): def __init__(self, bot: Tux): self.bot = bot @@ -26,6 +26,6 @@ class Dev(commands.Cog, name="Dev"): await ctx.send(str(5 / 0)) elif crash_type == "TypeError": # noinspection PyTypeChecker - await ctx.send(str(int([]))) + await ctx.send(str(int([]))) # type: ignore elif crash_type == "IndexError": await ctx.send(str([0][5])) diff --git a/tuxbot/cogs/Logs/logs.py b/tuxbot/cogs/Logs/logs.py index 0f7c86f..3dc2242 100644 --- a/tuxbot/cogs/Logs/logs.py +++ b/tuxbot/cogs/Logs/logs.py @@ -6,6 +6,7 @@ import textwrap import traceback from collections import defaultdict from logging import LogRecord +from typing import Any, Dict import discord import humanize @@ -47,13 +48,13 @@ class GatewayHandler(logging.Handler): self.cog.add_record(record) -class Logs(commands.Cog, name="Logs"): +class Logs(commands.Cog): def __init__(self, bot: Tux): self.bot = bot self.process = psutil.Process() self._batch_lock = asyncio.Lock() - self._data_batch = [] - self._gateway_queue = asyncio.Queue() + self._data_batch: list[Dict[str, Any]] = [] + self._gateway_queue: asyncio.Queue = asyncio.Queue() self.gateway_worker.start() # pylint: disable=no-member self.__config: LogsConfig = ConfigFile( @@ -61,8 +62,8 @@ class Logs(commands.Cog, name="Logs"): LogsConfig, ).config - self._resumes = [] - self._identifies = defaultdict(list) + self._resumes: list[datetime.datetime] = [] + self._identifies: defaultdict[Any, list] = defaultdict(list) self.old_on_error = bot.on_error bot.on_error = self.on_error @@ -215,7 +216,7 @@ class Logs(commands.Cog, name="Logs"): e = discord.Embed(colour=0x0A97F5, title="New DM") # blue colour e.set_author( name=message.author, - icon_url=message.author.avatar_url_as(format="png"), + icon_url=message.author.avatar.url, ) e.description = message.content if len(message.attachments) > 0: diff --git a/tuxbot/cogs/Network/functions/utils.py b/tuxbot/cogs/Network/functions/utils.py index 44462e7..a3c75b4 100644 --- a/tuxbot/cogs/Network/functions/utils.py +++ b/tuxbot/cogs/Network/functions/utils.py @@ -134,7 +134,7 @@ async def get_pydig_result( def check_ip_version_or_raise(version: str) -> Union[bool, NoReturn]: - if version in ["4", "6", ""]: + if version in ["4", "6", "None"]: return True raise InvalidIp(_("Invalid ip version")) diff --git a/tuxbot/cogs/Network/network.py b/tuxbot/cogs/Network/network.py index 9f234c6..e9741ea 100644 --- a/tuxbot/cogs/Network/network.py +++ b/tuxbot/cogs/Network/network.py @@ -1,7 +1,7 @@ import asyncio import logging import time -from typing import Union +from typing import Union, Optional import aiohttp import discord @@ -48,7 +48,7 @@ log = logging.getLogger("tuxbot.cogs.Network") _ = Translator("Network", __file__) -class Network(commands.Cog, name="Network"): +class Network(commands.Cog): def __init__(self, bot: Tux): self.bot = bot self.__config: NetworkConfig = ConfigFile( @@ -78,7 +78,7 @@ class Network(commands.Cog, name="Network"): self, ctx: ContextPlus, ip: IPConverter, - version: IPVersionConverter = "", + version: Optional[IPVersionConverter] = None, ): check_ip_version_or_raise(str(version)) diff --git a/tuxbot/cogs/Polls/functions/listeners.py b/tuxbot/cogs/Polls/functions/listeners.py index 3074232..e56d15a 100644 --- a/tuxbot/cogs/Polls/functions/listeners.py +++ b/tuxbot/cogs/Polls/functions/listeners.py @@ -2,7 +2,6 @@ import json from os.path import dirname import discord -from tuxbot.cogs import Polls from tuxbot.core.i18n import Translator from tuxbot.core.utils.functions.utils import upper_first @@ -14,7 +13,7 @@ _ = Translator("Polls", dirname(__file__)) async def _poll_reaction_add( - self: Polls, pld: discord.RawReactionActionEvent, poll: Poll + self, pld: discord.RawReactionActionEvent, poll: Poll ): if poll.is_anonymous: try: @@ -40,7 +39,7 @@ async def _poll_reaction_add( async def _suggest_reaction_add( - self: Polls, pld: discord.RawReactionActionEvent, suggest: Suggest + self, pld: discord.RawReactionActionEvent, suggest: Suggest ): poll = await suggest.poll @@ -48,10 +47,10 @@ async def _suggest_reaction_add( poll.author_id == pld.user_id or (await self.bot.is_owner(discord.Object(pld.user_id))) or ( - (channel := await self.bot.fetch_channel(pld.channel_id)) + (_channel := await self.bot.fetch_channel(pld.channel_id)) # pylint: disable=used-before-assignment .permissions_for( - await channel.guild.fetch_member(pld.user_id) + await _channel.guild.fetch_member(pld.user_id) ).administrator ) ): @@ -77,19 +76,19 @@ async def _suggest_reaction_add( await poll.save() - channel: discord.TextChannel = await self.bot.fetch_channel( + poll_channel: discord.TextChannel = await self.bot.fetch_channel( poll.channel_id ) - message = await channel.fetch_message(poll.message_id) + message = await poll_channel.fetch_message(poll.message_id) await message.add_reaction(emote) await self.update_poll(poll) - channel: discord.TextChannel = await self.bot.fetch_channel( + suggest_channel: discord.TextChannel = await self.bot.fetch_channel( suggest.channel_id ) - message = await channel.fetch_message(suggest.message_id) + message = await suggest_channel.fetch_message(suggest.message_id) await message.delete() @@ -102,20 +101,18 @@ async def _suggest_reaction_add( pass -async def cog_command_error(self: Polls, ctx, error): +async def cog_command_error(self, ctx, error): if isinstance(error, (InvalidChannel, BadPoll, TooLongProposition)): await ctx.send(_(str(error), ctx, self.bot.config)) -async def on_raw_reaction_add( - self: Polls, pld: discord.RawReactionActionEvent -): +async def on_raw_reaction_add(self, pld: discord.RawReactionActionEvent): poll = await self.get_poll(pld) - if poll: + if isinstance(poll, Poll): await _poll_reaction_add(self, pld, poll) - elif suggest := await self.get_suggest(pld): + elif isinstance(suggest := await self.get_suggest(pld), Suggest): await _suggest_reaction_add(self, pld, suggest) diff --git a/tuxbot/cogs/Polls/polls.py b/tuxbot/cogs/Polls/polls.py index 89e29f0..29bb61d 100644 --- a/tuxbot/cogs/Polls/polls.py +++ b/tuxbot/cogs/Polls/polls.py @@ -1,6 +1,6 @@ import json import logging -from typing import Union +from typing import Union, Dict, Iterable, Any import discord from discord.ext import commands @@ -21,7 +21,7 @@ log = logging.getLogger("tuxbot.cogs.Polls") _ = Translator("Polls", __file__) -class Polls(commands.Cog, name="Polls"): +class Polls(commands.Cog): def __init__(self, bot: Tux): self.bot = bot @@ -63,9 +63,9 @@ class Polls(commands.Cog, name="Polls"): poll_row.channel_id = stmt.channel.id poll_row.message_id = stmt.id poll_row.author_id = ctx.author.id - poll_row.content = {} + poll_row.content = {} # type: ignore poll_row.is_anonymous = anonymous - poll_row.available_choices = len(answers) + poll_row.available_choices = len(answers) # type: ignore await poll_row.save() @@ -113,12 +113,12 @@ class Polls(commands.Cog, name="Polls"): } content = ( - json.loads(poll.content) + json.loads(poll.content) # type: ignore if isinstance(poll.content, str) else poll.content ) - responses = {} + responses: Dict[int, int] = {} async for response in poll.choices: if responses.get(response.choice): @@ -126,14 +126,14 @@ class Polls(commands.Cog, name="Polls"): else: responses[response.choice] = 1 - for i, field in enumerate(content.get("fields")): + for i, field in enumerate(content.get("fields")): # type: ignore responders = responses.get(i, 0) - chart_options["data"]["labels"].append( + chart_options["data"]["labels"].append( # type: ignore field["name"][6:].replace("__", "") ) - chart_options["data"]["datasets"][0]["data"].append(responders) + chart_options["data"]["datasets"][0]["data"].append(responders) # type: ignore if responders <= 1: field["value"] = f"**{responders}** vote" @@ -142,22 +142,22 @@ class Polls(commands.Cog, name="Polls"): e = discord.Embed(description=content.get("description")) e.set_author( - name=content.get("author").get("name"), - icon_url=content.get("author").get("icon_url"), + name=content.get("author").get("name"), # type: ignore + icon_url=content.get("author").get("icon_url"), # type: ignore ) chart_url = URL(chart_base_url + json.dumps(chart_options)) e.set_thumbnail(url=str(chart_url)) - for field in content.get("fields"): + for field in content.get("fields"): # type: ignore e.add_field( name=field.get("name"), value=field.get("value"), inline=True ) - e.set_footer(text=content.get("footer").get("text")) + e.set_footer(text=content.get("footer").get("text")) # type: ignore await message.edit(embed=e) - poll.content = json.dumps(content) + poll.content = json.dumps(content) # type: ignore await poll.save() @@ -190,7 +190,7 @@ class Polls(commands.Cog, name="Polls"): """Just to change type for PyCharm""" suggest_row.poll = poll - suggest_row.proposition = new + suggest_row.proposition = new # type: ignore await suggest_row.save() @@ -215,7 +215,7 @@ class Polls(commands.Cog, name="Polls"): text=_("Requested by {author}", ctx, self.bot.config).format( author=ctx.author.name ), - icon_url=ctx.author.avatar_url_as(format="png"), + icon_url=ctx.author.avatar.url, ) await stmt.edit(content="", embed=e) diff --git a/tuxbot/cogs/Utils/utils.py b/tuxbot/cogs/Utils/utils.py index a0c94af..e2fe6b7 100644 --- a/tuxbot/cogs/Utils/utils.py +++ b/tuxbot/cogs/Utils/utils.py @@ -20,7 +20,7 @@ log = logging.getLogger("tuxbot.cogs.Utils") _ = Translator("Utils", __file__) -class Utils(commands.Cog, name="Utils"): +class Utils(commands.Cog): def __init__(self, bot: Tux): self.bot = bot diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index dfec248..07b009f 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -205,7 +205,7 @@ class Tux(commands.AutoShardedBot): self.console.print() async def is_owner( - self, user: Union[discord.User, discord.Member] + self, user: Union[discord.User, discord.Member, discord.Object] ) -> bool: """Determines if the user is a bot owner. diff --git a/tuxbot/core/i18n.py b/tuxbot/core/i18n.py index 07b2c8f..1d376d6 100644 --- a/tuxbot/core/i18n.py +++ b/tuxbot/core/i18n.py @@ -46,14 +46,16 @@ def get_locale_name(locale: str) -> str: class Translator: """Class to load texts at init.""" - def __init__(self, name: str, file_location: Union[Path, os.PathLike]): + def __init__( + self, name: str, file_location: Union[Path, os.PathLike, str] + ): """Initializes the Translator object. Parameters ---------- name : str The cog name. - file_location:Path|os.PathLike + file_location:Path|os.PathLike|str File path for the required extension. """ diff --git a/tuxbot/logging.py b/tuxbot/logging.py index 1b56586..464f504 100644 --- a/tuxbot/logging.py +++ b/tuxbot/logging.py @@ -13,27 +13,6 @@ formatter = logging.Formatter( ) -def _setup_logging(level: int, location: pathlib.Path, name: str) -> None: - logger = logging.getLogger(name) - logger.setLevel(level) - logger_file = location / f"{name}.log" - - handler = logging.handlers.RotatingFileHandler( - str(logger_file.resolve()), - maxBytes=MAX_BYTES, - backupCount=MAX_OLD_LOGS, - ) - - base_handler = logging.handlers.RotatingFileHandler( - str(logger_file.resolve()), - maxBytes=MAX_BYTES, - backupCount=MAX_OLD_LOGS, - ) - - handler.setFormatter(formatter) - base_handler.setFormatter(formatter) - - def init_logging(level: int, location: pathlib.Path) -> None: """Initialize loggers. @@ -45,8 +24,30 @@ def init_logging(level: int, location: pathlib.Path) -> None: Where to store Logs. """ - _setup_logging(level, location, "discord") - _setup_logging(level, location, "tuxbot") + # dpy_logger = logging.getLogger("discord") + # dpy_logger.setLevel(logging.WARN) + # dpy_logger_file = location / "discord.log" + + base_logger = logging.getLogger("tuxbot") + base_logger.setLevel(level) + base_logger_file = location / "tuxbot.log" + + # dpy_handler = logging.handlers.RotatingFileHandler( + # str(dpy_logger_file.resolve()), + # maxBytes=MAX_BYTES, + # backupCount=MAX_OLD_LOGS, + # ) + base_handler = logging.handlers.RotatingFileHandler( + str(base_logger_file.resolve()), + maxBytes=MAX_BYTES, + backupCount=MAX_OLD_LOGS, + ) stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) + + # dpy_handler.setFormatter(formatter) + base_handler.setFormatter(formatter) + + # dpy_logger.addHandler(dpy_handler) + base_logger.addHandler(base_handler) diff --git a/tuxbot/setup.py b/tuxbot/setup.py index 6a6d986..7952d09 100644 --- a/tuxbot/setup.py +++ b/tuxbot/setup.py @@ -199,7 +199,7 @@ def additional_config(cogs: Union[str, list] = "**"): console.print(Rule(f"\nConfiguration for `{cog_name}` module")) mod = importlib.import_module(str(path).replace("/", ".")[:-3]) mod_config_type = getattr(mod, cog_name.capitalize() + "Config") - mod_extra = mod.extra + mod_extra = mod.extra # type: ignore mod_config = config.ConfigFile( str(cogs_data_path(cog_name) / "config.yaml"),