From bad35daf5b4388c66746bb1a29be25c6332ff7c1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 18 Apr 2019 14:09:55 +0200 Subject: [PATCH] Do stuff --- royalnet/commands/ciaoruozi.py | 2 +- royalnet/commands/dateparser.py | 1 - royalnet/commands/error_handler.py | 3 +-- royalnet/commands/ping.py | 2 +- royalnet/commands/sync.py | 2 +- royalnet/utils/__init__.py | 6 ++--- royalnet/utils/call.py | 4 ++- royalnet/utils/command.py | 39 ------------------------------ royalnet/utils/commandargs.py | 38 +++++++++++++++++++++++++++++ 9 files changed, 48 insertions(+), 49 deletions(-) create mode 100644 royalnet/utils/commandargs.py diff --git a/royalnet/commands/ciaoruozi.py b/royalnet/commands/ciaoruozi.py index 9a4ef70b..2929512e 100644 --- a/royalnet/commands/ciaoruozi.py +++ b/royalnet/commands/ciaoruozi.py @@ -1,4 +1,4 @@ -from ..utils import Command, CommandArgs, Call +from ..utils import Command, Call from telegram import Update, User diff --git a/royalnet/commands/dateparser.py b/royalnet/commands/dateparser.py index f7f44522..22a94aa8 100644 --- a/royalnet/commands/dateparser.py +++ b/royalnet/commands/dateparser.py @@ -1,7 +1,6 @@ import datetime import dateparser from ..utils import Command, Call -from ..error import InvalidInputError class DateparserCommand(Command): diff --git a/royalnet/commands/error_handler.py b/royalnet/commands/error_handler.py index ff3b2d82..0081ffde 100644 --- a/royalnet/commands/error_handler.py +++ b/royalnet/commands/error_handler.py @@ -1,6 +1,5 @@ import traceback -from logging import Logger -from ..utils import Command, CommandArgs, Call +from ..utils import Command, Call from ..error import NoneFoundError, \ TooManyFoundError, \ UnregisteredError, \ diff --git a/royalnet/commands/ping.py b/royalnet/commands/ping.py index 489ea900..7b2608d4 100644 --- a/royalnet/commands/ping.py +++ b/royalnet/commands/ping.py @@ -1,6 +1,6 @@ import asyncio from ..utils import Command, Call -from royalnet.error import InvalidInputError +from ..error import InvalidInputError class PingCommand(Command): diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index 128867c9..b8c73fa8 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -2,7 +2,7 @@ import typing from telegram import Update, User from discord import Message, Member from ..utils import Command, Call, asyncify -from royalnet.error import UnsupportedError +from ..error import UnsupportedError from ..database.tables import Royal, Telegram, Discord diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index fa04fc33..925bd023 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -1,10 +1,10 @@ from .asyncify import asyncify from .call import Call -from .command import Command, CommandArgs +from .command import Command +from .commandargs import CommandArgs from .safeformat import safeformat from .classdictjanitor import cdj from .sleepuntil import sleep_until from .plusformat import plusformat -__all__ = ["asyncify", "Call", "Command", "safeformat", "CommandArgs", - "cdj", "sleep_until", "plusformat"] +__all__ = ["asyncify", "Call", "Command", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs"] diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index a91bd618..e3bd6ea1 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -2,7 +2,9 @@ import typing import asyncio import logging from ..network.messages import Message -from .command import Command, CommandArgs +from .command import Command +from royalnet.utils import CommandArgs + if typing.TYPE_CHECKING: from ..database import Alchemy diff --git a/royalnet/utils/command.py b/royalnet/utils/command.py index 399b8595..d1f0c077 100644 --- a/royalnet/utils/command.py +++ b/royalnet/utils/command.py @@ -1,47 +1,8 @@ -import re import typing - -from royalnet.error import InvalidInputError - if typing.TYPE_CHECKING: from .call import Call -class CommandArgs(list): - """The arguments of a command. Raises InvalidInputError if the requested argument does not exist.""" - - def __getitem__(self, item): - if isinstance(item, int): - try: - return super().__getitem__(item) - except IndexError: - raise InvalidInputError(f'Tried to get missing [{item}] arg from CommandArgs') - if isinstance(item, slice): - try: - return super().__getitem__(item) - except IndexError: - raise InvalidInputError(f'Tried to get invalid [{item}] slice from CommandArgs') - raise ValueError(f"Invalid type passed to CommandArgs.__getattr__: {type(item)}") - - def joined(self, *, require_at_least=0): - if len(self) < require_at_least: - raise InvalidInputError("Not enough arguments") - return " ".join(self) - - def match(self, pattern: typing.Pattern) -> typing.Sequence[typing.AnyStr]: - text = self.joined() - match = re.match(pattern, text) - if match is None: - raise InvalidInputError("Pattern didn't match") - return match.groups() - - def optional(self, index: int, default=None) -> typing.Optional: - try: - return self[index] - except InvalidInputError: - return default - - class Command: """A generic command, called from any source.""" diff --git a/royalnet/utils/commandargs.py b/royalnet/utils/commandargs.py new file mode 100644 index 00000000..490c201f --- /dev/null +++ b/royalnet/utils/commandargs.py @@ -0,0 +1,38 @@ +import re +import typing +from royalnet.error import InvalidInputError + + +class CommandArgs(list): + """The arguments of a command. Raises InvalidInputError if the requested argument does not exist.""" + + def __getitem__(self, item): + if isinstance(item, int): + try: + return super().__getitem__(item) + except IndexError: + raise InvalidInputError(f'Tried to get missing [{item}] arg from CommandArgs') + if isinstance(item, slice): + try: + return super().__getitem__(item) + except IndexError: + raise InvalidInputError(f'Tried to get invalid [{item}] slice from CommandArgs') + raise ValueError(f"Invalid type passed to CommandArgs.__getattr__: {type(item)}") + + def joined(self, *, require_at_least=0): + if len(self) < require_at_least: + raise InvalidInputError("Not enough arguments") + return " ".join(self) + + def match(self, pattern: typing.Pattern) -> typing.Sequence[typing.AnyStr]: + text = self.joined() + match = re.match(pattern, text) + if match is None: + raise InvalidInputError("Pattern didn't match") + return match.groups() + + def optional(self, index: int, default=None) -> typing.Optional: + try: + return self[index] + except InvalidInputError: + return default \ No newline at end of file