Skip to content

Commit 5594f9e

Browse files
authored
refactor(core): 主操作类获取方式改为属性,新增计划任务管理 (#18)
* refactor(core): 主操作类获取方式改为属性,新增计划任务管理 - 为MCSMAPI类添加@Property装饰器,使overview、instance、user、daemon、file、image等方法可以作为属性访问, 简化了调用方式。同时删除了多余的括号调用,在examples中更新了所有相关调用方式。 - resolve [Feature Request]: 计划任务管理 Fixes #17 BREAKING CHANGE: 原来的mcsm.daemon()、mcsm.instance()等调用方式需要改为mcsm.daemon、mcsm.instance * feat(schedule): 更新计划任务API接口参数 为计划任务的创建和更新方法添加daemonId和uuid参数, 使接口调用更加明确和一致。 fix(daemon): 改进节点系统信息模型定义 将DaemonSystemInfo模型中的字段改为可选类型,改进文档字符串描述, 使节点信息处理更加灵活。 refactor(instance): 优化实例信息获取逻辑 重构get_all_instance_info函数,使用字典推导式简化代码, 移除不必要的断言和文件写入操作。 feat(instance): 添加实例计划任务创建功能 在InstanceDetail类中添加create_schedule方法, 方便通过实例对象直接创建计划任务。 refactor(overview): 优化节点可用性检查逻辑 使用海象运算符简化节点可用性检查,添加类型忽略注释, 提高代码可读性。 * refactor(core): 移除不必要的海象操作符并优化代码结构 移除example/overview.py中的海象操作符(Available assignment operator), 使代码更加清晰易读。 优化mcsmapi/apis/schedule.py中的Schedule.create方法调用, 直接使用类方法替代复杂的send函数调用,提高代码可维护性。
1 parent 0262a87 commit 5594f9e

File tree

18 files changed

+250
-62
lines changed

18 files changed

+250
-62
lines changed

example/daemon.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
from mcsmapi import MCSMAPI
2+
from mcsmapi.models.daemon import DaemonConfig
23

34
mcsm = MCSMAPI("http://localhost:23333")
45

56
mcsm.login("admin", "547cABC9bf88@")
67

78
# mcsm.login_with_apikey("apikey")
89

9-
daemon_object = mcsm.daemon()
10+
daemon_object = mcsm.daemon
1011

1112
# show Daemon list
1213

1314
print(daemon_object.config())
1415

1516
# 创建节点
1617
daemonId = daemon_object.add(
17-
{
18-
"ip": "localhost",
19-
"port": 24444,
20-
"prefix": "",
21-
"remarks": "Unnamed Node",
22-
"available": True,
23-
}
18+
DaemonConfig(
19+
ip="localhost",
20+
port=24444,
21+
prefix="",
22+
remarks="Unnamed Node",
23+
)
2424
)
2525
# 删除节点
2626
daemon_object.delete(daemonId)

example/instance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
api.login("admin", "547cABC9bf88@")
66

7-
instance_object = api.instance()
7+
instance_object = api.instance
88

99
instance_list = instance_object.search("xxx")
1010

@@ -37,7 +37,7 @@
3737
f.rename("new_name")
3838

3939
# copy file
40-
f.copy("new_path")
40+
f.copy_to("new_path")
4141
# move file
4242
f.move("new_path")
4343

example/overview.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# mcsm.login_with_apikey("apikey")
88

99
# Get dashboard data
10-
overview = mcsm.overview()
10+
overview = mcsm.overview
1111
overview_data = overview.overview()
1212

1313
mcsm_version = overview_data.version
@@ -19,12 +19,14 @@
1919
print(remote.ip)
2020
print(remote.port)
2121
print(remote.prefix)
22-
print(remote.available)
23-
print(remote.version)
24-
print(remote.process.cpu)
25-
print(remote.system.freemem)
26-
print(remote.system.hostname)
27-
print(remote.system.loadavg)
22+
if remote.available:
23+
print(remote.version)
24+
print(remote.process.cpu) # type: ignore
25+
print(remote.system.freemem) # type: ignore
26+
print(remote.system.hostname) # type: ignore
27+
print(remote.system.loadavg) # type: ignore
28+
else:
29+
print("Not available")
2830

2931

3032
remote = remotes[0]

example/use_case/get_all_instance_info.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
# Get all instance info
1818
def get_all_instance_info(daemon_id: str):
1919
# Get all instance info from the daemon
20-
instance_object = mcsm.instance()
20+
instance_object = mcsm.instance
2121
instance_list = instance_object.search(daemonId=daemon_id).data
22-
23-
# Create a dictionary to store instance info
24-
instance_dict = {}
25-
for instance in instance_list:
26-
instance_dict[instance.instanceUuid] = {
22+
23+
return {
24+
instance.instanceUuid: {
2725
"name": instance.config.nickname,
2826
"status": instance.status,
2927
"daemonId": instance.daemonId,
3028
"uuid": instance.instanceUuid,
3129
}
32-
return instance_dict
30+
for instance in instance_list
31+
}
3332

3433
# Example usage
3534
daemon_id = "your_daemon_id" # Please change to your MCSM panel daemon id.
@@ -41,4 +40,3 @@ def get_all_instance_info(daemon_id: str):
4140
# Optional: Save info to a file in the current directory
4241
with open("instance_info.json", "w") as f:
4342
content = json.dump(instance_info, f, indent=4)
44-
f.write(content)

example/use_case/search_instance_by_name.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from mcsmapi import MCSMAPI
2-
import json
32

43
baseurl = "http://localhost:23333" # Change to your MCSM panel address and port(include http or https etc.).
54

@@ -17,7 +16,7 @@
1716
# Get instance
1817
def get_instance(daemon_id: str, instance_name: str):
1918
# Get specific instance info from the daemon by name
20-
instance_object = mcsm.instance()
19+
instance_object = mcsm.instance
2120
instance_list = instance_object.search(daemonId=daemon_id, instance_name=instance_name).data
2221

2322
# Error handling
@@ -41,17 +40,16 @@ def get_instance(daemon_id: str, instance_name: str):
4140
# Print instance status
4241
print(instance.config.nickname)
4342
print(instance.status)
44-
print(instance.space)
4543

4644
# Optional:
4745
# start
48-
#instance.start()
46+
instance.start()
4947

5048
# stop
51-
#instance.stop()
49+
instance.stop()
5250

5351
# restart
54-
#instance.restart()
52+
instance.restart()
5553

5654
# kill
57-
#instance.kill()
55+
instance.kill()

example/user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
api.login("admin", "547cABC9bf88@")
66

7-
userManager = api.user()
7+
userManager = api.user
88

99
users = userManager.search()
1010

@@ -13,7 +13,7 @@
1313
print(user.userName)
1414

1515
# 查找第一个管理员账号
16-
user = api.user().search(role=10).data[0]
16+
user = userManager.search(role=10).data[0]
1717

1818
# 封禁管理员账号
1919
user.update({"permission": -1})

mcsmapi/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,26 @@ def login_with_apikey(self, apikey: str):
3434
self.authentication = "apikey"
3535
return self
3636

37+
@property
3738
def overview(self):
3839
return Overview()
3940

41+
@property
4042
def instance(self):
4143
return Instance()
4244

45+
@property
4346
def user(self) :
4447
return User()
4548

49+
@property
4650
def daemon(self):
4751
return Daemon()
4852

53+
@property
4954
def file(self):
5055
return File()
5156

57+
@property
5258
def image(self):
5359
return Image()

mcsmapi/apis/daemon.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ def system() -> list[DaemonSystemInfo]:
5151
return [DaemonSystemInfo(**daemon) for daemon in daemons]
5252

5353
@staticmethod
54-
def add(config: dict[str, Any]) -> str:
54+
def add(config: DaemonConfig) -> str:
5555
"""
5656
新增一个节点
5757
58-
:params config: 节点的配置信息,以字典形式提供,缺失内容由DaemonConfig模型补全
58+
:params config: 节点的配置信息
5959
6060
:returns: 新增节点的UUID
6161
"""
6262
return send(
6363
"POST",
6464
f"{ApiPool.SERVICE}/remote_service",
65-
data=DaemonConfig(**config).model_dump(),
65+
data=config.model_dump(),
6666
)
6767

6868
@staticmethod

mcsmapi/apis/file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async def upload(daemonId: str, uuid: str, file: bytes, upload_dir: str) -> bool
126126
return True
127127

128128
@staticmethod
129-
def copy(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
129+
def copy_to(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:
130130
"""
131131
复制多个文件夹或文件到指定位置
132132
@@ -156,7 +156,7 @@ def copyOne(daemonId: str, uuid: str, source: str, target: str) -> bool:
156156
157157
:returns: 操作成功后返回True
158158
"""
159-
return File.copy(daemonId, uuid, {source: target})
159+
return File.copy_to(daemonId, uuid, {source: target})
160160

161161
@staticmethod
162162
def move(daemonId: str, uuid: str, copy_map: dict[str, str]) -> bool:

mcsmapi/apis/schedule.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from mcsmapi.pool import ApiPool
2+
from mcsmapi.request import send
3+
from mcsmapi.models.schedule import ScheduleDetail, SchedulePostBody
4+
5+
6+
class Schedule:
7+
8+
@staticmethod
9+
def list(daemonId: str, uuid: str) -> list[ScheduleDetail]:
10+
"""
11+
获取实例计划任务列表
12+
13+
:param daemonId: 节点ID
14+
:param uuid: 实例ID
15+
16+
:returns: 计划任务列表
17+
"""
18+
result = send(
19+
"GET",
20+
f"{ApiPool.SCHEDULE}",
21+
params={"daemonId": daemonId, "uuid": uuid},
22+
)
23+
return [ScheduleDetail(**r, daemonId=daemonId) for r in result]
24+
25+
@staticmethod
26+
def delete(daemonId: str, uuid: str, task_name: str) -> bool:
27+
"""
28+
删除计划任务
29+
30+
:param daemonId: 节点ID
31+
:param uuid: 实例ID
32+
:param task_name: 计划任务名称
33+
34+
:returns: 是否成功
35+
"""
36+
return send(
37+
"DELETE",
38+
f"{ApiPool.SCHEDULE}",
39+
params={"daemonId": daemonId, "uuid": uuid, "task_name": task_name},
40+
)
41+
42+
@staticmethod
43+
def create(daemonId: str, uuid: str, config: SchedulePostBody) -> bool:
44+
"""
45+
创建计划任务
46+
47+
:param daemonId: 节点ID
48+
:param uuid: 实例ID
49+
:param config: 计划任务配置
50+
51+
:returns: 是否成功
52+
"""
53+
return send(
54+
"POST",
55+
f"{ApiPool.SCHEDULE}",
56+
params={"daemonId": daemonId, "uuid": uuid},
57+
data=config.model_dump(),
58+
)
59+
60+
@staticmethod
61+
def update(daemonId: str, uuid: str, config: SchedulePostBody) -> bool:
62+
"""
63+
更新计划任务
64+
65+
:param daemonId: 节点ID
66+
:param uuid: 实例ID
67+
:param config: 计划任务配置
68+
69+
:returns: 是否成功
70+
"""
71+
return Schedule.create(daemonId, uuid, config)

0 commit comments

Comments
 (0)