From 32ab8538a43ebb9f13d715cd439fa0db35fc9390 Mon Sep 17 00:00:00 2001 From: chewingbever Date: Fri, 4 Sep 2020 11:12:22 +0200 Subject: [PATCH] Unfinished changes, i gotta push --- .flake8 | 2 ++ frank/module/decorators/__init__.py | 4 +-- frank/module/decorators/classes.py | 42 ++++++++++++++++++++++------ frank/module/decorators/functions.py | 16 ++++++++--- frank/module/meta.py | 10 +++---- pyproject.toml | 16 +++++++++++ setup.py | 2 ++ 7 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 .flake8 create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..dcc253a --- /dev/null +++ b/.flake8 @@ -0,0 +1,2 @@ +[flake8] +inline-quotes = double diff --git a/frank/module/decorators/__init__.py b/frank/module/decorators/__init__.py index cc5e89d..67c183b 100644 --- a/frank/module/decorators/__init__.py +++ b/frank/module/decorators/__init__.py @@ -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", diff --git a/frank/module/decorators/classes.py b/frank/module/decorators/classes.py index f0fa326..c35ce09 100644 --- a/frank/module/decorators/classes.py +++ b/frank/module/decorators/classes.py @@ -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 """ diff --git a/frank/module/decorators/functions.py b/frank/module/decorators/functions.py index d60fcec..60ef048 100644 --- a/frank/module/decorators/functions.py +++ b/frank/module/decorators/functions.py @@ -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 diff --git a/frank/module/meta.py b/frank/module/meta.py index 29f0977..4446092 100644 --- a/frank/module/meta.py +++ b/frank/module/meta.py @@ -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), ) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..f96e88f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,16 @@ +[tool.black] +line-length = 80 +target-version = ['py38'] +include = '\.pyi?$' +exclude = ''' + +( + /( + \.eggs + | \.git + | venv + | build + | dist + )/ +) +''' diff --git a/setup.py b/setup.py index 9238b29..2cd43fa 100644 --- a/setup.py +++ b/setup.py @@ -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()]