This commit is contained in:
csunny
2023-05-10 20:52:31 +08:00
parent fd8bc8d169
commit eb069f1a45
2 changed files with 36 additions and 0 deletions

36
pilot/commands/command.py Normal file
View File

@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import functools
import importlib
import inspect
from typing import Any, Callable, Optional
class Command:
"""A class representing a command.
Attributes:
name (str): The name of the command.
description (str): A brief description of what the command does.
signature (str): The signature of the function that the command executes. Default to None.
"""
def __init__(self,
name: str,
description: str,
method: Callable[..., Any],
signature: str = "",
enabled: bool = True,
disabled_reason: Optional[str] = None,
) -> None:
self.name = name
self.description = description
self.method = method
self.signature = signature if signature else str(inspect.signature(self.method))
self.enabled = enabled
self.disabled_reason = disabled_reason
def __call__(self, *args: Any, **kwds: Any) -> Any:
if not self.enabled:
return f"Command '{self.name}' is disabled: {self.disabled_reason}"
return self.method(*args, **kwds)