sdev 0.4.3__tar.gz → 0.4.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.4.3
3
+ Version: 0.4.4
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -62,39 +62,39 @@ sdev --no-check-alive shell "echo hello"
62
62
 
63
63
  ## 快速使用 (Python)
64
64
 
65
- 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
65
+ 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口。`Demoboard` 在实例化时默认会自动执行 `check_alive()`。
66
66
 
67
67
  ```python
68
68
  from sdev import sdev
69
69
 
70
70
  # 使用 context manager 自动连接/断开
71
71
  with sdev("/dev/ttyUSB0", 115200) as board:
72
- # connect 时默认会执行 check_alive(),确保板子已就绪
72
+ # 实例化时默认已执行 check_alive(),确保板子已就绪
73
73
  out = board.shell("ls") # 执行命令并返回输出行列表
74
74
  print(out)
75
75
  ```
76
76
 
77
- 手动控制连接与存活检测:
77
+ 使用旧版兼容接口:
78
78
 
79
79
  ```python
80
80
  from sdev import Demoboard
81
81
 
82
- board = Demoboard("/dev/ttyUSB0", 115200)
83
- board.connect()
84
- board.check_alive() # 检查并确保进入 shell 提示符
85
- board.shell("echo hello")
86
- board.disconnect()
82
+ with Demoboard("/dev/ttyUSB0", 115200) as board:
83
+ # execute_command 是 shell 的别名,方便旧代码迁移
84
+ out = board.execute_command("ls", flag=" #")
85
+ print(out)
87
86
  ```
88
87
 
89
88
  ## 主要接口
90
89
 
91
90
  ### Demoboard (推荐)
92
91
 
93
- 继承自 `SerialDevice`,提供针对开发板的高层封装。
92
+ 继承自 `SerialDevice`,提供针对开发板的高层封装。实例化时会自动调用 `check_alive()`。
94
93
 
95
94
  | 方法 | 说明 |
96
95
  |------|------|
97
96
  | `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
97
+ | `execute_command(cmd, flag, timeout=None, stream=False)` | `shell` 方法的别名,用于兼容旧版代码。 |
98
98
  | `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
99
99
 
100
100
  ### SerialDevice (底层)
@@ -28,39 +28,39 @@ sdev --no-check-alive shell "echo hello"
28
28
 
29
29
  ## 快速使用 (Python)
30
30
 
31
- 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
31
+ 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口。`Demoboard` 在实例化时默认会自动执行 `check_alive()`。
32
32
 
33
33
  ```python
34
34
  from sdev import sdev
35
35
 
36
36
  # 使用 context manager 自动连接/断开
37
37
  with sdev("/dev/ttyUSB0", 115200) as board:
38
- # connect 时默认会执行 check_alive(),确保板子已就绪
38
+ # 实例化时默认已执行 check_alive(),确保板子已就绪
39
39
  out = board.shell("ls") # 执行命令并返回输出行列表
40
40
  print(out)
41
41
  ```
42
42
 
43
- 手动控制连接与存活检测:
43
+ 使用旧版兼容接口:
44
44
 
45
45
  ```python
46
46
  from sdev import Demoboard
47
47
 
48
- board = Demoboard("/dev/ttyUSB0", 115200)
49
- board.connect()
50
- board.check_alive() # 检查并确保进入 shell 提示符
51
- board.shell("echo hello")
52
- board.disconnect()
48
+ with Demoboard("/dev/ttyUSB0", 115200) as board:
49
+ # execute_command 是 shell 的别名,方便旧代码迁移
50
+ out = board.execute_command("ls", flag=" #")
51
+ print(out)
53
52
  ```
54
53
 
55
54
  ## 主要接口
56
55
 
57
56
  ### Demoboard (推荐)
58
57
 
59
- 继承自 `SerialDevice`,提供针对开发板的高层封装。
58
+ 继承自 `SerialDevice`,提供针对开发板的高层封装。实例化时会自动调用 `check_alive()`。
60
59
 
61
60
  | 方法 | 说明 |
62
61
  |------|------|
63
62
  | `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
63
+ | `execute_command(cmd, flag, timeout=None, stream=False)` | `shell` 方法的别名,用于兼容旧版代码。 |
64
64
  | `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
65
65
 
66
66
  ### SerialDevice (底层)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sdev"
7
- version = "0.4.3"
7
+ version = "0.4.4"
8
8
  description = "串口控制器工具包"
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -14,7 +14,7 @@ from typing import Literal
14
14
  from .core import SerialDevice
15
15
  from .class_wrapper import Demoboard, Relay
16
16
 
17
- __version__ = "0.4.3"
17
+ __version__ = "0.4.4"
18
18
  __author__ = "klrc"
19
19
  __email__ = "1440698245@qq.com"
20
20
 
@@ -24,6 +24,7 @@ def sdev(
24
24
  baudrate: int = 115200,
25
25
  *,
26
26
  device_type: Literal["demoboard", "relay"] = "demoboard",
27
+ **kwargs,
27
28
  ):
28
29
  """
29
30
  构造一个设备实例。
@@ -31,9 +32,9 @@ def sdev(
31
32
  默认返回 Demoboard,后续可根据需要扩展其它类型。
32
33
  """
33
34
  if device_type == "demoboard":
34
- return Demoboard(port, baudrate)
35
+ return Demoboard(port, baudrate, **kwargs)
35
36
  if device_type == "relay":
36
- return Relay(port, baudrate)
37
+ return Relay(port, baudrate, **kwargs)
37
38
  raise ValueError(f"unsupported device_type: {device_type!r}")
