aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--LICENSE2
-rw-r--r--pyproject.toml2
-rw-r--r--src/infini/core.py2
-rw-r--r--src/infini/loader.py23
-rw-r--r--src/infini/router.py32
5 files changed, 51 insertions, 10 deletions
diff --git a/LICENSE b/LICENSE
index ab2c80e0..85711601 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) 2023 HydroRoll-Team & 浊莲
+Copyright (c) 2024 HydroRoll-Team & Noctisynth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/pyproject.toml b/pyproject.toml
index 63a1f7f3..772a23cc 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "infini"
-version = "2.0.2a3"
+version = "2.0.2"
description = "Infini 核心标准文本输入输出模块"
authors = [
{ name = "苏向夜", email = "fu050409@163.com" },
diff --git a/src/infini/core.py b/src/infini/core.py
index 40ed6f4c..87066370 100644
--- a/src/infini/core.py
+++ b/src/infini/core.py
@@ -30,9 +30,9 @@ class Core:
yield self.generate(stream)
continue
outcome = stream
+ yield outcome
if handled_stream.block:
return
- yield outcome
def pre_intercept(self, input: Input) -> Generator[Output | Input, Any, None]:
return self.pre_interceptor.input(input)
diff --git a/src/infini/loader.py b/src/infini/loader.py
index eabf0236..623476f9 100644
--- a/src/infini/loader.py
+++ b/src/infini/loader.py
@@ -132,11 +132,28 @@ class Loader:
self.load_from_register(register)
def load_from_register(self, register: Register):
- self.pre_interceptors.extend(register.pre_interceptors)
- self.handlers.extend(register.handlers)
+ self.pre_interceptors = self._update_list(
+ self.pre_interceptors, register.interceptors
+ )
+ self.handlers = self._update_list(self.handlers, register.handlers)
self.events.update(register.events)
self.global_variables.update(register.global_variables)
- self.interceptors.extend(register.interceptors)
+ self.interceptors = self._update_list(self.interceptors, register.interceptors)
+
+ def _update_list(self, old_list: List[RouterType], new_list: List[RouterType]):
+ list = old_list.copy()
+ for value in new_list:
+ exists = False
+ for old_value in old_list:
+ if old_value["router"] == value["router"]:
+ if value["priority"] > old_value["priority"]:
+ list.remove(old_value)
+ else:
+ exists = True
+ break
+ if not exists:
+ list.append(value)
+ return list
def close(self):
uninstall()
diff --git a/src/infini/router.py b/src/infini/router.py
index 4b781ad9..c84e11a4 100644
--- a/src/infini/router.py
+++ b/src/infini/router.py
@@ -1,30 +1,54 @@
-from infini.typing import Sequence, List
+from infini.typing import Sequence, Literal
class Router:
- signs: List[str]
+ type: Literal["normal"] = "normal"
+ signs: set[str]
def __init__(self, sign: str, alias: Sequence[str] = []) -> None:
- self.signs = [sign]
- self.signs.extend(alias)
+ self.signs = {sign}
+ self.signs.update(alias)
+
+ def __eq__(self, __router: "Router") -> bool:
+ return __router.type == self.type and __router.signs == self.signs
def match(self, input: str) -> bool:
return any([input == sign for sign in self.signs])
class Startswith(Router):
+ type: Literal["startswith"] = "startswith"
+
def match(self, input: str) -> bool:
input = input.strip()
return any([input.startswith(sign) for sign in self.signs])
class Contains(Router):
+ type: Literal["contains"] = "contains"
+
def match(self, input: str) -> bool:
input = input.strip()
return any([sign in input for sign in self.signs])
class Endswith(Router):
+ type: Literal["endswith"] = "endswith"
+
def match(self, input: str) -> bool:
input = input.strip()
return any([input.endswith(sign) for sign in self.signs])
+
+
+class Command(Router):
+ type: Literal["command"] = "command"
+ prefix: tuple = (".", "/")
+
+ def match(self, input: str) -> bool:
+ input = input.strip()
+ if input:
+ if input.startswith(self.prefix):
+ input = input[1:]
+ return any([input.startswith(sign) for sign in self.signs])
+
+ return False