diff options
| -rw-r--r-- | hrc/core.py | 35 | ||||
| -rw-r--r-- | hrc/rule/__init__.py | 16 | ||||
| -rw-r--r-- | src/dev/mod.rs | 1 | ||||
| -rw-r--r-- | src/doc/mod.rs | 1 | ||||
| -rw-r--r-- | src/feat/mod.rs | 1 | ||||
| -rw-r--r-- | src/lib.rs | 59 | ||||
| -rw-r--r-- | src/perf/mod.rs | 2 |
7 files changed, 62 insertions, 53 deletions
diff --git a/hrc/core.py b/hrc/core.py index 0e124e3..3d19b46 100644 --- a/hrc/core.py +++ b/hrc/core.py @@ -512,18 +512,6 @@ class Core: rule_load_type: Optional[RuleLoadType] = None, reload: bool = False, ) -> None: - """Load rules. - - Args: - *rules: plug-in class, plug-in module name or plug-in module file path. Type can be ``Type[rule]``, ``str`` or ``pathlib.Path``. - If it is ``Type[rule]``, it will be loaded as a plug-in class. - If it is of type ``str``, it will be loaded as the plug-in module name, and the format is the same as the Python ``import`` statement. - For example: ``path.of.rule``. - If it is of type ``pathlib.Path``, it will be loaded as the plug-in module file path. - For example: ``pathlib.Path("path/of/rule")``. - rule_load_type: Plug-in loading type, if it is ``None``, it will be automatically determined, otherwise the specified type will be used. - reload: Whether to reload the module. - """ for rule_ in rules: try: if isinstance(rule_, type) and issubclass(rule_, Rule): @@ -585,28 +573,11 @@ class Core: def load_rules( self, *rules: Union[Type[Rule[Any, Any, Any]], str, Path] ) -> None: - """Load the rule. - - Args: - *rules: ``rule`` class, rule module name or plug-in module file path. - Type can be ``Type[rule]``, ``str`` or ``pathlib.Path``. - If it is ``Type[rule]``, it will be loaded as a plug-in class. - If it is of type ``str``, it will be loaded as the plug-in module name, and the format is the same as the Python ``import`` statement. - For example: ``path.of.rule``. - If it is of type ``pathlib.Path``, it will be loaded as the plug-in module file path. - For example: ``pathlib.Path("path/of/rule")``. - """ self._extend_rules.extend(rules) return self._load_rules(*rules) def _load_rules_from_dirs(self, *dirs: Path) -> None: - """Load plug-ins from the directory. Plug-ins in modules starting with ``_`` will not be imported. The path can be a relative path or an absolute path. - - Args: - *dirs: Module paths that store modules containing rules. - For example: ``pathlib.Path("path/of/rules/")`` . - """ dir_list = [str(x.resolve()) for x in dirs] logger.info( f'Loading rules from dirs "{", ".join(map(str, dir_list))}"') @@ -618,12 +589,6 @@ class Core: ) def load_rules_from_dirs(self, *dirs: Path) -> None: - """Load plug-ins from the directory. Plug-ins in modules starting with ``_`` will not be imported. The path can be a relative path or an absolute path. - - Args: - *dirs: Module paths that store modules containing rules. - For example: ``pathlib.Path("path/of/rules/")`` . - """ self._extend_rule_dirs.extend(dirs) self._load_rules_from_dirs(*dirs) diff --git a/hrc/rule/__init__.py b/hrc/rule/__init__.py index 4b3d259..467f74a 100644 --- a/hrc/rule/__init__.py +++ b/hrc/rule/__init__.py @@ -7,7 +7,7 @@ from . import BaseRule # noqa: F401 from ..typing import RuleT # noqa: F401 import inspect -from abc import abstractmethod +from abc import abstractmethod # noqa: F401 from enum import Enum from typing import ( TYPE_CHECKING, @@ -140,20 +140,6 @@ class Rule(ABC, Generic[EventT, StateT, ConfigT]): @final def state(self, value: StateT) -> None: self.core.rule_state[self.name] = value - - @abstractmethod - async def handle(self) -> None: - """Method to handle events. iamai will call this method when the ``rule()`` method returns ``True``. Each plugin must implement this method.""" - raise NotImplementedError - - @abstractmethod - async def rule(self) -> bool: - """Method to match the event. When the event is processed, this method will be called in sequence according to the priority of the plugin. When this method returns ``True``, the event will be handed over to this plugin for processing. Each plugin must implement this method. - - .. note:: - It is not recommended to implement event processing directly in this method. Please leave the specific processing of events to the ``handle()`` method. - """ - raise NotImplementedError @staticmethod async def enable(): ... diff --git a/src/dev/mod.rs b/src/dev/mod.rs index e69de29..8b13789 100644 --- a/src/dev/mod.rs +++ b/src/dev/mod.rs @@ -0,0 +1 @@ + diff --git a/src/doc/mod.rs b/src/doc/mod.rs index e69de29..8b13789 100644 --- a/src/doc/mod.rs +++ b/src/doc/mod.rs @@ -0,0 +1 @@ + diff --git a/src/feat/mod.rs b/src/feat/mod.rs index e69de29..8b13789 100644 --- a/src/feat/mod.rs +++ b/src/feat/mod.rs @@ -0,0 +1 @@ + @@ -1,3 +1,4 @@ +use clap::builder::Str; use pyo3::prelude::*; use pyo3::wrap_pyfunction; @@ -17,11 +18,65 @@ fn process_rule_pack(rule_pack: &str) -> PyResult<String> { Ok(format!("Processed rule pack: {}", rule_pack)) } -/// A Python module implemented in Rust. +#[pyclass] +struct Integer { + inner: i32, +} + +// A "tuple" struct +#[pyclass] +struct Number(i32); + +// PyO3 supports custom discriminants in enums +#[pyclass] +enum HttpResponse { + Ok = 200, + NotFound = 404, + Teapot = 418, + // ... +} + +use pyo3::exceptions::PyValueError; +use pyo3::prelude::*; +use pyo3::types::PyString; + +#[pyclass] +#[derive(Debug, Clone)] +enum RuleLoadType { + DIR, + NAME, + FILE, + CLASS, +} + +#[pymethods] +impl RuleLoadType { + #[staticmethod] + fn from_str(s: &str) -> PyResult<Self> { + match s { + "dir" => Ok(RuleLoadType::DIR), + "name" => Ok(RuleLoadType::NAME), + "file" => Ok(RuleLoadType::FILE), + "class" => Ok(RuleLoadType::CLASS), + _ => Err(PyValueError::new_err("Invalid value for RuleLoadType")), + } + } + + fn __str__(&self) -> PyResult<String> { + Ok(match self { + RuleLoadType::DIR => "dir", + RuleLoadType::NAME => "name", + RuleLoadType::FILE => "file", + RuleLoadType::CLASS => "class", + } + .to_string()) + } +} + #[pymodule] #[pyo3(name = "LibCore")] fn libcore(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(process_rule_pack, m)?)?; - perf::Asparagus; + m.add_class::<RuleLoadType>()?; Ok(()) } diff --git a/src/perf/mod.rs b/src/perf/mod.rs index 0444a3a..5dfb691 100644 --- a/src/perf/mod.rs +++ b/src/perf/mod.rs @@ -1 +1 @@ -pub struct Asparagus {}
\ No newline at end of file +pub struct Asparagus {} |