38
39
 
39
40
 
@@ -21,11 +21,25 @@ class Demoboard(SerialDevice):
21
21
  - 提供 shell() / check_alive() 等高层方法。
22
22
  """
23
23
 
24
- def __init__(self, *args, **kwargs):
25
- super().__init__(*args, **kwargs)
26
- self.check_alive()
27
-
28
- def execute_command(self, cmd, flag, timeout=None, stream=False):
24
+ def __init__(self, port: str, baudrate: int = 115200, check_alive: bool = True):
25
+ super().__init__(port, baudrate)
26
+ self._auto_check_alive = check_alive
27
+
28
+ def connect(self) -> None:
29
+ """连接并自动执行 check_alive。"""
30
+ if self._is_connected:
31
+ return
32
+ super().connect()
33
+ if self._auto_check_alive:
34
+ self.check_alive()
35
+
36
+ def execute_command(
37
+ self,
38
+ cmd: str,
39
+ flag: str = " #",
40
+ timeout: Optional[float] = None,
41
+ stream: bool = False,
42
+ ) -> Union[List[str], Generator[str, None, None]]:
29
43
  """alias for shell"""
30
44
  return self.shell(cmd, prompt_flag=flag, timeout=timeout, stream=stream)
31
45
 
@@ -61,8 +75,10 @@ class Relay(SerialDevice):
61
75
  - 具体继电器控制逻辑后续在 sdev.relay.functional 中补充。
62
76
  """
63
77
 
64
- # 先占位,避免 API 变动;后续按需要扩展方法。
65
- pass
78
+ def __init__(self, port: str, baudrate: int = 115200, **kwargs):
79
+ # 吞掉不支持的参数,确保与 sdev() 工厂兼容
80
+ kwargs.pop("check_alive", None)
81
+ super().__init__(port, baudrate)
66
82
 
67
83
 
68
84
  __all__ = ["Demoboard", "Relay"]
@@ -27,10 +27,8 @@ def _get_default_baudrate() -> int:
27
27
  def cmd_shell(args: argparse.Namespace) -> int:
28
28
  port = args.port or _get_default_port()
29
29
  baudrate = args.baudrate or _get_default_baudrate()
30
- with Demoboard(port, baudrate) as board:
30
+ with Demoboard(port, baudrate, check_alive=not args.no_check_alive) as board:
31
31
  try:
32
- if not args.no_check_alive:
33
- board.check_alive()
34
32
  board.shell(args.command, prompt_flag=args.flag, timeout=args.timeout)
35
33
  except TimeoutError as e:
36
34
  print(e, file=sys.stderr)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sdev
3
- Version: 0.4.3
3
+ Version: 0.4.4
4
4
  Summary: 串口控制器工具包
5
5
  Home-page: https://github.com/klrc/sdev
6
6
  Author: klrc
@@ -62,39 +62,39 @@ sdev --no-check-alive shell "echo hello"
62
62
 
63
63
  ## 快速使用 (Python)
64
64
 
65
- 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口:
65
+ 推荐使用 `sdev()` 工厂函数或 `Demoboard` 类,它们提供了更高层、更易用的接口。`Demoboard` 在实例化时默认会自动执行 `check_alive()`。
66
66
 
67
67
  ```python
68
68
  from sdev import sdev
69
69
 
70
70
  # 使用 context manager 自动连接/断开
71
71
  with sdev("/dev/ttyUSB0", 115200) as board:
72
- # connect 时默认会执行 check_alive(),确保板子已就绪
72
+ # 实例化时默认已执行 check_alive(),确保板子已就绪
73
73
  out = board.shell("ls") # 执行命令并返回输出行列表
74
74
  print(out)
75
75
  ```
76
76
 
77
- 手动控制连接与存活检测:
77
+ 使用旧版兼容接口:
78
78
 
79
79
  ```python
80
80
  from sdev import Demoboard
81
81
 
82
- board = Demoboard("/dev/ttyUSB0", 115200)
83
- board.connect()
84
- board.check_alive() # 检查并确保进入 shell 提示符
85
- board.shell("echo hello")
86
- board.disconnect()
82
+ with Demoboard("/dev/ttyUSB0", 115200) as board:
83
+ # execute_command 是 shell 的别名,方便旧代码迁移
84
+ out = board.execute_command("ls", flag=" #")
85
+ print(out)
87
86
  ```
88
87
 
89
88
  ## 主要接口
90
89
 
91
90
  ### Demoboard (推荐)
92
91
 
93
- 继承自 `SerialDevice`,提供针对开发板的高层封装。
92
+ 继承自 `SerialDevice`,提供针对开发板的高层封装。实例化时会自动调用 `check_alive()`。
94
93
 
95
94
  | 方法 | 说明 |
96
95
  |------|------|
97
96
  | `shell(cmd, prompt_flag=" #", timeout=None, stream=False)` | 清缓冲 -> 发送命令 -> 等待命令回显 -> 等待提示符。返回输出列表或生成器。 |
97
+ | `execute_command(cmd, flag, timeout=None, stream=False)` | `shell` 方法的别名,用于兼容旧版代码。 |
98
98
  | `check_alive(uboot_flag="uboot#", prompt_flag=" #", response_timeout=3)` | 存活检测:处理 U-Boot 挂起、发送 Ctrl-C 唤醒并等待提示符。 |
99
99
 
100
100
  ### SerialDevice (底层)
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="sdev",
8
- version="0.4.3",
8
+ version="0.4.4",
9
9
  author="klrc",
10
10
  author_email="144069824@qq.com",
11
11
  description="串口控制器工具包",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes