Skip to content

Commit c8160e1

Browse files
committed
update: update SDK and CLI
1 parent 31bb109 commit c8160e1

File tree

6 files changed

+64
-37
lines changed

6 files changed

+64
-37
lines changed

netpulse-client/netpulse_client/__init__.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
提供同步和异步的网络设备操作接口, 支持命令执行和配置推送。
55
6-
核心方法 (与API端点对应) :
6+
核心方法 (与API端点对应) :
77
- exec_command(): 同步执行命令 -> /device/execute
88
- exec_config(): 同步推送配置 -> /device/execute
99
- bulk_command(): 同步批量执行命令 -> /device/bulk
@@ -53,32 +53,37 @@
5353
create_device_request,
5454
)
5555

56-
__version__ = "0.1.0"
56+
from importlib import metadata
57+
58+
try:
59+
__version__ = metadata.version("netpulse-client")
60+
except metadata.PackageNotFoundError:
61+
__version__ = "0.1.1-dev"
5762

5863
__all__ = [
59-
# 客户端类
60-
"NetPulseClient",
61-
"AsyncNetPulseClient",
6264
"AsyncJobHandle",
65+
"AsyncNetPulseClient",
66+
"AuthenticationError",
67+
"BatchResult",
68+
"CommandResult",
69+
"ConfigResult",
6370
# 数据模型
6471
"ConnectionArgs", # 主要的连接参数模型
72+
"ConnectionError",
73+
"ConnectionTestResult",
6574
"Device", # 向后兼容的Device别名
66-
"CommandResult",
67-
"ConfigResult",
68-
"BatchResult",
69-
"JobInfo",
70-
"WorkerInfo",
7175
"HealthCheckResult",
72-
"ConnectionTestResult",
73-
# 工具函数
74-
"create_device_request",
75-
"create_batch_device_request",
76+
"JobError",
77+
"JobInfo",
78+
# 客户端类
79+
"NetPulseClient",
7680
# 异常类
7781
"NetPulseError",
78-
"AuthenticationError",
79-
"ConnectionError",
80-
"JobError",
82+
"SDKValidationError",
8183
"TimeoutError",
8284
"ValidationError",
83-
"SDKValidationError",
85+
"WorkerInfo",
86+
"create_batch_device_request",
87+
# 工具函数
88+
"create_device_request",
8489
]

