Metadata-Version: 2.4
Name: autestoy
Version: 0.1.6
Summary: autestoy is a Python toy library for automating tests. autestoy -> auto test toy
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: dearpygui>=2.3
Requires-Dist: paramiko>=4.0.0
Requires-Dist: pyserial>=3.5
Requires-Dist: screeninfo>=0.8.1
Requires-Dist: telnetlib3>=4.0.2

# autestoy

> auto + test + toy — 面向嵌入式/硬件测试场景的 Python 自动化测试工具库

## 主要功能

- **多协议远程连接**：SSH（含 Channel、SFTP、sudo）、Serial、Telnet
- **本机命令执行**：`Local` / `LocalPopen`，接口与远程协议一致
- **类 Verilog 数据类型**：`Bits` 支持位宽字面量、切片、拼接（`@`）、位运算；`BitView` 视图借用
- **寄存器建模**：`Register` / `Field` / `RegGroup`，快速定义硬件寄存器及位域
- **命令记录**：`CmdRecord` 统一封装执行结果，支持正则搜索、字段提取、fifo 匹配
- **消息总线**：`MessageIO` 发布/订阅模式，解耦命令执行与输出渲染

## 安装

```bash
pip install autestoy
```

## 快速上手

### SSH

```python
import autestoy as att

conf = att.RemoteConfig(
    user="root", host="192.168.1.100", password="xxx"
).set_name("DUT")

with att.SSH(conf) as ssh:
    # 执行命令
    r = ssh.exec_run("uname -a")
    if "Linux" in r:
        print("Linux 设备")

    # 正则搜索
    m = r.search(r"(\d+\.\d+\.\d+)")
    print(m.group(1))  # 内核版本

    # 批量执行
    ssh.exec_run_lines("make clean", "make", "make test")

    # 后台长任务 + 实时监控
    task = ssh.long_running("tail -f /var/log/app.log")
    ssh.exec_run("systemctl restart my-app")
    t = att.TrySeconds(10)
    while t:
        if task.search_next_line(r"error"):
            att.ulog("发现异常", att.AnsiColor.red)
            break
    task.task_kill()
```

### Channel / SFTP

```python
with att.SSH(conf) as ssh:
    # 交互式通道（保持目录和环境变量）
    with ssh.create_channel("build") as ch:
        ch.run("cd /project && export MODE=release")
        ch.run("./build.sh")

    # 文件传输
    sftp = ssh.create_ftp()
    sftp.put("local.bin", "/remote/fw.bin")
    sftp.get("/remote/log.txt", "./log.txt")
```

### Bits

```python
val = att.Bits("8'd255")        # Verilog 风格字面量
val = att.Bits(0x1234, 16)      # 整数 + 位宽
low = val[7:0]                  # 切片读取 : 0x34
val[15:8] = 0xAB                # 切片赋值
c = att.Bits(0xFF, 8) @ att.Bits(0x00, 8)  # 拼接 : 0xFF00
```

### 寄存器定义

```python
from autestoy.tools.register import Field, Register, dataclass
from autestoy.tools.datatype import Bits

@dataclass(slots=True)
class PWR_CTLR(Register):
    VOLTAGE = Field(
        bit_range=(12, 10), default=Bits(0, 3),
        select={Bits(0b000, 3): "1.2V", Bits(0b011, 3): "3.3V"},
        R=True, W=True,
    )

reg = PWR_CTLR()
print(reg.VOLTAGE.select_str)   # 当前电压描述
```

### 串口 / Telnet / 本机

```python
ser = att.SerialShell(att.SerialConfig(port="COM3", baudrate=115200))
ser.login(("root", "xxx"))
ser.shell_run("cat /proc/cpuinfo")

with att.Local() as local:
    local.exec_run("df -h")
```

### 消息总线

```python
from autestoy.export.messageio import MessageDispatcher
from autestoy.export.term import MessageTerminal

disp = MessageDispatcher()
disp.link_line(MessageTerminal()).start()
disp.start()
# ... 正常使用，输出自动路由到终端
disp.join()
```

## 项目结构

```
src/autestoy/
├── __init__.py
├── export/          # 终端输出、消息总线、Markdown 导出
├── protocols/       # SSH、Serial、Telnet、本地执行
└── tools/           # Bits、Register、CmdRecord、ansi、时间戳等
```
