Unfinished changes, i gotta push

aliases
Jef Roosens 2020-09-04 11:12:22 +02:00
parent 7a7bd6fed4
commit 32ab8538a4
7 changed files with 73 additions and 19 deletions

2
.flake8 100644
View File

@ -0,0 +1,2 @@
[flake8]
inline-quotes = double

View File

@ -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
__all__ = [
"command",
"Command",
"RegularCommand",
"regex_command",
"RegexCommand",
"default",

View File

@ -54,9 +54,9 @@ class Simple:
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):
@ -72,18 +72,44 @@ class Command(Simple):
self.cmd = cmd
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:
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
prefix.
@ -110,7 +136,7 @@ class Daemon(Simple):
"""
Args
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
fine-grained control of the daemon, if desired
"""

View File

@ -3,27 +3,35 @@
from __future__ import annotations
# Own imports
from .classes import Command, RegexCommand, Daemon, Default
from .classes import RegularCommand, RegexCommand, Daemon, Default
# Typing imports
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# 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.
Args:
cmd: keyword used to call this function
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):
return Command(func, cmd, help_str)
return RegularCommand(func, cmd, help_str, aliases)
return inner

View File

@ -6,7 +6,7 @@ from __future__ import annotations
from functools import cached_property
# Own imports
from .decorators import Command, Daemon, Default, RegexCommand
from .decorators import SimpleCommand, Daemon, Default, RegexCommand
# Typing imports
from typing import TYPE_CHECKING
@ -32,13 +32,13 @@ class ModuleMeta:
return output
@cached_property
def commands(self) -> List[Command]:
def commands(self) -> List[SimpleCommand]:
# This also matches RegexCommand objects
# The sort puts all the RegexCommand objects at the back, making them
# be matched last
# The sort puts all the RegexCommand objects at the back,making
# them be matched last
return sorted(
self._filter_attrs(lambda val: isinstance(val, Command)),
self._filter_attrs(lambda val: isinstance(val, SimpleCommand)),
key=lambda x: isinstance(x, RegexCommand),
)

16
pyproject.toml 100644
View File

@ -0,0 +1,16 @@
[tool.black]
line-length = 80
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs
| \.git
| venv
| build
| dist
)/
)
'''

View File

@ -2,6 +2,8 @@
# Third-party imports
import setuptools
# TODO: switch to alternative that uses pyproject.toml (e.g. poetry)
with open('requirements.txt', 'r') as reqs_file:
reqs = [line.strip() for line in reqs_file.readlines()]