netpulse-client/netpulse_client/async_client.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ async def execute(
185185
186186
Args:
187187
device: 目标设备
188-
command: 要执行的命令 (Pull操作)
189-
config: 要推送的配置 (Push操作)
188+
command: 要执行的命令 (Pull操作)
189+
config: 要推送的配置 (Push操作)
190190
parse_with: 解析器类型
191191
save: 是否保存配置
192192
dry_run: 是否为干运行模式
@@ -229,9 +229,15 @@ async def execute(
229229
}
230230

231231
if parse_with:
232+
cmd_or_cfg = command or config
233+
template_name = (
234+
cmd_or_cfg.replace(" ", "_")
235+
if isinstance(cmd_or_cfg, str)
236+
else cmd_or_cfg[0].replace(" ", "_")
237+
)
232238
options["parsing"] = {
233239
"name": parse_with,
234-
"template": f"file:///templates/{(command or config).replace(' ', '_') if isinstance((command or config), str) else (command or config)[0].replace(' ', '_')}.{parse_with}",
240+
"template": f"file:///templates/{template_name}.{parse_with}",
235241
}
236242

237243
# 创建请求
@@ -317,8 +323,8 @@ async def bulk(
317323
318324
Args:
319325
devices: 设备列表
320-
command: 要执行的命令 (Pull操作)
321-
config: 要推送的配置 (Push操作)
326+
command: 要执行的命令 (Pull操作)
327+
config: 要推送的配置 (Push操作)
322328
parse_with: 解析器类型
323329
save: 是否保存配置
324330
dry_run: 是否为干运行模式
@@ -373,9 +379,15 @@ async def bulk(
373379
}
374380

375381
if parse_with:
382+
cmd_or_cfg = command or config
383+
template_name = (
384+
cmd_or_cfg.replace(" ", "_")
385+
if isinstance(cmd_or_cfg, str)
386+
else cmd_or_cfg[0].replace(" ", "_")
387+
)
376388
options["parsing"] = {
377389
"name": parse_with,
378-
"template": f"file:///templates/{(command or config).replace(' ', '_') if isinstance((command or config), str) else (command or config)[0].replace(' ', '_')}.{parse_with}",
390+
"template": f"file:///templates/{template_name}.{parse_with}",
379391
}
380392

381393
# 创建请求
@@ -722,14 +734,16 @@ async def _wait_for_result_with_progressive_retry(
722734
"""
723735
异步激进递进式重试等待任务结果
724736
725-
轮询策略:
737+
轮询策略:
726738
- 初始延迟: 0.4秒
727-
- 递增步长序列: 0.1s -> 0.2s -> 0.3s -> 0.5s -> 1.5s -> 2.5s -> 4.0s -> 6.0s -> 9.0s -> 13.5s -> 20.0s -> 30.0s
728-
- 轮询间隔: 0.4s -> 0.5s -> 0.7s -> 1.0s -> 1.5s -> 3.0s -> 5.5s -> 9.5s -> 15.5s -> 24.5s -> 37.5s -> 57.5s
739+
- 递增步长序列: 0.1s -> 0.2s -> 0.3s -> 0.5s -> 1.5s -> 2.5s -> 4.0s
740+
-> 6.0s -> 9.0s -> 13.5s -> 20.0s -> 30.0s
741+
- 轮询间隔: 0.4s -> 0.5s -> 0.7s -> 1.0s -> 1.5s -> 3.0s -> 5.5s
742+
-> 9.5s -> 15.5s -> 24.5s -> 37.5s -> 57.5s
729743
- 最大间隔: 30秒
730744
- 最大总时长: 120秒
731745
732-
示例轮询序列:
746+
示例轮询序列:
733747
第1次: 0.4s (初始)
734748
第2次: 0.5s (0.4s + 0.1s)
735749
第3次: 0.7s (0.5s + 0.2s)
@@ -766,16 +780,19 @@ async def _wait_for_result_with_progressive_retry(
766780

767781
if status in ["finished", "failed"]:
768782
logger.info(
769-
f"任务 {job_id} 完成, 总耗时: {total_elapsed:.2f}秒, 轮询次数: {attempt}"
783+
f"任务 {job_id} 完成, 总耗时: {total_elapsed:.2f}秒, "
784+
f"轮询次数: {attempt}"
770785
)
771786
return result
772787
elif status == "queued":
773788
logger.info(
774-
f"任务 {job_id} 排队中... (第{attempt}次轮询, 已耗时{total_elapsed:.2f}秒)"
789+
f"任务 {job_id} 排队中... "
790+
f"(第{attempt}次轮询, 已耗时{total_elapsed:.2f}秒)"
775791
)
776792
elif status == "started":
777793
logger.info(
778-
f"任务 {job_id} 执行中... (第{attempt}次轮询, 已耗时{total_elapsed:.2f}秒)"
794+
f"任务 {job_id} 执行中... "
795+
f"(第{attempt}次轮询, 已耗时{total_elapsed:.2f}秒)"
779796
)
780797

781798
# 激进递进式延迟
@@ -796,7 +813,8 @@ async def _wait_for_result_with_progressive_retry(
796813
# 记录轮询信息
797814
if attempt % 3 == 0: # 每3次轮询记录一次详细信息
798815
logger.info(
799-
f"任务 {job_id} 轮询中... 第{attempt}次, 当前延迟: {delay:.2f}s, 总耗时: {total_elapsed:.2f}s"
816+
f"任务 {job_id} 轮询中... 第{attempt}次, "
817+
f"当前延迟: {delay:.2f}s, 总耗时: {total_elapsed:.2f}s"
800818
)
801819

802820
except Exception as e:

netpulse-client/netpulse_client/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ def _wait_for_result_with_progressive_retry(
9696
9797
轮询策略:
9898
- 初始延迟: 0.4秒
99-
- 递增步长序列: 0.1s -> 0.2s -> 0.3s -> 0.5s -> 1.5s -> 2.5s -> 4.0s -> 6.0s -> 9.0s -> 13.5s -> 20.0s -> 30.0s
100-
- 轮询间隔: 0.4s -> 0.5s -> 0.7s -> 1.0s -> 1.5s -> 3.0s -> 5.5s -> 9.5s -> 15.5s -> 24.5s -> 37.5s -> 57.5s
99+
- 递增步长序列: 0.1s -> 0.2s -> 0.3s -> 0.5s -> 1.5s -> 2.5s -> 4.0s
100+
-> 6.0s -> 9.0s -> 13.5s -> 20.0s -> 30.0s
101+
- 轮询间隔: 0.4s -> 0.5s -> 0.7s -> 1.0s -> 1.5s -> 3.0s -> 5.5s
102+
-> 9.5s -> 15.5s -> 24.5s -> 37.5s -> 57.5s
101103
- 最大间隔: 30秒
102104
- 最大总时长: 120秒
103105

netpulse-client/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "netpulse-client"
7-
version = "0.1.0"
7+
version = "0.1.1"
88
description = "Professional Python SDK for NetPulse network automation platform"
99
authors = [
1010
{ name = "NetPulse Team", email = "[email protected]" },
@@ -106,6 +106,8 @@ strict_equality = true
106106
[tool.ruff]
107107
line-length = 100
108108
target-version = "py38"
109+
110+
[tool.ruff.lint]
109111
select = ["E", "F", "W", "RUF", "I"]
110112

111113
[tool.pytest.ini_options]

netpulse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
try:
44
__version__ = metadata.version("netpulse")
55
except metadata.PackageNotFoundError:
6-
__version__ = "0.1.0-dev"
6+
__version__ = "0.1.1-dev"

netpulse/cli/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def main():
862862
signal.signal(signal.SIGINT, lambda sig, frame: sys.exit(0))
863863
_ = CliApp.run(RootSettings)
864864
except Exception as e:
865-
print(f"Uncatched exception: {e}")
865+
logging.error(f"Uncaught exception: {e}", exc_info=True)
866866
sys.exit(1)
867867

868868

0 commit comments

Comments
 (0)