Precis 后端 / Precis Backend
Alpha — 核心功能已成型,接口可能调整。
FastAPI + CLI + 核心校验引擎,采用三层分离架构。
完整项目说明请见 根目录 README.md。
架构
backend/app/
├── api/ # API 层
│ ├── main.py # FastAPI 应用入口(路由注册、中间件、CORS)
│ ├── dependencies.py # 依赖注入
│ ├── middleware/ # 中间件
│ ├── models/ # 请求/响应 Pydantic 模型
│ ├── routers/ # 路由定义
│ │ ├── core/ # 核心路由(项目、工作区)
│ │ ├── project/ # V2 项目 CRUD(Schema、Constraint、Regex、Transform)
│ │ ├── validation/ # 校验执行与历史
│ │ ├── preview/ # 数据预览
│ │ └── ai/ # AI 配置生成
│ └── services/ # API 层服务
├── cli/ # 交互式命令行(shell/ REPL、start 启动器、__main__)
├── shared/ # 三层分离架构
│ ├── core/ # 框架级基础设施
│ │ # 文件 I/O、配置解析(YAML)、数据加载
│ ├── domain/ # 纯业务领域逻辑(无 I/O 依赖)
│ │ ├── constraints/ # 10 种约束类型定义(not_null.py、unique.py 等,每种一个文件)
│ │ ├── transforms/ # 22 种转换类型定义(string_split.py、math_expr.py 等,每种一个文件)
│ │ ├── data_types.py # 数据类型定义(string/integer/float/decimal/boolean/date)
│ │ ├── dataset_schema.py # Schema 模型
│ │ ├── expression_system.py # 表达式求值系统
│ │ └── schema/ # Schema 相关领域逻辑
│ └── services/ # 应用服务(编排 core 和 domain)
│ ├── validation/ # 校验引擎(两阶段流水线)
│ │ ├── executor.py # ValidationExecutor 主编排器
│ │ ├── engine.py # 校验执行引擎
│ │ ├── data_loader.py # 数据加载
│ │ ├── loader.py # 配置加载
│ │ ├── resolver.py # 数据源解析
│ │ ├── extractors.py # 派生列提取(regex)
│ │ ├── history.py # 校验历史持久化
│ │ ├── dag/ # 转换 DAG 执行
│ │ ├── validators/ # 各类型校验器(每种约束一个文件)
│ │ └── types.py # 校验类型定义
│ ├── ai/ # AI 服务
│ ├── llm/ # LLM 集成(OpenAI / Ollama)
│ ├── preview/ # 数据预览服务
│ ├── diff/ # 配置差异比较
│ └── hardware.py # 硬件检测
└── start_server.py # 服务器启动入口
关键约定
domain/不得导入core/或services/,保持纯净- API 路由在
api/routers/,请求/响应模型在api/models/ - 路由注册入口:
api/main.py - 所有请求通过
X-Project-Config-Pathheader 标识当前项目 - 校验类接口约定"永远返回 200、结果看 body";配置文件损坏(如清单 YAML 语法错误/空文件)返回 422 并附说明
- 打包模式下
Origin: null跨域请求须携带X-Precis-Auth头(值来自 Electron 注入的PRECIS_API_TOKEN,见api/middleware/token_auth.py);未配置 token 时中间件直通,PRECIS_ALLOW_NULL_ORIGIN=1为旧的全局放行兼容开关
校验引擎(两阶段流水线)
阶段 1: 数据加载与预处理
├── DataSourceResolver → 解析文件路径
├── DataLoader → 加载 Excel/CSV/JSON
├── process_dataframe → 类型转换、格式检查
├── extractors → 派生列提取(regex)
└── Transform DAG → 拓扑排序执行 transform 链
阶段 2: 约束校验
└── 逐约束调用 validate(),聚合错误
(validators/ 下每种类型一个:not_null.py, unique.py, foreign_key.py ...)
开发命令
# 安装
python -m venv .venv
pip install -e ".[dev]"
# 运行
python -m uvicorn app.api.main:app --reload --port 18000
# 代码检查
python -m ruff check . # lint(不自动修复)
python -m ruff check --fix . # lint 自动修复
python -m ruff format . # 格式化
python -m mypy . # 类型检查
# 测试
python -m pytest # 运行全部测试
# CLI
python -B -m app.cli
配置文件格式(V2 YAML)
| 文件类型 | 命名 | 说明 |
|---|---|---|
| 项目清单 | project.precis.yaml |
索引所有 Schema/Constraint/Regex/Transform 资源 |
| Schema | *.schema.yaml |
表结构定义(列、数据类型、内嵌约束) |
| Constraint | *.constraint.yaml |
独立约束(refs + params 分离设计) |
| Regex | *.regex.yaml |
正则节点(引用模式或直接模式) |
Release files for precis-cli 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| precis_cli-0.1.4.tar.gz | 772.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| precis_cli-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 2.0 MB
Release files / precis_cli-0.1.4.tar.gz
| Download URL | precis_cli-0.1.4.tar.gz |
|---|---|
| Size | 772.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1eaad2a1cac7f9a35ffce6310dcada7cec0de97a422813f90cedd8d3e081995b
|
|
BLAKE2b-256 checksum How to use checksums |
7bde4c560b8e70631f797324bcaea973ffc7dd1c4785cfec6e1756772ed50892
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.
Transparency logRelease files / precis_cli-0.1.4-py3-none-any.whl
| Download URL | precis_cli-0.1.4-py3-none-any.whl |
|---|---|
| Size | 1.2 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa5aa689e5afa8c12f24bdcbdb3e67c31bb96022aeb42146355f478e3db74cd2
|
|
BLAKE2b-256 checksum How to use checksums |
bb19ca4e3107899363ae4ddac93bf0cda9e27084fea2bc31eb8ae345aa13ef2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 20, 2026.
Transparency log