mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 05:39:54 +08:00
fix + 简化
This commit is contained in:
parent
ff130d7f84
commit
e10dff96d2
|
@ -14,7 +14,7 @@ use colored::Colorize;
|
||||||
use pyo3::{
|
use pyo3::{
|
||||||
exceptions::PyTypeError,
|
exceptions::PyTypeError,
|
||||||
intern,
|
intern,
|
||||||
types::{PyAny, PyAnyMethods, PyModule, PyTracebackMethods, PyTuple},
|
types::{PyAnyMethods, PyModule, PyTracebackMethods, PyTuple},
|
||||||
Bound, Py, PyErr, PyResult, Python,
|
Bound, Py, PyErr, PyResult, Python,
|
||||||
};
|
};
|
||||||
use tracing::{event, span, warn, Level};
|
use tracing::{event, span, warn, Level};
|
||||||
|
@ -123,12 +123,21 @@ pub fn get_py_err_traceback(py_err: &PyErr) -> String {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PyPlugin {
|
pub struct PyPlugin {
|
||||||
pub file_path: PathBuf,
|
pub file_path: PathBuf,
|
||||||
pub changed_time: Option<SystemTime>,
|
pub modify_time: Option<SystemTime>,
|
||||||
pub py_module: Py<PyModule>,
|
pub py_module: Py<PyModule>,
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PyPlugin {
|
impl PyPlugin {
|
||||||
|
pub fn new(path: PathBuf, modify_time: Option<SystemTime>, module: Py<PyModule>) -> Self {
|
||||||
|
PyPlugin {
|
||||||
|
file_path: path.clone(),
|
||||||
|
modify_time,
|
||||||
|
py_module: module,
|
||||||
|
enabled: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 从文件创建一个新的
|
/// 从文件创建一个新的
|
||||||
pub fn new_from_path(path: &PathBuf) -> Option<Self> {
|
pub fn new_from_path(path: &PathBuf) -> Option<Self> {
|
||||||
let raw_file = load_py_file(path);
|
let raw_file = load_py_file(path);
|
||||||
|
@ -159,7 +168,7 @@ impl PyPlugin {
|
||||||
Ok(raw_file) => match Self::try_from(raw_file) {
|
Ok(raw_file) => match Self::try_from(raw_file) {
|
||||||
Ok(plugin) => {
|
Ok(plugin) => {
|
||||||
self.py_module = plugin.py_module;
|
self.py_module = plugin.py_module;
|
||||||
self.changed_time = plugin.changed_time;
|
self.modify_time = plugin.modify_time;
|
||||||
self.enabled = PyStatus::get().config.get_status(&self.get_id());
|
self.enabled = PyStatus::get().config.get_status(&self.get_id());
|
||||||
event!(Level::INFO, "更新 Python 插件文件 {:?} 完成", self.file_path);
|
event!(Level::INFO, "更新 Python 插件文件 {:?} 完成", self.file_path);
|
||||||
true
|
true
|
||||||
|
@ -186,7 +195,7 @@ impl PyPlugin {
|
||||||
match get_change_time(&self.file_path) {
|
match get_change_time(&self.file_path) {
|
||||||
None => false,
|
None => false,
|
||||||
Some(time) => {
|
Some(time) => {
|
||||||
if let Some(changed_time) = self.changed_time {
|
if let Some(changed_time) = self.modify_time {
|
||||||
time.eq(&changed_time)
|
time.eq(&changed_time)
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
|
@ -345,7 +354,7 @@ fn set_bytes_cfg_default_plugin(
|
||||||
impl TryFrom<RawPyPlugin> for PyPlugin {
|
impl TryFrom<RawPyPlugin> for PyPlugin {
|
||||||
type Error = PyErr;
|
type Error = PyErr;
|
||||||
fn try_from(value: RawPyPlugin) -> Result<Self, Self::Error> {
|
fn try_from(value: RawPyPlugin) -> Result<Self, Self::Error> {
|
||||||
let (path, changed_time, content) = value;
|
let (path, modify_time, content) = value;
|
||||||
let py_module: Py<PyModule> = match py_module_from_code(&content, &path) {
|
let py_module: Py<PyModule> = match py_module_from_code(&content, &path) {
|
||||||
Ok(module) => module,
|
Ok(module) => module,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -372,30 +381,20 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
|
||||||
"加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, bytes | str]",
|
"加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, bytes | str]",
|
||||||
path
|
path
|
||||||
);
|
);
|
||||||
return Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(
|
return Err(PyTypeError::new_err(
|
||||||
"返回的不是 [str, bytes | str]".to_string(),
|
"返回的不是 [str, bytes | str]".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(PyPlugin {
|
Ok(PyPlugin::new(path, modify_time, module.clone().unbind()))
|
||||||
file_path: path,
|
|
||||||
changed_time,
|
|
||||||
py_module: module.unbind(),
|
|
||||||
enabled: true,
|
|
||||||
})
|
|
||||||
} else if config.is_none() {
|
} else if config.is_none() {
|
||||||
// 没有配置文件
|
// 没有配置文件
|
||||||
Ok(PyPlugin {
|
Ok(PyPlugin::new(path, modify_time, module.clone().unbind()))
|
||||||
file_path: path,
|
|
||||||
changed_time,
|
|
||||||
py_module: module.unbind(),
|
|
||||||
enabled: true,
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
warn!(
|
warn!(
|
||||||
"加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]",
|
"加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]",
|
||||||
path
|
path
|
||||||
);
|
);
|
||||||
Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(
|
Err(PyTypeError::new_err(
|
||||||
"返回的不是 [str, str]".to_string(),
|
"返回的不是 [str, str]".to_string(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -406,12 +405,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(PyPlugin {
|
Ok(PyPlugin::new(path, modify_time, module.clone().unbind()))
|
||||||
file_path: path,
|
|
||||||
changed_time,
|
|
||||||
py_module: module.unbind(),
|
|
||||||
enabled: true,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user