From 948b52ea870ba3346704aa85ec45b940468cef78 Mon Sep 17 00:00:00 2001 From: linuxdaemon Date: Thu, 14 Mar 2024 00:36:41 +0000 Subject: [PATCH] Repo wide file-reformat --- .github/workflows/codeql.yml | 8 ++-- .pre-commit-config.yaml | 1 + cloudbot/plugin.py | 6 +-- cloudbot/util/database.py | 1 + cloudbot/util/filesize.py | 60 +++++++++++------------ cloudbot/util/formatting.py | 1 + cloudbot/util/mapping.py | 20 +++----- cloudbot/util/web.py | 2 +- docs/user/openweathermap.md | 1 - plugins/animal_gifs.py | 1 + plugins/core/chan_key_db.py | 1 + plugins/core/chan_track.py | 1 + plugins/core/optout.py | 1 + plugins/core/server_info.py | 1 + plugins/etymology.py | 1 + plugins/gaming.py | 3 +- plugins/herald.py | 3 +- plugins/imgur.py | 6 ++- plugins/notes.py | 1 + plugins/shorten.py | 2 +- plugins/stock.py | 3 +- plugins/tvdb.py | 6 +-- plugins/weather.py | 68 +++++++++++++-------------- plugins/wyr.py | 1 + tests/core_tests/test_plugin_hooks.py | 1 + tests/plugin_tests/test_weather.py | 9 ++-- tests/util/__init__.py | 2 +- 27 files changed, 109 insertions(+), 102 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 41667a22..70c718df 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -13,11 +13,11 @@ name: "CodeQL" on: push: - branches: [ "main" ] + branches: ["main"] pull_request: - branches: [ "main" ] + branches: ["main"] schedule: - - cron: '35 22 * * 5' + - cron: '35 22 * * 5' jobs: analyze: @@ -40,7 +40,7 @@ jobs: strategy: fail-fast: false matrix: - language: [ 'python' ] + language: ['python'] # CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ] # Use only 'java-kotlin' to analyze code written in Java, Kotlin or both # Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 38491742..1f7100ce 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,6 +2,7 @@ ci: skip: - pylint - mypy +exclude: ^\.vscode/.*$ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: c4a0b883114b00d8d76b479c820ce7950211c99b # frozen: v4.5.0 diff --git a/cloudbot/plugin.py b/cloudbot/plugin.py index 7db88c94..56a9f6fb 100644 --- a/cloudbot/plugin.py +++ b/cloudbot/plugin.py @@ -85,9 +85,9 @@ def __init__(self, bot): self.bot = bot self.plugins: Dict[str, Plugin] = {} - self._plugin_name_map: MutableMapping[ - str, Plugin - ] = WeakValueDictionary() + self._plugin_name_map: MutableMapping[str, Plugin] = ( + WeakValueDictionary() + ) self.commands: Dict[str, CommandHook] = {} self.raw_triggers: Dict[str, List[RawHook]] = defaultdict(list) self.catch_all_triggers: List[RawHook] = [] diff --git a/cloudbot/util/database.py b/cloudbot/util/database.py index df65645d..be5dfa77 100644 --- a/cloudbot/util/database.py +++ b/cloudbot/util/database.py @@ -1,6 +1,7 @@ """ database - contains variables set by cloudbot to be easily access """ + from sqlalchemy import MetaData from sqlalchemy.engine import Engine from sqlalchemy.ext.declarative import declarative_base diff --git a/cloudbot/util/filesize.py b/cloudbot/util/filesize.py index 76b8f2dd..b786ea7d 100644 --- a/cloudbot/util/filesize.py +++ b/cloudbot/util/filesize.py @@ -53,48 +53,48 @@ """ traditional = ( - (1024 ** 5, "P"), - (1024 ** 4, "T"), - (1024 ** 3, "G"), - (1024 ** 2, "M"), - (1024 ** 1, "K"), - (1024 ** 0, "B"), + (1024**5, "P"), + (1024**4, "T"), + (1024**3, "G"), + (1024**2, "M"), + (1024**1, "K"), + (1024**0, "B"), ) alternative = ( - (1024 ** 5, " PB"), - (1024 ** 4, " TB"), - (1024 ** 3, " GB"), - (1024 ** 2, " MB"), - (1024 ** 1, " KB"), - (1024 ** 0, (" byte", " bytes")), + (1024**5, " PB"), + (1024**4, " TB"), + (1024**3, " GB"), + (1024**2, " MB"), + (1024**1, " KB"), + (1024**0, (" byte", " bytes")), ) verbose = ( - (1024 ** 5, (" petabyte", " petabytes")), - (1024 ** 4, (" terabyte", " terabytes")), - (1024 ** 3, (" gigabyte", " gigabytes")), - (1024 ** 2, (" megabyte", " megabytes")), - (1024 ** 1, (" kilobyte", " kilobytes")), - (1024 ** 0, (" byte", " bytes")), + (1024**5, (" petabyte", " petabytes")), + (1024**4, (" terabyte", " terabytes")), + (1024**3, (" gigabyte", " gigabytes")), + (1024**2, (" megabyte", " megabytes")), + (1024**1, (" kilobyte", " kilobytes")), + (1024**0, (" byte", " bytes")), ) iec = ( - (1024 ** 5, "Pi"), - (1024 ** 4, "Ti"), - (1024 ** 3, "Gi"), - (1024 ** 2, "Mi"), - (1024 ** 1, "Ki"), - (1024 ** 0, ""), + (1024**5, "Pi"), + (1024**4, "Ti"), + (1024**3, "Gi"), + (1024**2, "Mi"), + (1024**1, "Ki"), + (1024**0, ""), ) si = ( - (1000 ** 5, "P"), - (1000 ** 4, "T"), - (1000 ** 3, "G"), - (1000 ** 2, "M"), - (1000 ** 1, "K"), - (1000 ** 0, "B"), + (1000**5, "P"), + (1000**4, "T"), + (1000**3, "G"), + (1000**2, "M"), + (1000**1, "K"), + (1000**0, "B"), ) # re.I style aliases diff --git a/cloudbot/util/formatting.py b/cloudbot/util/formatting.py index 6ba3acc4..c086ef33 100644 --- a/cloudbot/util/formatting.py +++ b/cloudbot/util/formatting.py @@ -44,6 +44,7 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ + import copy import re import warnings diff --git a/cloudbot/util/mapping.py b/cloudbot/util/mapping.py index bf2e579e..ee03dae0 100644 --- a/cloudbot/util/mapping.py +++ b/cloudbot/util/mapping.py @@ -19,32 +19,26 @@ from typing import Protocol class MapBase(Protocol[K_contra, V]): - def __getitem__(self, item: K_contra) -> V: - ... + def __getitem__(self, item: K_contra) -> V: ... - def __delitem__(self, item: K_contra) -> None: - ... + def __delitem__(self, item: K_contra) -> None: ... - def __setitem__(self, item: K_contra, value: V) -> None: - ... + def __setitem__(self, item: K_contra, value: V) -> None: ... - def get(self, item: K_contra, default: V = None) -> Optional[V]: - ... + def get(self, item: K_contra, default: V = None) -> Optional[V]: ... def setdefault( self, key: K_contra, default: Union[V, T] = None - ) -> Union[V, T]: - ... + ) -> Union[V, T]: ... def pop( self, key: K_contra, default: Union[V, T] = None - ) -> Union[V, T]: - ... + ) -> Union[V, T]: ... else: class MapBase(Generic[K_contra, V]): - ... + pass class KeyFoldMixin(MapBase[K_contra, V]): diff --git a/cloudbot/util/web.py b/cloudbot/util/web.py index 486bdb16..e4d29c1e 100644 --- a/cloudbot/util/web.py +++ b/cloudbot/util/web.py @@ -224,7 +224,7 @@ class Isgd(Shortener): def shorten(self, url, custom=None, key=None): p = {"url": url, "format": "json"} if custom: - p['shorturl'] = custom + p["shorturl"] = custom try: r = requests.get("https://is.gd/create.php", params=p) diff --git a/docs/user/openweathermap.md b/docs/user/openweathermap.md index d4d08f83..2b38afba 100644 --- a/docs/user/openweathermap.md +++ b/docs/user/openweathermap.md @@ -2,4 +2,3 @@ The weather.py plugin uses the OpenWeatherMap OneCall 3.0 API. Once you have an OpenWeatherMap API account, simply insert the API key in the config.json under api_keys -> openweathermap. - diff --git a/plugins/animal_gifs.py b/plugins/animal_gifs.py index 238a40cc..e7f2dd5f 100644 --- a/plugins/animal_gifs.py +++ b/plugins/animal_gifs.py @@ -1,6 +1,7 @@ """ All GIFs courtesy of http://bestanimations.com/ """ + import random from urllib.parse import urljoin diff --git a/plugins/core/chan_key_db.py b/plugins/core/chan_key_db.py index a0e6a96d..051e651e 100644 --- a/plugins/core/chan_key_db.py +++ b/plugins/core/chan_key_db.py @@ -4,6 +4,7 @@ Author: - linuxdaemon """ + from itertools import zip_longest from typing import Any, Dict, List, Optional diff --git a/plugins/core/chan_track.py b/plugins/core/chan_track.py index 5a53823a..385cdee3 100644 --- a/plugins/core/chan_track.py +++ b/plugins/core/chan_track.py @@ -4,6 +4,7 @@ Requires: server_info.py """ + import gc import json import logging diff --git a/plugins/core/optout.py b/plugins/core/optout.py index fb962fa3..7e3092d2 100644 --- a/plugins/core/optout.py +++ b/plugins/core/optout.py @@ -1,6 +1,7 @@ """ Bot wide hook opt-out for channels """ + from collections import defaultdict from functools import total_ordering from threading import RLock diff --git a/plugins/core/server_info.py b/plugins/core/server_info.py index 82c2aff4..6fbd9e1b 100644 --- a/plugins/core/server_info.py +++ b/plugins/core/server_info.py @@ -1,6 +1,7 @@ """ Tracks verious server info like ISUPPORT tokens """ + from typing import Callable, Dict, MutableMapping, TypeVar from cloudbot import hook diff --git a/plugins/etymology.py b/plugins/etymology.py index 90b82f41..55d13973 100644 --- a/plugins/etymology.py +++ b/plugins/etymology.py @@ -6,6 +6,7 @@ - Scaevolus - linuxdaemon """ + import re import requests diff --git a/plugins/gaming.py b/plugins/gaming.py index b4de68b6..45401d98 100644 --- a/plugins/gaming.py +++ b/plugins/gaming.py @@ -9,6 +9,7 @@ License: GPL v3 """ + import random import re @@ -50,7 +51,7 @@ def approximate(count, n): # Calculate a random sum approximated using a randomized normal variate with the midpoint used as the mu # and an approximated standard deviation based on variance as the sigma mid = 0.5 * (n + 1) * count - var = (n ** 2 - 1) / 12 + var = (n**2 - 1) / 12 adj_var = (var * count) ** 0.5 normalvariate = random.normalvariate(mid, adj_var) return normalvariate diff --git a/plugins/herald.py b/plugins/herald.py index e70e6fd5..ebd87368 100644 --- a/plugins/herald.py +++ b/plugins/herald.py @@ -63,7 +63,8 @@ def load_cache(db): @hook.command() def herald(text, nick, chan, db, reply): """{|show|delete|remove} - adds a greeting for your nick that will be announced everytime you join the - channel. Using .herald show will show your current herald and .herald delete will remove your greeting.""" + channel. Using .herald show will show your current herald and .herald delete will remove your greeting. + """ if text.lower() == "show": greeting = herald_cache[chan.casefold()].get(nick.casefold()) if greeting is None: diff --git a/plugins/imgur.py b/plugins/imgur.py index 617c9002..3e58f570 100644 --- a/plugins/imgur.py +++ b/plugins/imgur.py @@ -73,7 +73,8 @@ def get_items(text): @hook.command(autohelp=False) def imgur(text): """[search term] / [/r/subreddit] / [/user/username] / memes / random - returns a link to a random imgur image based - on your input. if no input is given the bot will get an image from the imgur frontpage""" + on your input. if no input is given the bot will get an image from the imgur frontpage + """ text = text.strip().lower() if not container.api: @@ -131,7 +132,8 @@ def imgur(text): @hook.command("imguralbum", "multiimgur", "imgalbum", "album", autohelp=False) def imguralbum(text, conn): """[search term] / [/r/subreddit] / [/user/username] / memes / random - returns a link to lots of random images - based on your input. if no input is given the bot will get images from the imgur frontpage""" + based on your input. if no input is given the bot will get images from the imgur frontpage + """ text = text.strip().lower() if not container.api: diff --git a/plugins/notes.py b/plugins/notes.py index fc5ee875..1d55ce22 100644 --- a/plugins/notes.py +++ b/plugins/notes.py @@ -1,6 +1,7 @@ """ Notes/todo list """ + from datetime import datetime from sqlalchemy import ( diff --git a/plugins/shorten.py b/plugins/shorten.py index 274c282b..ed516055 100644 --- a/plugins/shorten.py +++ b/plugins/shorten.py @@ -76,7 +76,7 @@ def gitio(text, reply): custom = args[1] if len(args) > 1 else None try: - if URL(url).host == 'git.io': + if URL(url).host == "git.io": return web.expand(url, "git.io") return web.shorten(url, custom=custom, service="git.io") diff --git a/plugins/stock.py b/plugins/stock.py index 3005b9c2..b8e5b725 100644 --- a/plugins/stock.py +++ b/plugins/stock.py @@ -4,6 +4,7 @@ Authors: - linuxdaemon """ + import math from decimal import Decimal @@ -93,7 +94,7 @@ def format_money(n): c = number_suffixes[idx] if c: exp = idx * 3 - n = n / (10 ** exp) + n = n / (10**exp) return "{:,.2f}{}".format(n, c) diff --git a/plugins/tvdb.py b/plugins/tvdb.py index c60953fd..bc162ace 100644 --- a/plugins/tvdb.py +++ b/plugins/tvdb.py @@ -346,12 +346,10 @@ def _gen_bounds(self, i: int) -> None: self._gen_to_index(i) @overload - def __getitem__(self, item: int) -> T: - ... + def __getitem__(self, item: int) -> T: ... @overload - def __getitem__(self, item: slice) -> List[T]: - ... + def __getitem__(self, item: slice) -> List[T]: ... def __getitem__(self, item: Union[int, slice]) -> Union[T, List[T]]: if isinstance(item, slice): diff --git a/plugins/weather.py b/plugins/weather.py index 22687f22..68ce4c2c 100644 --- a/plugins/weather.py +++ b/plugins/weather.py @@ -4,8 +4,8 @@ import googlemaps import pyowm -from pyowm import OWM from googlemaps.exceptions import ApiError +from pyowm import OWM from pyowm.weatherapi25.weather import Weather from sqlalchemy import Column, PrimaryKeyConstraint, String, Table @@ -210,9 +210,7 @@ def check_and_parse(event, db): owm_api = data.owm_api wm = owm_api.weather_manager() conditions = wm.one_call( - location_data['lat'], - location_data['lng'], - exclude="minutely,hourly" + location_data["lat"], location_data["lng"], exclude="minutely,hourly" ) return (location_data, conditions), None @@ -229,25 +227,25 @@ def weather(reply, db, triggered_prefix, event): daily_conditions: List[Weather] = owm.forecast_daily current: Weather = owm.current today = daily_conditions[0] - wind_mph = current.wind('miles_hour') - wind_speed = wind_mph['speed'] - today_temp = today.temperature('fahrenheit') - today_high = today_temp['max'] - today_low = today_temp['min'] - current_temperature = current.temperature('fahrenheit')['temp'] + wind_mph = current.wind("miles_hour") + wind_speed = wind_mph["speed"] + today_temp = today.temperature("fahrenheit") + today_high = today_temp["max"] + today_low = today_temp["min"] + current_temperature = current.temperature("fahrenheit")["temp"] current_data = { - 'name': "Current", - 'wind_direction': bearing_to_card(wind_mph['deg']), - 'wind_speed_mph': wind_speed, - 'wind_speed_kph': mph_to_kph(wind_speed), - 'summary': current.status, - 'temp_f': round_temp(current_temperature), - 'temp_c': round_temp(convert_f2c(current_temperature)), - 'temp_high_f': round_temp(today_high), - 'temp_high_c': round_temp(convert_f2c(today_high)), - 'temp_low_f': round_temp(today_low), - 'temp_low_c': round_temp(convert_f2c(today_low)), - 'humidity': current.humidity / 100, + "name": "Current", + "wind_direction": bearing_to_card(wind_mph["deg"]), + "wind_speed_mph": wind_speed, + "wind_speed_kph": mph_to_kph(wind_speed), + "summary": current.status, + "temp_f": round_temp(current_temperature), + "temp_c": round_temp(convert_f2c(current_temperature)), + "temp_high_f": round_temp(today_high), + "temp_high_c": round_temp(convert_f2c(today_high)), + "temp_low_f": round_temp(today_low), + "temp_low_c": round_temp(convert_f2c(today_low)), + "humidity": current.humidity / 100, } parts = [ @@ -295,34 +293,32 @@ def forecast(reply, db, event): today, tomorrow, *three_days = daily_conditions[:5] today_data = { - 'data': today, - } - tomorrow_data = { - 'data': tomorrow + "data": today, } - three_days_data = [{'data': d} for d in three_days] + tomorrow_data = {"data": tomorrow} + three_days_data = [{"data": d} for d in three_days] today_data["name"] = "Today" tomorrow_data["name"] = "Tomorrow" for day_fc in (today_data, tomorrow_data): - wind_speed = day_fc['data'].wind('miles_hour') + wind_speed = day_fc["data"].wind("miles_hour") day_fc.update( - wind_direction=bearing_to_card(wind_speed['deg']), - wind_speed_mph=wind_speed['speed'], - wind_speed_kph=mph_to_kph(wind_speed['speed']), - summary=day_fc['data'].status, + wind_direction=bearing_to_card(wind_speed["deg"]), + wind_speed_mph=wind_speed["speed"], + wind_speed_kph=mph_to_kph(wind_speed["speed"]), + summary=day_fc["data"].status, ) for fc_data in (today_data, tomorrow_data, *three_days_data): - temp = fc_data['data'].temperature('fahrenheit') - high = temp['max'] - low = temp['min'] + temp = fc_data["data"].temperature("fahrenheit") + high = temp["max"] + low = temp["min"] fc_data.update( temp_high_f=round_temp(high), temp_high_c=round_temp(convert_f2c(high)), temp_low_f=round_temp(low), temp_low_c=round_temp(convert_f2c(low)), - humidity=fc_data['data'].humidity / 100, + humidity=fc_data["data"].humidity / 100, ) parts = [ diff --git a/plugins/wyr.py b/plugins/wyr.py index 2a4d0c26..be73f347 100644 --- a/plugins/wyr.py +++ b/plugins/wyr.py @@ -13,6 +13,7 @@ License: BSD 3-Clause License """ + from cloudbot import hook diff --git a/tests/core_tests/test_plugin_hooks.py b/tests/core_tests/test_plugin_hooks.py index 9c7263d1..84c876ac 100644 --- a/tests/core_tests/test_plugin_hooks.py +++ b/tests/core_tests/test_plugin_hooks.py @@ -1,6 +1,7 @@ """ Validates all hook registrations in all plugins """ + import asyncio import importlib import inspect diff --git a/tests/plugin_tests/test_weather.py b/tests/plugin_tests/test_weather.py index 60a320f2..2c2db926 100644 --- a/tests/plugin_tests/test_weather.py +++ b/tests/plugin_tests/test_weather.py @@ -9,7 +9,7 @@ from cloudbot.event import CommandEvent from cloudbot.util.func_utils import call_with_args from plugins import weather -from tests.util import HookResult, wrap_hook_response, get_data_file +from tests.util import HookResult, get_data_file, wrap_hook_response @pytest.mark.parametrize( @@ -66,9 +66,12 @@ def test_temp_convert(temp_f, temp_c): def test_mph_to_kph(mph, kph): assert weather.mph_to_kph(mph) == kph + def get_test_data(): return { - 'json': json.loads(get_data_file('owm.json').read_text(encoding='utf-8')) + "json": json.loads( + get_data_file("owm.json").read_text(encoding="utf-8") + ) } @@ -248,7 +251,7 @@ def test_find_location( "Humidity: 45%; Wind: 15MPH/24KPH SE | " "\x02Tomorrow\x02: foobar; High: 64F/18C; " "Low: 57F/14C; Humidity: 45%; Wind: 15MPH/24KPH SE " - "-- 123 Test St, Example City, CA" + "-- 123 Test St, Example City, CA", ), {}, ) diff --git a/tests/util/__init__.py b/tests/util/__init__.py index 52fa0fb0..4a7ed4a0 100644 --- a/tests/util/__init__.py +++ b/tests/util/__init__.py @@ -79,7 +79,7 @@ def action(*args, **kwargs): # pragma: no cover def get_data_path() -> Path: - return Path(__file__).parent.parent / 'data' + return Path(__file__).parent.parent / "data" def get_data_file(name: str) -> Path: