Metadata-Version: 2.4
Name: pb-calibration
Version: 0.1.7
Summary: PB 车辆配置/标定文件（vehicle_config.proto）反序列化校验、解析为 YAML、从 YAML 统一格式地组装为 .pb.txt。V0.1.7：新增 PB 工作区实体级 CRUD，统一由 pb_calibration 控制 manifest，并保留 V0.1.6 的格式与一致性校验能力。
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: PyYAML>=6.0
Requires-Dist: protobuf>=4.21.0
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"

# pb-calibration

**当前版本：0.1.7**

基于 **vehicle_config.proto**（与 **mdrive** `master` 对齐，依赖 **`modules/message/common/geometry.proto`**）的 PB 车辆配置/标定文件（`.pb.txt`）反序列化校验、解析为 YAML、从 YAML 统一格式地组装为 `.pb.txt`。  
V0.1.7：在 V0.1.6 基础上，新增 PB 工作区实体级 CRUD（list/get/create/update/delete/replace），把 manifest 控制权统一收回到 `pb_calibration`，不再要求外部程序自己拼 `order_manifest.yaml`。  
V0.1.6：无论是未修改、修改还是新增内容，build 输出都统一空格与缩进；未修改块尽量保留原数字字面量，修改/新增块的浮点标准化精度提升到最多 16 位小数；新增 YAML 工作区一致性校验与 register/remove/reorder 共享接口，避免“文件已存在但未被 manifest 引用”的静默错误。  
V0.1.5：新增“未修改原文保真”重组、未知字段警告与尽量保留、浮点标准化输出、YAML 全量双引号字符串化。  
V0.1.4：`vehicle_config.proto` 与上游 mdrive 一致，`Transform` 使用 `mdrive.message.common.Point3D` / `Quaternion`；`VehicleLidarConfig.enable_ids` 为 `uint32`（与此前本地单文件内嵌 `int32` 的实验性定义不同）。  
V0.1.3：在 V0.1.2 基础上为 `VehicleConfig` 增加 `vehicle_lidar_config`（`enable_ids`），兼容 mdrive_conf 新标定。  
V0.1.2：VehicleInfo 新增字段 `color`、`manufacturer` 等；此前版本曾将 Point3D/Quaternion 内嵌在单文件内。

## 环境与安装

推荐使用 **Conda** 环境 `py310`，并在该环境中安装本包及依赖：

```bash
conda activate py310
pip install -e .   # 开发安装
# 或
pip install .      # 普通安装
```

若仅安装不开发，也可：

```bash
pip install pb-calibration
```

## 功能

1. **反序列化检查**：检查 `pb.txt` 是否存在格式问题（能否正常解析）。
2. **解析为 YAML**：将 `pb.txt` 解析为每个单独的 yaml 文件，输出到**指定目录**；该目录与后续「组合」时使用的目录一致。
3. **从 YAML 组装**：按顺序将指定目录中的单独 yaml 文件组装成 `pb.txt`（输入目录与解析输出目录一致，可指定）。
4. **统一格式重组**：无论 PB 内容是否修改，输出都会统一为规范空格与缩进，避免 tab、混排空格和多空格残留。
5. **未知字段警告**：如果输入 PB 含当前 proto 未识别字段，`parse` 会给出 warning；未修改对应块时会尽量保留其原字段内容，修改该块后会再次警告这些字段可能丢失。
6. **统一输出规范**：修改或新增的 `float/double` 字段统一输出为十进制、不使用科学计数法、最多 16 位小数、四舍五入、`0.0` 不省略；空内容不写。未修改块也会统一格式，但尽量保留原数字字面量。
7. **YAML 全量字符串化**：所有 YAML 标量值均以双引号字符串输出，避免隐式类型转换。
8. **工作区一致性校验**：`build` 前会强制检查 `order_manifest` 与 `intrinsics/`、`extrinsics/` 文件集是否一致；孤儿 YAML、缺失文件、重复 `frame_id`、manifest/frame_id 不一致、内参矩阵长度错误都会直接报错。
9. **实体级 CRUD**：新增 `list/get/create/update/delete/replace` 的 intrinsics / extrinsics 公共接口，外部程序不再需要自己控制 manifest。
10. **共享工作区操作**：保留 `validate_yaml_workspace`、`register_intrinsic_file`、`register_extrinsic_file`、`remove_intrinsic_entry`、`remove_extrinsic_entry`、`reorder_intrinsic_entries`、`reorder_extrinsic_entries`，供 skill、GUI 与脚本共用。
11. **顶层块操作**：新增 `get/update_vehicle_info`、`get/update_vehicle_param`、`get/update_vehicle_lidar_config`。
12. **YamlConfigManager**：类，存储默认 YAML 路径；**不缓存 YAML 内容**，每次从默认路径读取，保证多程序共同维护时数据为最新。支持 `read_yaml` / `update_yaml`、`read_order_manifest` / `update_order_manifest`、`rescan` / `get_cached` / `get`、一致性校验与 CRUD 封装。
13. **函数式低层 API**：`read_yaml`、`update_yaml`、`read_order_manifest`、`update_order_manifest` 等接口仍保留，供专家/兼容场景使用，但不再是推荐的 manifest 编辑主路径。

## 命令行

```bash
# 1. 检查 pb.txt 格式
pb-calibration check -i /path/to/vehicle_config.pb.txt

# 2. 解析 pb.txt 为 yaml，输出到指定目录（该目录即后续 build 的输入）
pb-calibration parse -i /path/to/vehicle_config.pb.txt -o /path/to/yaml_dir

# 3. 从指定目录按顺序组装为 pb.txt（目录与 parse 的 -o 一致）
pb-calibration build -i /path/to/yaml_dir -o /path/to/vehicle_config_out.pb.txt

# 4. build 前先检查工作区一致性
pb-calibration validate -i /path/to/yaml_dir
```

## 测试

在本版本目录（V0.1.7）下执行：

```bash
conda activate py310
pip install -e .[dev]
pytest test/ -v
python -m build
```

或单独运行：`pytest test/test_crud_api.py -v`、`pytest test/test_workspace_consistency.py -v`、`pytest test/test_v015_features.py -v`、`pytest test/test_roundtrip.py -v`、`pytest test/test_api.py -v`、`pytest test/test_yaml_manager.py -v`。

## 从 proto 重新生成 `*_pb2.py`（维护者）

在包根目录下，需同时生成 `geometry.proto` 与 `vehicle_config.proto`（若系统无 `protoc`，可用 `grpcio-tools`）：

```bash
conda activate py310
python -m grpc_tools.protoc --proto_path=pb_calibration/protobuf --python_out=pb_calibration/protobuf \
  pb_calibration/protobuf/modules/message/common/geometry.proto \
  pb_calibration/protobuf/vehicle_config.proto
```

生成后请将 `vehicle_config_pb2.py` 中对 `geometry_pb2` 的导入改为相对导入：  
`from .modules.message.common import geometry_pb2 as ...`，以便打包安装后可解析。

## 依赖

- Python >= 3.10（推荐在 conda 环境 `py310` 下使用）
- PyYAML >= 6.0
- protobuf >= 4.21.0

## License

MIT
