1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
This commit is contained in:
Steffo 2019-04-18 14:09:55 +02:00
parent 5b9c441113
commit bad35daf5b
9 changed files with 48 additions and 49 deletions

View file

@ -1,4 +1,4 @@
from ..utils import Command, CommandArgs, Call from ..utils import Command, Call
from telegram import Update, User from telegram import Update, User

View file

@ -1,7 +1,6 @@
import datetime import datetime
import dateparser import dateparser
from ..utils import Command, Call from ..utils import Command, Call
from ..error import InvalidInputError
class DateparserCommand(Command): class DateparserCommand(Command):

View file

@ -1,6 +1,5 @@
import traceback import traceback
from logging import Logger from ..utils import Command, Call
from ..utils import Command, CommandArgs, Call
from ..error import NoneFoundError, \ from ..error import NoneFoundError, \
TooManyFoundError, \ TooManyFoundError, \
UnregisteredError, \ UnregisteredError, \

View file

@ -1,6 +1,6 @@
import asyncio import asyncio
from ..utils import Command, Call from ..utils import Command, Call
from royalnet.error import InvalidInputError from ..error import InvalidInputError
class PingCommand(Command): class PingCommand(Command):

View file

@ -2,7 +2,7 @@ import typing
from telegram import Update, User from telegram import Update, User
from discord import Message, Member from discord import Message, Member
from ..utils import Command, Call, asyncify from ..utils import Command, Call, asyncify
from royalnet.error import UnsupportedError from ..error import UnsupportedError
from ..database.tables import Royal, Telegram, Discord from ..database.tables import Royal, Telegram, Discord

View file

@ -1,10 +1,10 @@
from .asyncify import asyncify from .asyncify import asyncify
from .call import Call from .call import Call
from .command import Command, CommandArgs from .command import Command
from .commandargs import CommandArgs
from .safeformat import safeformat from .safeformat import safeformat
from .classdictjanitor import cdj from .classdictjanitor import cdj
from .sleepuntil import sleep_until from .sleepuntil import sleep_until
from .plusformat import plusformat from .plusformat import plusformat
__all__ = ["asyncify", "Call", "Command", "safeformat", "CommandArgs", __all__ = ["asyncify", "Call", "Command", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs"]
"cdj", "sleep_until", "plusformat"]

View file

@ -2,7 +2,9 @@ import typing
import asyncio import asyncio
import logging import logging
from ..network.messages import Message from ..network.messages import Message
from .command import Command, CommandArgs from .command import Command
from royalnet.utils import CommandArgs
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from ..database import Alchemy from ..database import Alchemy

View file

@ -1,47 +1,8 @@
import re
import typing import typing
from royalnet.error import InvalidInputError
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from .call import Call 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: class Command:
"""A generic command, called from any source.""" """A generic command, called from any source."""

View file

@ -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