Skip to content

Commit

Permalink
refactor: Comment hook function while running the rules packages
Browse files Browse the repository at this point in the history
TODO: builtin hook function in every rules packages.
  • Loading branch information
HsiangNianian committed Jul 18, 2024
1 parent 0f39a9d commit 14368e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
41 changes: 22 additions & 19 deletions hrc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,34 @@ async def _run(self) -> None:
await core_run_hook_func(self)

try:
for _rule in self.rules:
for rule_enable_hook_func in self._rule_enable_hooks:
await rule_enable_hook_func(_rule)
try:
await _rule.enable()
except Exception as e:
self.error_or_exception(
f"Enable rule {_rule!r} failed:", e)

for _rule in self.rules:
for rule_run_hook_func in self._rule_run_hooks:
await rule_run_hook_func(_rule)
_rule_task = asyncio.create_task(_rule.safe_run())
self._rule_tasks.add(_rule_task)
_rule_task.add_done_callback(self._rule_tasks.discard)
##########################################################
# @TODO: builtin hook function in every rules packages.
##########################################################
# for _rule in self.rules:
# for rule_enable_hook_func in self._rule_enable_hooks:
# await rule_enable_hook_func(_rule)
# try:
# await _rule.enable()
# except Exception as e:
# self.error_or_exception(
# f"Enable rule {_rule!r} failed:", e)

# for _rule in self.rules:
# for rule_run_hook_func in self._rule_run_hooks:
# await rule_run_hook_func(_rule)
# _rule_task = asyncio.create_task(_rule.safe_run())
# self._rule_tasks.add(_rule_task)
# _rule_task.add_done_callback(self._rule_tasks.discard)

await self.should_exit.wait()

if hot_reload_task is not None: # pragma: no cover
await hot_reload_task
finally:
for _rule in self.rules:
for rule_disable_hook_func in self._rule_disable_hooks:
await rule_disable_hook_func(_rule)
await _rule.disable()
# for _rule in self.rules:
# for rule_disable_hook_func in self._rule_disable_hooks:
# await rule_disable_hook_func(_rule)
# await _rule.disable()

while self._rule_tasks:
await asyncio.sleep(0)
Expand Down
19 changes: 13 additions & 6 deletions hrc/rule/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ def state(self) -> StateT:
@final
def state(self, value: StateT) -> None:
self.core.rule_state[self.name] = value

@staticmethod
async def enable(): ...

@staticmethod
async def disable(): ...

async def enable(self): ...

async def disable(self): ...

@staticmethod
def aliases(names, ignore_case=False):
Expand All @@ -155,3 +153,12 @@ def decorator(func):
return func

return decorator

@final
async def safe_run(self) -> None:
try:
await self.enable()
except Exception as e:
self.bot.error_or_exception(
f"Enable rule {self.__class__.__name__} failed:", e
)
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use clap::builder::FalseyValueParser;
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn process_rule_pack(rule_pack: &str) -> PyResult<String> {
// 处理规则包的逻辑
Ok(format!("Processed rule pack: {}", rule_pack))
}

#[pymodule]
#[pyo3(name = "LibCore")]
fn libcore(_py: Python, m: &PyModule) -> PyResult<()> {
let py_log = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/hrc/log.py"));
let py_event = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/hrc/event.py"));
let py_core = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/hrc/core.py"));
let py_const = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/hrc/const.py"));

m.add_function(wrap_pyfunction!(process_rule_pack, m)?)?;
Ok(())
}

0 comments on commit 14368e5

Please sign in to comment.