Unfinished changes, i gotta push
parent
7a7bd6fed4
commit
32ab8538a4
|
@ -1,9 +1,9 @@
|
||||||
from .classes import Command, RegexCommand, Daemon, Default
|
from .classes import RegularCommand, RegexCommand, Daemon, Default
|
||||||
from .functions import command, regex_command, daemon, default
|
from .functions import command, regex_command, daemon, default
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"command",
|
"command",
|
||||||
"Command",
|
"RegularCommand",
|
||||||
"regex_command",
|
"regex_command",
|
||||||
"RegexCommand",
|
"RegexCommand",
|
||||||
"default",
|
"default",
|
||||||
|
|
|
@ -54,9 +54,9 @@ class Simple:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
class Command(Simple):
|
class SimpleCommand(Simple):
|
||||||
"""
|
"""
|
||||||
Represents a command of the module.
|
Base class for the various command types.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, func: callable, cmd: str, help_str: str = None):
|
def __init__(self, func: callable, cmd: str, help_str: str = None):
|
||||||
|
@ -72,18 +72,44 @@ class Command(Simple):
|
||||||
self.cmd = cmd
|
self.cmd = cmd
|
||||||
self.help_str = help_str
|
self.help_str = help_str
|
||||||
|
|
||||||
def match(self, prefix: str) -> bool:
|
def match(self, message: str) -> bool:
|
||||||
"""
|
"""
|
||||||
Returns wether the command matches the given prefix.
|
Returns wether the command matches the given message.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
prefix: string to match own prefix against
|
message: message to check
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return self.cmd == prefix
|
return self.cmd == message.split(" ")[0]
|
||||||
|
|
||||||
|
|
||||||
class RegexCommand(Command):
|
class RegularCommand(SimpleCommand):
|
||||||
|
"""
|
||||||
|
Defines a regular command; Handles command aliases as well.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
func: callable,
|
||||||
|
cmd: str,
|
||||||
|
help_str: str = None,
|
||||||
|
alias: Union[str, List[str]] = None,
|
||||||
|
requires_prefix: bool = False,
|
||||||
|
):
|
||||||
|
super().__init__(self, func, cmd, help_str)
|
||||||
|
|
||||||
|
self.alias = alias
|
||||||
|
self.requires_prefix = requires_prefix
|
||||||
|
|
||||||
|
def match(self, message: str) -> bool:
|
||||||
|
# This just makes it a bit easier to use in the function
|
||||||
|
module = self._obj
|
||||||
|
client = module._client
|
||||||
|
|
||||||
|
words = [word for word in message.split(" ") if word]
|
||||||
|
|
||||||
|
|
||||||
|
class RegexCommand(SimpleCommand):
|
||||||
"""
|
"""
|
||||||
A subclass of Command that can use a regex pattern instead of a fixed
|
A subclass of Command that can use a regex pattern instead of a fixed
|
||||||
prefix.
|
prefix.
|
||||||
|
@ -110,7 +136,7 @@ class Daemon(Simple):
|
||||||
"""
|
"""
|
||||||
Args
|
Args
|
||||||
func: function to wrap
|
func: function to wrap
|
||||||
interval: time between calls of the function; if < 0, the function
|
interval: time between calls of the function; if <= 0, the function
|
||||||
is assumed to contain its own infinite loop, allowing for
|
is assumed to contain its own infinite loop, allowing for
|
||||||
fine-grained control of the daemon, if desired
|
fine-grained control of the daemon, if desired
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -3,27 +3,35 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
# Own imports
|
# Own imports
|
||||||
from .classes import Command, RegexCommand, Daemon, Default
|
from .classes import RegularCommand, RegexCommand, Daemon, Default
|
||||||
|
|
||||||
# Typing imports
|
# Typing imports
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
# Built-in imports
|
# Built-in imports
|
||||||
from typing import Union
|
from typing import Union, List
|
||||||
|
|
||||||
|
|
||||||
def command(cmd, help_str: str = None) -> callable:
|
def command(
|
||||||
|
cmd,
|
||||||
|
help_str: str = None,
|
||||||
|
alias: Union[str, List[str]] = None,
|
||||||
|
requires_prefix: bool = False,
|
||||||
|
) -> callable:
|
||||||
"""
|
"""
|
||||||
Converts a method into a command by replacing it with a Command object.
|
Converts a method into a command by replacing it with a Command object.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
cmd: keyword used to call this function
|
cmd: keyword used to call this function
|
||||||
help_str: short description of the command
|
help_str: short description of the command
|
||||||
|
alias: alias(es) for the command
|
||||||
|
requires_prefix: defines wether the command needs the Frank prefix for
|
||||||
|
its aliases to work or not
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def inner(func):
|
def inner(func):
|
||||||
return Command(func, cmd, help_str)
|
return RegularCommand(func, cmd, help_str, aliases)
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ from __future__ import annotations
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
|
|
||||||
# Own imports
|
# Own imports
|
||||||
from .decorators import Command, Daemon, Default, RegexCommand
|
from .decorators import SimpleCommand, Daemon, Default, RegexCommand
|
||||||
|
|
||||||
# Typing imports
|
# Typing imports
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
@ -32,13 +32,13 @@ class ModuleMeta:
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def commands(self) -> List[Command]:
|
def commands(self) -> List[SimpleCommand]:
|
||||||
# This also matches RegexCommand objects
|
# This also matches RegexCommand objects
|
||||||
# The sort puts all the RegexCommand objects at the back, making them
|
# The sort puts all the RegexCommand objects at the back,making
|
||||||
# be matched last
|
# them be matched last
|
||||||
|
|
||||||
return sorted(
|
return sorted(
|
||||||
self._filter_attrs(lambda val: isinstance(val, Command)),
|
self._filter_attrs(lambda val: isinstance(val, SimpleCommand)),
|
||||||
key=lambda x: isinstance(x, RegexCommand),
|
key=lambda x: isinstance(x, RegexCommand),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
[tool.black]
|
||||||
|
line-length = 80
|
||||||
|
target-version = ['py38']
|
||||||
|
include = '\.pyi?$'
|
||||||
|
exclude = '''
|
||||||
|
|
||||||
|
(
|
||||||
|
/(
|
||||||
|
\.eggs
|
||||||
|
| \.git
|
||||||
|
| venv
|
||||||
|
| build
|
||||||
|
| dist
|
||||||
|
)/
|
||||||
|
)
|
||||||
|
'''
|
2
setup.py
2
setup.py
|
@ -2,6 +2,8 @@
|
||||||
# Third-party imports
|
# Third-party imports
|
||||||
import setuptools
|
import setuptools
|
||||||
|
|
||||||
|
# TODO: switch to alternative that uses pyproject.toml (e.g. poetry)
|
||||||
|
|
||||||
|
|
||||||
with open('requirements.txt', 'r') as reqs_file:
|
with open('requirements.txt', 'r') as reqs_file:
|
||||||
reqs = [line.strip() for line in reqs_file.readlines()]
|
reqs = [line.strip() for line in reqs_file.readlines()]
|
||||||
|
|
Reference in New Issue