mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 15:29:54 +08:00
Compare commits
No commits in common. "1f7ffcb2d4d1ecaf662e43cc7ed70848713db061" and "09aaccf291b14c33f0cd4cb761a58162cf662953" have entirely different histories.
1f7ffcb2d4
...
09aaccf291
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,5 +1,5 @@
|
||||||
venv
|
venv
|
||||||
env*
|
env
|
||||||
|
|
||||||
config.toml
|
config.toml
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,6 @@ notice_start = true # 是否在启动 bot 后通知
|
||||||
|
|
||||||
# 机器人的管理员
|
# 机器人的管理员
|
||||||
admin_list = [0] # 机器人的管理员
|
admin_list = [0] # 机器人的管理员
|
||||||
# 过滤的人
|
|
||||||
filter_list = [0]
|
|
||||||
|
|
||||||
# python 插件路径
|
# python 插件路径
|
||||||
py_plugin_path = "/path/to/your/plugin"
|
py_plugin_path = "/path/to/your/plugin"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ica-rs"
|
name = "ica-rs"
|
||||||
version = "0.4.8"
|
version = "0.4.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -9,6 +9,7 @@ edition = "2021"
|
||||||
ed25519 = "2.2.3"
|
ed25519 = "2.2.3"
|
||||||
ed25519-dalek = "2.1.1"
|
ed25519-dalek = "2.1.1"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
|
blake3 = "1.5.0"
|
||||||
rust_socketio = { version = "0.4.4", features = ["async"]}
|
rust_socketio = { version = "0.4.4", features = ["async"]}
|
||||||
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -6,7 +6,7 @@ else:
|
||||||
NewMessage = TypeVar("NewMessage")
|
NewMessage = TypeVar("NewMessage")
|
||||||
IcaClient = TypeVar("IcaClient")
|
IcaClient = TypeVar("IcaClient")
|
||||||
|
|
||||||
_version_ = "1.1.0"
|
_version_ = "1.0.0"
|
||||||
|
|
||||||
def on_message(msg: NewMessage, client: IcaClient) -> None:
|
def on_message(msg: NewMessage, client: IcaClient) -> None:
|
||||||
if not (msg.is_from_self or msg.is_reply):
|
if not (msg.is_from_self or msg.is_reply):
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import re
|
|
||||||
import time
|
import time
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from typing import TYPE_CHECKING, TypeVar, Optional
|
from typing import TYPE_CHECKING, TypeVar
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ica_typing import NewMessage, IcaClient
|
from ica_typing import NewMessage, IcaClient
|
||||||
|
@ -10,7 +10,7 @@ else:
|
||||||
NewMessage = TypeVar("NewMessage")
|
NewMessage = TypeVar("NewMessage")
|
||||||
IcaClient = TypeVar("IcaClient")
|
IcaClient = TypeVar("IcaClient")
|
||||||
|
|
||||||
_version_ = "2.1.2-rs"
|
_version_ = "2.0.0-rs"
|
||||||
|
|
||||||
def format_data_size(data_bytes: float) -> str:
|
def format_data_size(data_bytes: float) -> str:
|
||||||
data_lens = ["B", "KB", "MB", "GB", "TB"]
|
data_lens = ["B", "KB", "MB", "GB", "TB"]
|
||||||
|
@ -46,24 +46,18 @@ def format_hit_count(count: int) -> str:
|
||||||
else:
|
else:
|
||||||
return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4))
|
return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4))
|
||||||
|
|
||||||
|
def bmcl(msg: NewMessage, client: IcaClient) -> None:
|
||||||
def wrap_request(url: str, client: IcaClient) -> Optional[dict]:
|
req_time = time.time()
|
||||||
response = requests.get(url)
|
# 记录请求时间
|
||||||
|
response = requests.get("https://bd.bangbang93.com/openbmclapi/metric/dashboard")
|
||||||
if not response.status_code == 200 or response.reason != "OK":
|
if not response.status_code == 200 or response.reason != "OK":
|
||||||
|
reply = msg.reply_with(f"请求数据失败\n{response.status_code}")
|
||||||
client.warn(
|
client.warn(
|
||||||
f"数据请求失败, 请检查网络\n{response.status}"
|
f"数据请求失败, 请检查网络\n{response.status}"
|
||||||
)
|
)
|
||||||
return None
|
client.send_message(reply)
|
||||||
return response.json()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
|
|
||||||
req_time = time.time()
|
|
||||||
# 记录请求时间
|
|
||||||
data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/dashboard", client)
|
|
||||||
if data is None:
|
|
||||||
return
|
return
|
||||||
|
data = response.json()
|
||||||
data_bytes: float = data["bytes"]
|
data_bytes: float = data["bytes"]
|
||||||
data_hits: int = data["hits"]
|
data_hits: int = data["hits"]
|
||||||
data_bandwidth: float = data["currentBandwidth"]
|
data_bandwidth: float = data["currentBandwidth"]
|
||||||
|
@ -74,7 +68,7 @@ def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
|
||||||
hits_count = format_hit_count(data_hits)
|
hits_count = format_hit_count(data_hits)
|
||||||
|
|
||||||
report_msg = (
|
report_msg = (
|
||||||
f"OpenBMCLAPI 面板v{_version_}-状态\n"
|
f"OpenBMCLAPI 状态面板v{_version_} :\n"
|
||||||
f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n"
|
f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n"
|
||||||
f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n"
|
f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n"
|
||||||
f"当日请求: {hits_count} 数据量: {data_len}\n"
|
f"当日请求: {hits_count} 数据量: {data_len}\n"
|
||||||
|
@ -84,113 +78,9 @@ def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None:
|
||||||
client.info(report_msg)
|
client.info(report_msg)
|
||||||
reply = msg.reply_with(report_msg)
|
reply = msg.reply_with(report_msg)
|
||||||
client.send_message(reply)
|
client.send_message(reply)
|
||||||
|
|
||||||
|
|
||||||
def parse_rank(data: dict) -> dict:
|
|
||||||
rank_data = {"hits": 0, "bytes": 0}
|
|
||||||
if "metric" in data:
|
|
||||||
rank_data["hits"] = data["metric"]["hits"]
|
|
||||||
rank_data["bytes"] = data["metric"]["bytes"]
|
|
||||||
return {
|
|
||||||
"name": data["name"],
|
|
||||||
"start": data["isEnabled"],
|
|
||||||
# "full": "全量" if "fullSize" in data else "分片",
|
|
||||||
# "version": data["version"] if "version" in data else "未知版本",
|
|
||||||
"owner": data["sponsor"]["name"] if "sponsor" in data else "未知",
|
|
||||||
"rank": rank_data
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None:
|
|
||||||
req_time = time.time()
|
|
||||||
# 记录请求时间
|
|
||||||
rank_data = wrap_request("https://bd.bangbang93.com/openbmclapi/metric/rank", client)
|
|
||||||
if rank_data is None:
|
|
||||||
return
|
|
||||||
ranks = [parse_rank(data) for data in rank_data]
|
|
||||||
if name is None:
|
|
||||||
# 全部排名
|
|
||||||
# 显示前3名
|
|
||||||
limit = 3
|
|
||||||
if len(ranks) < limit:
|
|
||||||
show_ranks = ranks
|
|
||||||
else:
|
|
||||||
show_ranks = ranks[:limit]
|
|
||||||
rank_msg = (
|
|
||||||
f"{'✅' if r['start'] else '❌'}名称: {r['name']}\n"
|
|
||||||
# f"-{rank['full']} \n"
|
|
||||||
# f"版本: {r['version']}\n"
|
|
||||||
f"赞助者: {r['owner']}|"
|
|
||||||
f"h/d {format_hit_count(r['rank']['hits'])}|{format_data_size(r['rank']['bytes'])}"
|
|
||||||
for r in show_ranks
|
|
||||||
)
|
|
||||||
rank_msg = "\n".join(rank_msg)
|
|
||||||
report_msg = (
|
|
||||||
f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n"
|
|
||||||
f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\n"
|
|
||||||
)
|
|
||||||
reply = msg.reply_with(report_msg)
|
|
||||||
client.info(report_msg)
|
|
||||||
client.send_message(reply)
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
# 搜索是否有这个名字的节点
|
|
||||||
names = [r["name"].lower() for r in ranks]
|
|
||||||
finds = [re.search(name.lower(), n) for n in names]
|
|
||||||
if not any(finds):
|
|
||||||
reply = msg.reply_with(f"未找到名为{name}的节点")
|
|
||||||
client.send_message(reply)
|
|
||||||
return
|
|
||||||
# 如果找到 > 3 个节点, 则提示 不显示
|
|
||||||
counts = [True for find in finds if find]
|
|
||||||
if len(counts) > 3:
|
|
||||||
if len(counts) > 10:
|
|
||||||
reply = msg.reply_with(f"搜索|{name}|到{len(counts)}个节点, 请用更精确的名字")
|
|
||||||
else:
|
|
||||||
# 4~10 个节点 只显示名称和次序
|
|
||||||
find_msg = [f"{'✅' if r['start'] else '❌'}{r['name']}-No.{i+1}" for i, r in enumerate(ranks) if finds[i]]
|
|
||||||
find_msg = "\n".join(find_msg)
|
|
||||||
report_msg = f"OpenBMCLAPI 面板v{_version_}-搜索|{name}|\n{find_msg}"
|
|
||||||
reply = msg.reply_with(report_msg)
|
|
||||||
client.send_message(reply)
|
|
||||||
return
|
|
||||||
rank_msgs = []
|
|
||||||
for i, find in enumerate(finds):
|
|
||||||
if find:
|
|
||||||
rank = ranks[i]
|
|
||||||
rank_msg = (
|
|
||||||
f"{'✅' if rank['start'] else '❌'}名称: {rank['name']}-No.{i+1}\n"
|
|
||||||
# f"-{rank['full']} \n"
|
|
||||||
# f"版本: {rank['version']}\n"
|
|
||||||
f"赞助商: {rank['owner']}|"
|
|
||||||
f"h/d {format_hit_count(rank['rank']['hits'])}|{format_data_size(rank['rank']['bytes'])}"
|
|
||||||
)
|
|
||||||
rank_msgs.append(rank_msg)
|
|
||||||
rank_msgs = "\n".join(rank_msgs)
|
|
||||||
report_msg = f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msgs}"
|
|
||||||
reply = msg.reply_with(report_msg)
|
|
||||||
client.info(report_msg)
|
|
||||||
client.send_message(reply)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
help = """/bmcl -> dashboard
|
|
||||||
/bmcl rank -> all rank
|
|
||||||
/bmcl rank <name> -> rank of <name>"""
|
|
||||||
|
|
||||||
|
|
||||||
def on_message(msg: NewMessage, client: IcaClient) -> None:
|
def on_message(msg: NewMessage, client: IcaClient) -> None:
|
||||||
if not (msg.is_from_self or msg.is_reply):
|
if not (msg.is_from_self or msg.is_reply):
|
||||||
if msg.content.startswith("/bmcl"):
|
if msg.content == "/bmcl":
|
||||||
if msg.content == "/bmcl":
|
bmcl(msg, client)
|
||||||
bmcl_dashboard(msg, client)
|
|
||||||
elif msg.content == "/bmcl rank":
|
|
||||||
bmcl_rank(msg, client, None)
|
|
||||||
elif msg.content.startswith("/bmcl rank") and len(msg.content) > 11:
|
|
||||||
name = msg.content[11:]
|
|
||||||
bmcl_rank(msg, client, name)
|
|
||||||
else:
|
|
||||||
reply = msg.reply_with(help)
|
|
||||||
client.send_message(reply)
|
|
||||||
|
|
|
@ -19,8 +19,6 @@ pub struct IcaConfig {
|
||||||
pub notice_start: bool,
|
pub notice_start: bool,
|
||||||
/// 管理员列表
|
/// 管理员列表
|
||||||
pub admin_list: Vec<i64>,
|
pub admin_list: Vec<i64>,
|
||||||
/// 过滤列表
|
|
||||||
pub filter_list: Vec<i64>,
|
|
||||||
/// Python 插件路径
|
/// Python 插件路径
|
||||||
pub py_plugin_path: Option<String>,
|
pub py_plugin_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ use crate::client::IcalinguaStatus;
|
||||||
use crate::data_struct::files::MessageFile;
|
use crate::data_struct::files::MessageFile;
|
||||||
use crate::data_struct::{MessageId, RoomId, UserId};
|
use crate::data_struct::{MessageId, RoomId, UserId};
|
||||||
|
|
||||||
use tracing::warn;
|
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value as JsonValue};
|
use serde_json::{json, Value as JsonValue};
|
||||||
|
@ -139,15 +138,7 @@ impl NewMessage {
|
||||||
}
|
}
|
||||||
// 回复的消息
|
// 回复的消息
|
||||||
let reply: Option<ReplyMessage> = match message.get("replyMessage") {
|
let reply: Option<ReplyMessage> = match message.get("replyMessage") {
|
||||||
Some(value) => {
|
Some(value) => serde_json::from_value::<ReplyMessage>(value.clone()).ok(),
|
||||||
match serde_json::from_value::<ReplyMessage>(value.clone()) {
|
|
||||||
Ok(reply) => Some(reply),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Failed to parse reply message: {}", e);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
// At
|
// At
|
||||||
|
@ -205,8 +196,7 @@ impl NewMessage {
|
||||||
|
|
||||||
pub fn output(&self) -> String {
|
pub fn output(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
// >10 >10 >15
|
"Room: {}, Sender: {}|{}, Content: {}",
|
||||||
"{:>10}|{:>12}|{:<20}|{}",
|
|
||||||
self.room_id, self.sender_id, self.sender_name, self.content
|
self.room_id, self.sender_id, self.sender_name, self.content
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rust_socketio::asynchronous::Client;
|
||||||
use rust_socketio::{Event, Payload};
|
use rust_socketio::{Event, Payload};
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::client::{send_message, IcalinguaStatus};
|
use crate::client::send_message;
|
||||||
use crate::data_struct::all_rooms::Room;
|
use crate::data_struct::all_rooms::Room;
|
||||||
use crate::data_struct::messages::NewMessage;
|
use crate::data_struct::messages::NewMessage;
|
||||||
use crate::data_struct::online_data::OnlineData;
|
use crate::data_struct::online_data::OnlineData;
|
||||||
|
@ -16,7 +16,7 @@ pub async fn get_online_data(payload: Payload, _client: Client) {
|
||||||
let online_data = OnlineData::new_from_json(value);
|
let online_data = OnlineData::new_from_json(value);
|
||||||
info!(
|
info!(
|
||||||
"update_online_data {}",
|
"update_online_data {}",
|
||||||
format!("{:?}", online_data).cyan()
|
format!("{:#?}", online_data).cyan()
|
||||||
);
|
);
|
||||||
unsafe {
|
unsafe {
|
||||||
crate::ClientStatus.update_online_data(online_data);
|
crate::ClientStatus.update_online_data(online_data);
|
||||||
|
@ -30,12 +30,7 @@ pub async fn add_message(payload: Payload, client: Client) {
|
||||||
if let Payload::Text(values) = payload {
|
if let Payload::Text(values) = payload {
|
||||||
if let Some(value) = values.first() {
|
if let Some(value) = values.first() {
|
||||||
let message = NewMessage::new_from_json(value);
|
let message = NewMessage::new_from_json(value);
|
||||||
// 检测是否在过滤列表内
|
|
||||||
if IcalinguaStatus::get_config().filter_list.contains(&message.sender_id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
info!("add_message {}", message.output().cyan());
|
info!("add_message {}", message.output().cyan());
|
||||||
// info!("add_message {}", format!("{:#?}", message).cyan());
|
|
||||||
// 就在这里处理掉最基本的消息
|
// 就在这里处理掉最基本的消息
|
||||||
// 之后的处理交给插件
|
// 之后的处理交给插件
|
||||||
if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {
|
if message.content.eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {
|
||||||
|
@ -54,7 +49,7 @@ pub async fn delete_message(payload: Payload, _client: Client) {
|
||||||
// 消息 id
|
// 消息 id
|
||||||
if let Some(value) = values.first() {
|
if let Some(value) = values.first() {
|
||||||
if let Some(msg_id) = value.as_str() {
|
if let Some(msg_id) = value.as_str() {
|
||||||
info!("delete_message {}", format!("{}", msg_id).yellow());
|
warn!("delete_message {}", format!("{}", msg_id).yellow());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,7 +88,6 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
||||||
"notify",
|
"notify",
|
||||||
"closeLoading", // 发送消息/加载新聊天 有一个 loading
|
"closeLoading", // 发送消息/加载新聊天 有一个 loading
|
||||||
"updateRoom",
|
"updateRoom",
|
||||||
"syncRead",
|
|
||||||
];
|
];
|
||||||
match &event {
|
match &event {
|
||||||
Event::Custom(event_name) => {
|
Event::Custom(event_name) => {
|
||||||
|
|
|
@ -38,7 +38,6 @@ async fn main() {
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_max_level(tracing::Level::DEBUG)
|
.with_max_level(tracing::Level::DEBUG)
|
||||||
.init();
|
.init();
|
||||||
info!("ica-async-rs v{}", VERSION);
|
|
||||||
|
|
||||||
// 从命令行获取 host 和 key
|
// 从命令行获取 host 和 key
|
||||||
// 从命令行获取配置文件路径
|
// 从命令行获取配置文件路径
|
||||||
|
|
|
@ -82,12 +82,30 @@ pub fn load_py_plugins(path: &PathBuf) {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if let Some(ext) = path.extension() {
|
if let Some(ext) = path.extension() {
|
||||||
if ext == "py" {
|
if ext == "py" {
|
||||||
match load_py_module(&path) {
|
match load_py_file(&path) {
|
||||||
Some((changed_time, py_module)) => {
|
Ok((changed_time, content)) => {
|
||||||
PyStatus::add_file(path.clone(), changed_time, py_module);
|
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
||||||
|
let module: PyResult<Py<PyAny>> = PyModule::from_code(
|
||||||
|
py,
|
||||||
|
&content,
|
||||||
|
&path.to_string_lossy(),
|
||||||
|
&path.to_string_lossy()
|
||||||
|
)
|
||||||
|
.map(|module| module.into());
|
||||||
|
module
|
||||||
|
});
|
||||||
|
match py_module {
|
||||||
|
Ok(py_module) => {
|
||||||
|
info!("加载到插件: {:?}", path);
|
||||||
|
PyStatus::add_file(path, changed_time, py_module);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
Err(e) => {
|
||||||
warn!("加载 Python 插件: {:?} 失败", path);
|
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,12 +145,24 @@ pub fn verify_plugins() {
|
||||||
}
|
}
|
||||||
info!("file change list: {:?}", need_reload_files);
|
info!("file change list: {:?}", need_reload_files);
|
||||||
for reload_file in need_reload_files {
|
for reload_file in need_reload_files {
|
||||||
match load_py_module(&reload_file) {
|
match load_py_file(&reload_file) {
|
||||||
Some((changed_time, py_module)) => {
|
Ok((changed_time, content)) => {
|
||||||
|
let py_module = Python::with_gil(|py| -> Py<PyAny> {
|
||||||
|
let module: Py<PyAny> = PyModule::from_code(
|
||||||
|
py,
|
||||||
|
&content,
|
||||||
|
&reload_file.to_string_lossy(),
|
||||||
|
&reload_file.to_string_lossy(),
|
||||||
|
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.into();
|
||||||
|
module
|
||||||
|
});
|
||||||
PyStatus::add_file(reload_file.clone(), changed_time, py_module);
|
PyStatus::add_file(reload_file.clone(), changed_time, py_module);
|
||||||
}
|
},
|
||||||
None => {
|
Err(e) => {
|
||||||
warn!("重载 Python 插件: {:?} 失败", reload_file);
|
warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,35 +180,6 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<(Option<SystemTime>, Stri
|
||||||
Ok((changed_time, content))
|
Ok((changed_time, content))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_py_module(path: &PathBuf) -> Option<(Option<SystemTime>, Py<PyAny>)> {
|
|
||||||
let (changed_time, content) = match load_py_file(&path) {
|
|
||||||
Ok((changed_time, content)) => (changed_time, content),
|
|
||||||
Err(e) => {
|
|
||||||
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
|
||||||
let module: PyResult<Py<PyAny>> = PyModule::from_code(
|
|
||||||
py,
|
|
||||||
&content,
|
|
||||||
&path.to_string_lossy(),
|
|
||||||
&path.to_string_lossy(),
|
|
||||||
// !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的
|
|
||||||
).map(|module| module.into());
|
|
||||||
module
|
|
||||||
});
|
|
||||||
match py_module {
|
|
||||||
Ok(py_module) => {
|
|
||||||
Some((changed_time, py_module))
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_py(config: &IcaConfig) {
|
pub fn init_py(config: &IcaConfig) {
|
||||||
debug!("initing python threads");
|
debug!("initing python threads");
|
||||||
pyo3::prepare_freethreaded_python();
|
pyo3::prepare_freethreaded_python();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user