summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-01-26 23:29:12 +0800
committer苏向夜 <fu050409@163.com>2024-01-26 23:29:12 +0800
commit4316bdefad91f1f239dff393fc0741df74c2480f (patch)
treec6fa33b4a39a060d64474b234f344603785cac46
parente999efafbc75133b03211dbecaf9bc424bf478ff (diff)
downloadinfini-4316bdefad91f1f239dff393fc0741df74c2480f.tar.gz
infini-4316bdefad91f1f239dff393fc0741df74c2480f.zip
:sparkles: feat(register): add register feature
-rw-r--r--src/infini/register.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/infini/register.py b/src/infini/register.py
new file mode 100644
index 00000000..3be22343
--- /dev/null
+++ b/src/infini/register.py
@@ -0,0 +1,94 @@
+from infini.input import Input
+from infini.output import Output
+from infini.router import ContainsRouter, Router
+from infini.typing import List, Dict, Any, Callable, RouterType
+from functools import wraps
+
+
+class Register:
+ pre_interceptors: List[RouterType]
+ handlers: List[RouterType]
+ events: Dict[str, str]
+ global_variables: Dict[str, str | Callable]
+ interceptors: List[RouterType]
+
+ def __init__(self) -> None:
+ self.pre_interceptors = []
+ self.handlers = []
+ self.events = {}
+ self.global_variables = {}
+ self.interceptors = []
+
+ def pre_interceptor(self, router: Router | str, priority: int = 0):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(input: Input) -> Input | Output:
+ return func(input)
+
+ self.pre_interceptors.append(
+ {
+ "priority": priority,
+ "router": ContainsRouter(router)
+ if isinstance(router, str)
+ else router,
+ "handler": wrapper,
+ }
+ )
+ return wrapper
+
+ return decorator
+
+ def handler(self, router: Router | str, priority: int = 0):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(input: Input) -> Output:
+ return func(input)
+
+ self.handlers.append(
+ {
+ "priority": priority,
+ "router": ContainsRouter(router)
+ if isinstance(router, str)
+ else router,
+ "handler": wrapper,
+ }
+ )
+ return wrapper
+
+ return decorator
+
+ def regist_textevent(self, name: str, text: str):
+ self.events[name] = text
+
+ def regist_variable(self, name: str, data: Any):
+ self.global_variables[name] = data
+
+ def dynamic_variable(self, name: str | None = None):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(*args, **kwargs) -> str:
+ return func(*args, **kwargs)
+
+ self.global_variables[name or func.__name__] = wrapper
+ return wrapper
+
+ return decorator
+
+ def interceptor(self, router: Router | str, priority: int = 0):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(input: Input) -> Input | Output:
+ return func(input)
+
+ self.interceptors.append(
+ {
+ "priority": priority,
+ "router": ContainsRouter(router)
+ if isinstance(router, str)
+ else router,
+ "handler": wrapper,
+ }
+ )
+ return wrapper
+
+ return decorator