at-of isolation
This commit is contained in:
parent
4a821aa168
commit
d6d3b873de
25
v6d2ctx/at_of.py
Normal file
25
v6d2ctx/at_of.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from typing import Callable, Generic, TypeVar
|
||||||
|
|
||||||
|
from .context import Implicit
|
||||||
|
|
||||||
|
K = TypeVar('K')
|
||||||
|
V = TypeVar('V')
|
||||||
|
|
||||||
|
|
||||||
|
class AtOf(Generic[K, V]):
|
||||||
|
def __call__(self) -> tuple[Callable[[K], Callable[[V], V]], Callable[[K], V]]:
|
||||||
|
bucket: dict[K, V] = {}
|
||||||
|
|
||||||
|
def at(key: K) -> Callable[[V], V]:
|
||||||
|
def wrap(value: V) -> V:
|
||||||
|
bucket[key] = value
|
||||||
|
return value
|
||||||
|
return wrap
|
||||||
|
|
||||||
|
def of(key: K) -> V:
|
||||||
|
try:
|
||||||
|
return bucket[key]
|
||||||
|
except IndexError:
|
||||||
|
raise Implicit
|
||||||
|
|
||||||
|
return at, of
|
@ -1,7 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import time
|
import time
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from typing import Union, Optional, Callable, Awaitable
|
from typing import Awaitable, Callable, Optional, Union
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
@ -47,16 +47,7 @@ def escape(s: str):
|
|||||||
return res.getvalue()
|
return res.getvalue()
|
||||||
|
|
||||||
|
|
||||||
buckets: dict[str, dict[str, Callable[[Context, list[str]], Awaitable[None]]]] = {}
|
command_type = Callable[[Context, list[str]], Awaitable[None]]
|
||||||
|
|
||||||
|
|
||||||
def at(bucket: str, name: str):
|
|
||||||
def wrap(f: Callable[[Context, list[str]], Awaitable[None]]):
|
|
||||||
buckets.setdefault(bucket, {})[name] = f
|
|
||||||
|
|
||||||
return f
|
|
||||||
|
|
||||||
return wrap
|
|
||||||
|
|
||||||
|
|
||||||
class Explicit(Exception):
|
class Explicit(Exception):
|
||||||
@ -68,13 +59,6 @@ class Implicit(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def of(bucket: str, name: str) -> Callable[[Context, list[str]], Awaitable[None]]:
|
|
||||||
try:
|
|
||||||
return buckets[bucket][name]
|
|
||||||
except KeyError:
|
|
||||||
raise Implicit
|
|
||||||
|
|
||||||
|
|
||||||
benchmarks: dict[str, dict[str, float]] = {}
|
benchmarks: dict[str, dict[str, float]] = {}
|
||||||
_t = time.perf_counter()
|
_t = time.perf_counter()
|
||||||
|
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
|
from typing import Callable
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
from v6d2ctx.context import Context, Implicit, Explicit
|
from v6d2ctx.context import Context, Explicit, Implicit, command_type
|
||||||
from v6d2ctx.handle_command import handle_command
|
from v6d2ctx.handle_command import handle_command
|
||||||
|
|
||||||
|
|
||||||
async def handle_args(message: discord.Message, args: list[str], client: discord.Client):
|
async def handle_args(of: Callable[[str], command_type], message: discord.Message, args: list[str], client: discord.Client):
|
||||||
match args:
|
match args:
|
||||||
case []:
|
case []:
|
||||||
return
|
return
|
||||||
case [command_name, *command_args]:
|
case [command_name, *command_args]:
|
||||||
ctx = Context(message, client)
|
ctx = Context(message, client)
|
||||||
try:
|
try:
|
||||||
await handle_command(ctx, command_name, command_args)
|
await handle_command(of, ctx, command_name, command_args)
|
||||||
except Implicit:
|
except Implicit:
|
||||||
pass
|
pass
|
||||||
except Explicit as e:
|
except Explicit as e:
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
from v6d2ctx.context import Context, of
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
|
from v6d2ctx.context import Context, command_type
|
||||||
|
|
||||||
|
|
||||||
async def handle_command(ctx: Context, name: str, args: list[str]) -> None:
|
async def handle_command(of: Callable[[str], command_type], ctx: Context, name: str, args: list[str]) -> None:
|
||||||
await of('commands', name)(ctx, args)
|
await of(name)(ctx, args)
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
import shlex
|
import shlex
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
from v6d2ctx.handle_args import handle_args
|
from v6d2ctx.handle_args import handle_args
|
||||||
|
|
||||||
|
from v6d2ctx.context import command_type
|
||||||
|
|
||||||
async def handle_content(message: discord.Message, content: str, prefix: str, client: discord.Client):
|
|
||||||
|
async def handle_content(of: Callable[[str], command_type], message: discord.Message, content: str, prefix: str, client: discord.Client):
|
||||||
if message.author.bot:
|
if message.author.bot:
|
||||||
return
|
return
|
||||||
if not content.startswith(prefix):
|
if not content.startswith(prefix):
|
||||||
return
|
return
|
||||||
content = content.removeprefix(prefix)
|
content = content.removeprefix(prefix)
|
||||||
args = shlex.split(content)
|
args = shlex.split(content)
|
||||||
await handle_args(message, args, client)
|
await handle_args(of, message, args, client)
|
||||||
|
Loading…
Reference in New Issue
Block a user