This repository has been archived on 2021-03-28. You can view files and clone it, but cannot push or open issues/pull-requests.
frank/frank/module/decorators/functions.py

69 lines
1.5 KiB
Python
Raw Normal View History

2020-08-25 17:07:27 +02:00
# =====IMPORTS=====
2020-08-27 11:59:15 +02:00
# Future imports
from __future__ import annotations
2020-08-25 17:07:27 +02:00
# Own imports
from .classes import Command, RegexCommand, Daemon, Default
2020-08-27 11:59:15 +02:00
# Typing imports
from typing import TYPE_CHECKING
2020-08-27 11:59:15 +02:00
if TYPE_CHECKING:
# Built-in imports
from typing import Union
2020-08-25 17:07:27 +02:00
def command(cmd, help_str: str = None) -> 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
"""
def inner(func):
return Command(func, cmd, help_str)
return inner
def regex_command(pattern: str, help_str: str = None) -> callable:
"""
Converts the method into a RegexCommand.
Args:
pattern: regex pattern to match command with
help_str: short description of the command
"""
def inner(func):
return RegexCommand(func, pattern, help_str)
return inner
2020-08-27 11:59:15 +02:00
def daemon(interval: Union[int, float] = 0) -> callable:
2020-08-25 17:07:27 +02:00
"""
Converts the method into a Daemon, which will then be run when the module
is started.
"""
def inner(func):
2020-08-27 11:59:15 +02:00
return Daemon(func, interval)
2020-08-25 17:07:27 +02:00
return inner
# TODO: make sure the default is unique
def default(help_str: str = None) -> callable:
"""
Converts the method into the Default method, making it the default command
when the module is run without a command.
"""
def inner(func):
return Default(func, help_str)
return inner