From c085feea71e22f4364ff8466af5c4a82fc27ce04 Mon Sep 17 00:00:00 2001 From: timofey Date: Mon, 2 Oct 2023 19:12:46 +0000 Subject: [PATCH] refactor --- docs/source/conf.py | 17 +++++++------- setup.py | 16 ++++++------- v6d3music/__init__.py | 2 +- v6d3music/api.py | 8 ++++--- v6d3music/core/default_effects.py | 5 ++++- v6d3music/core/mainservice.py | 6 ++--- v6d3music/core/monitoring.py | 31 ++++++++++---------------- v6d3music/core/queueaudio.py | 4 +++- v6d3music/core/set_config.py | 2 ++ v6d3music/core/ytaudio.py | 8 ++++--- v6d3music/run-bot.py | 2 +- v6d3music/utils/bytes_hash.py | 2 +- v6d3music/utils/fill.py | 4 ++-- v6d3music/utils/options_for_effects.py | 4 ++-- 14 files changed, 58 insertions(+), 53 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index d92cc47..3ce76be 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -8,26 +8,27 @@ import os.path import sys -project = 'parrrate-music' -copyright = '2022, PARRRATE TNV' -author = 'PARRRATE TNV' + +project = "parrrate-music" +copyright = "2022, PARRRATE TNV" +author = "PARRRATE TNV" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration extensions = [ - 'sphinx.ext.autodoc', + "sphinx.ext.autodoc", ] -templates_path = ['_templates'] +templates_path = ["_templates"] exclude_patterns = [] # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output -html_theme = 'pydata_sphinx_theme' -html_static_path = ['_static'] +html_theme = "pydata_sphinx_theme" +html_static_path = ["_static"] -sys.path.insert(0, os.path.abspath('../..')) +sys.path.insert(0, os.path.abspath("../..")) diff --git a/setup.py b/setup.py index 6a6eff3..8f3dfa4 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,12 @@ from setuptools import setup setup( - name='v6d3music', - version='', - packages=['v6d3music'], - url='', - license='', - author='PARRRATE TNV', - author_email='', - description='' + name="v6d3music", + version="", + packages=["v6d3music"], + url="", + license="", + author="PARRRATE TNV", + author_email="", + description="", ) diff --git a/v6d3music/__init__.py b/v6d3music/__init__.py index 48ba684..5008afd 100644 --- a/v6d3music/__init__.py +++ b/v6d3music/__init__.py @@ -1,3 +1,3 @@ -__all__ = ('api', 'app', 'commands', 'config') +__all__ = ("api", "app", "commands", "config") from . import api, app, commands, config diff --git a/v6d3music/api.py b/v6d3music/api.py index a53b934..1264efe 100644 --- a/v6d3music/api.py +++ b/v6d3music/api.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import asyncio import time @@ -118,7 +120,7 @@ class UserApi: sub._key = key return sub - async def to_guild_api(self, guild_id: int) -> "GuildApi": + async def to_guild_api(self, guild_id: int) -> GuildApi: guild = self.client.get_guild(guild_id) or await self.client.fetch_guild(guild_id) if guild is None: raise UserApi.UnknownMember("unknown guild") @@ -127,7 +129,7 @@ class UserApi: raise UserApi.UnknownMember("unknown member of a guild") return GuildApi(self, member) - async def to_operator_api(self) -> "OperatorApi": + async def to_operator_api(self) -> OperatorApi: if not self.pi.is_operator(self.user_id): raise UserApi.UnknownMember("not an operator") return OperatorApi(self) @@ -188,7 +190,7 @@ class GuildApi(UserApi): self.member = member self.guild = member.guild - async def to_voice_api(self) -> "VoiceApi": + async def to_voice_api(self) -> VoiceApi: voice = self.member.voice if voice is None: raise GuildApi.VoiceNotConnected("you are not connected to voice") diff --git a/v6d3music/core/default_effects.py b/v6d3music/core/default_effects.py index eb20feb..c0e5897 100644 --- a/v6d3music/core/default_effects.py +++ b/v6d3music/core/default_effects.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from contextlib import AsyncExitStack from ptvp35 import DbFactory, KVJson @@ -21,11 +23,12 @@ class DefaultEffects: raise Explicit("these effects are not allowed") await self.__db.set(gid, effects) - async def __aenter__(self) -> "DefaultEffects": + async def __aenter__(self) -> DefaultEffects: async with AsyncExitStack() as es: self.__db = await es.enter_async_context(DbFactory(myroot / "effects.db", kvfactory=KVJson())) self.__es = es.pop_all() return self + raise RuntimeError async def __aexit__(self, exc_type, exc_val, exc_tb): async with self.__es: diff --git a/v6d3music/core/mainservice.py b/v6d3music/core/mainservice.py index 975178a..a6bd8a8 100644 --- a/v6d3music/core/mainservice.py +++ b/v6d3music/core/mainservice.py @@ -99,10 +99,10 @@ class MainService: raise Explicit("not in a guild") return await self.raw_vc_for_member(ctx.member) - def mode(self, *, create: bool, force_play: bool) -> "MainMode": + def mode(self, *, create: bool, force_play: bool) -> MainMode: return MainMode(self, create=create, force_play=force_play) - def context(self, ctx: Context, *, create: bool, force_play: bool) -> "MainContext": + def context(self, ctx: Context, *, create: bool, force_play: bool) -> MainContext: return self.mode(create=create, force_play=force_play).context(ctx) async def create(self, guild: discord.Guild) -> MainAudio: @@ -266,7 +266,7 @@ class MainMode: vc.play(source) return source - def context(self, ctx: Context) -> "MainContext": + def context(self, ctx: Context) -> MainContext: return MainContext(self, ctx) diff --git a/v6d3music/core/monitoring.py b/v6d3music/core/monitoring.py index 7a755e3..3f82f1a 100644 --- a/v6d3music/core/monitoring.py +++ b/v6d3music/core/monitoring.py @@ -1,4 +1,6 @@ -__all__ = ('Monitoring', 'PersistentMonitoring') +from __future__ import annotations + +__all__ = ("Monitoring", "PersistentMonitoring") import asyncio from contextlib import AsyncExitStack, ExitStack @@ -6,7 +8,7 @@ from typing import Any, Callable, Generic, TypeVar from rainbowadn.instrument import Instrumentation -T = TypeVar('T', bound=Instrumentation, covariant=True) +T = TypeVar("T", bound=Instrumentation, covariant=True) class Provider(Generic[T]): @@ -19,7 +21,7 @@ class Provider(Generic[T]): def __enter__(self) -> T: if self.__closed: - raise RuntimeError('the provider is closed') + raise RuntimeError("the provider is closed") if self.__count < 0: raise RuntimeError if self.__count == 0: @@ -62,19 +64,15 @@ class ProviderManager(Generic[T]): class Monitoring: async def get(self, provider: Callable[[], T]) -> Provider[T]: if provider not in self.__providers: - self.__providers[provider] = asyncio.create_task( - self.__es.enter_async_context(ProviderManager(provider)) - ) + self.__providers[provider] = asyncio.create_task(self.__es.enter_async_context(ProviderManager(provider))) return await self.__providers[provider] - async def __aenter__(self) -> 'Monitoring': + async def __aenter__(self) -> "Monitoring": async with AsyncExitStack() as es: - self.__providers: dict[ - Callable[[], Instrumentation], - asyncio.Future[Provider] - ] = {} + self.__providers: dict[Callable[[], Instrumentation], asyncio.Future[Provider]] = {} self.__es = es.pop_all() return self + raise RuntimeError async def __aexit__(self, exc_type, exc_val, exc_tb): async with self.__es: @@ -90,16 +88,11 @@ class PersistentMonitoring: async def get(self, provider: Callable[[], T]) -> T: if provider not in self.__instrumentations: - self.__instrumentations[provider] = asyncio.create_task( - self._get(provider) - ) + self.__instrumentations[provider] = asyncio.create_task(self._get(provider)) return await self.__instrumentations[provider] - def __enter__(self) -> 'PersistentMonitoring': - self.__instrumentations: dict[ - Callable[[], Instrumentation], - asyncio.Future - ] = {} + def __enter__(self) -> PersistentMonitoring: + self.__instrumentations: dict[Callable[[], Instrumentation], asyncio.Future] = {} self.__es = ExitStack() return self diff --git a/v6d3music/core/queueaudio.py b/v6d3music/core/queueaudio.py index c0dc3cc..28e2220 100644 --- a/v6d3music/core/queueaudio.py +++ b/v6d3music/core/queueaudio.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import asyncio import random import traceback @@ -53,7 +55,7 @@ class QueueAudio(discord.AudioSource): return respawned @classmethod - async def create(cls, servicing: YTAServicing, db: DbConnection, guild: discord.Guild) -> "QueueAudio": + async def create(cls, servicing: YTAServicing, db: DbConnection, guild: discord.Guild) -> QueueAudio: return cls(db, guild, await cls.respawned(servicing, db, guild)) async def save(self, delay: bool) -> None: diff --git a/v6d3music/core/set_config.py b/v6d3music/core/set_config.py index d4db44a..a5a0e0b 100644 --- a/v6d3music/core/set_config.py +++ b/v6d3music/core/set_config.py @@ -17,8 +17,10 @@ async def repeat(repeated: Callable[[], Coroutine[Any, Any, T]]) -> T: async def set_config(key: str, value: Any) -> None: json = {"key": key, "value": value} + async def call() -> None: async with aiohttp.ClientSession() as s, s.post("http://sessionservice/config/", json=json) as response: if response.status != 200: raise RuntimeError("config request failed") + await repeat(call) diff --git a/v6d3music/core/ytaudio.py b/v6d3music/core/ytaudio.py index dfd4f87..6b843ba 100644 --- a/v6d3music/core/ytaudio.py +++ b/v6d3music/core/ytaudio.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import asyncio import random import re @@ -222,7 +224,7 @@ class YTAudio(discord.AudioSource): } @classmethod - async def respawn(cls, servicing: YTAServicing, guild: discord.Guild, respawn: dict) -> "YTAudio": + async def respawn(cls, servicing: YTAServicing, guild: discord.Guild, respawn: dict) -> YTAudio: member_id: int | None = respawn["rby"] if member_id is None: member = None @@ -267,7 +269,7 @@ class YTAudio(discord.AudioSource): "canbeskipped": self.can_be_skipped_by(member), } - def copy(self) -> "YTAudio": + def copy(self) -> YTAudio: return YTAudio( self.servicing, self.url, @@ -278,7 +280,7 @@ class YTAudio(discord.AudioSource): 0, ) - def branch(self) -> "YTAudio": + def branch(self) -> YTAudio: if self.stop_at is not None: raise Explicit("already branched") self.stop_at = stop_at = self.already_read + 50 diff --git a/v6d3music/run-bot.py b/v6d3music/run-bot.py index 838f98c..40e2b01 100644 --- a/v6d3music/run-bot.py +++ b/v6d3music/run-bot.py @@ -1,4 +1,4 @@ from .main import main -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/v6d3music/utils/bytes_hash.py b/v6d3music/utils/bytes_hash.py index c6bdf84..b644ce3 100644 --- a/v6d3music/utils/bytes_hash.py +++ b/v6d3music/utils/bytes_hash.py @@ -1,6 +1,6 @@ import nacl.hash -__all__ = ('bytes_hash',) +__all__ = ("bytes_hash",) def bytes_hash(b: bytes) -> str: diff --git a/v6d3music/utils/fill.py b/v6d3music/utils/fill.py index efb3348..42bf961 100644 --- a/v6d3music/utils/fill.py +++ b/v6d3music/utils/fill.py @@ -1,5 +1,5 @@ import discord -__all__ = ('FILL',) +__all__ = ("FILL",) -FILL = b'\x00' * discord.opus.Encoder.FRAME_SIZE +FILL = b"\x00" * discord.opus.Encoder.FRAME_SIZE diff --git a/v6d3music/utils/options_for_effects.py b/v6d3music/utils/options_for_effects.py index b1ba041..3b23d1c 100644 --- a/v6d3music/utils/options_for_effects.py +++ b/v6d3music/utils/options_for_effects.py @@ -1,8 +1,8 @@ import shlex from typing import Optional -__all__ = ('options_for_effects',) +__all__ = ("options_for_effects",) def options_for_effects(effects: str | None) -> Optional[str]: - return f'-af {shlex.quote(effects)}' if effects else None + return f"-af {shlex.quote(effects)}" if effects else None