Module graia.application.message.parser.signature

Expand source code
import abc
from typing import Any, Callable, Optional as O
from pydantic import BaseModel, validator
from graia.application.message.chain import MessageChain
import regex


class NormalMatch(BaseModel, abc.ABC):
    @abc.abstractmethod
    def operator(self) -> str:
        pass


class PatternReceiver(BaseModel):
    name: str
    isGreed: bool = False

    def __init__(self, name: str, isGreed: bool = False) -> None:
        super().__init__(name=name, isGreed=isGreed)

    @validator("name", allow_reuse=True)
    def name_checker(cls, v):
        if not regex.match("^[a-zA-Z_][a-zA-Z0-9_]*$", v):
            raise ValueError("invaild name")
        return v

    def __hash__(self) -> int:
        return super(object, self).__hash__()


class FullMatch(NormalMatch):
    pattern: str

    def __init__(self, pattern) -> None:
        super().__init__(pattern=pattern)

    def operator(self):
        return regex.escape(self.pattern)


class RegexMatch(NormalMatch):
    pattern: str

    def __init__(self, pattern) -> None:
        super().__init__(pattern=pattern)

    def operator(self):
        return self.pattern


class RequireParam(PatternReceiver):
    checker: O[Callable[[MessageChain], bool]] = None
    translator: O[Callable[[MessageChain], Any]] = None


class OptionalParam(PatternReceiver):
    checker: O[Callable[[MessageChain], bool]] = None
    translator: O[Callable[[MessageChain], Any]] = None

Classes

class FullMatch (pattern)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class FullMatch(NormalMatch):
    pattern: str

    def __init__(self, pattern) -> None:
        super().__init__(pattern=pattern)

    def operator(self):
        return regex.escape(self.pattern)

Ancestors

  • NormalMatch
  • pydantic.main.BaseModel
  • pydantic.utils.Representation
  • abc.ABC

Class variables

var pattern : str

Methods

def operator(self)
Expand source code
def operator(self):
    return regex.escape(self.pattern)
class NormalMatch (**data: Any)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class NormalMatch(BaseModel, abc.ABC):
    @abc.abstractmethod
    def operator(self) -> str:
        pass

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation
  • abc.ABC

Subclasses

Methods

def operator(self) ‑> str
Expand source code
@abc.abstractmethod
def operator(self) -> str:
    pass
class OptionalParam (name: str, isGreed: bool = False)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class OptionalParam(PatternReceiver):
    checker: O[Callable[[MessageChain], bool]] = None
    translator: O[Callable[[MessageChain], Any]] = None

Ancestors

Class variables

var checker : Union[Callable[[MessageChain], bool], NoneType]
var translator : Union[Callable[[MessageChain], Any], NoneType]
class PatternReceiver (name: str, isGreed: bool = False)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class PatternReceiver(BaseModel):
    name: str
    isGreed: bool = False

    def __init__(self, name: str, isGreed: bool = False) -> None:
        super().__init__(name=name, isGreed=isGreed)

    @validator("name", allow_reuse=True)
    def name_checker(cls, v):
        if not regex.match("^[a-zA-Z_][a-zA-Z0-9_]*$", v):
            raise ValueError("invaild name")
        return v

    def __hash__(self) -> int:
        return super(object, self).__hash__()

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Subclasses

Class variables

var isGreed : bool
var name : str

Static methods

def name_checker(v)
Expand source code
@validator("name", allow_reuse=True)
def name_checker(cls, v):
    if not regex.match("^[a-zA-Z_][a-zA-Z0-9_]*$", v):
        raise ValueError("invaild name")
    return v
class RegexMatch (pattern)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class RegexMatch(NormalMatch):
    pattern: str

    def __init__(self, pattern) -> None:
        super().__init__(pattern=pattern)

    def operator(self):
        return self.pattern

Ancestors

  • NormalMatch
  • pydantic.main.BaseModel
  • pydantic.utils.Representation
  • abc.ABC

Class variables

var pattern : str

Methods

def operator(self)
Expand source code
def operator(self):
    return self.pattern
class RequireParam (name: str, isGreed: bool = False)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class RequireParam(PatternReceiver):
    checker: O[Callable[[MessageChain], bool]] = None
    translator: O[Callable[[MessageChain], Any]] = None

Ancestors

Class variables

var checker : Union[Callable[[MessageChain], bool], NoneType]
var translator : Union[Callable[[MessageChain], Any], NoneType]