Skip to content

feat(cli): 第一波 CLI 实现 —— po 命令(核心模块 + 测试跑通) - #184

Closed
edwinjhlee wants to merge 5 commits into
CoderWanFeng:developfrom
x-cmd-sourcecode:x-cmd-261025-po-cli
Closed

edwinjhlee wants to merge 5 commits into
CoderWanFeng:developfrom
x-cmd-sourcecode:x-cmd-261025-po-cli

Conversation

@edwinjhlee

Copy link
Copy Markdown
Contributor

这是第一波 CLI 实现

目标:给 python-office 增加 Python 编写的命令行 po,本波先跑通部分功能和测试——7 个核心模块 42 个子命令全链路可用,其余模块后续分波补齐。

用法

po --help                                    # 7 模块 42 命令
po pdf merge2pdf a.pdf b.pdf --output-file merged.pdf
po excel fake2excel --columns name company --rows 5
po --json tools url2ip www.baidu.com         # 单行 JSON,方便脚本/agent
python -m office --version                   # 等价入口

本波内容

  • office/cli/ 新子包:声明式注册表 + argparse 构建 + 延迟分发 + --json 输出层;只调用公开的 office.api.*,对现有代码零侵入(setup.cfg 加 entry_points、新增 office/__main__.py)
  • 核心 7 模块 42 命令:pdf(11) / excel(7) / tools(6) / image(8) / file(8) / markdown(1) / finance(1);help 全英文
  • office/__init__.py 懒加载(PEP 562):import office 不再拖入整个 po* 依赖树(只用 pdf 只需装 popdf);office.pdf.f() / from office import pdf / star-import / AttributeError 语义全部不变,Python 3.6 走 eager 回退
  • 平台守卫:Windows-only 模块在 macOS/Linux 上干净报错(先守卫后导入)
  • 测试三件套:
    • pytest tests/test_code/test_cli.py:37 passed(stub 依赖,无需安装 po* 即可跑;含懒加载回归)
    • .x-cmd/test.sh real 模式:15/15(真转换 + 产物校验:txt2pdf→merge2pdf 页数、fake2excel 单元格内容、excel2markdown 表格、url2ip 真实 DNS)
    • stub 模式:10/10(零依赖环境自动回退)

后续分波计划

video / web / word / ppt / wechat(20 命令)与更多真转换测试已拆成独立 issue 逐步跟进(见 x-cmd-sourcecode#3-#7),注册表结构就绪,加入只是数据条目。

注:分支同时携带 x-cmd 镜像的若干修复提交,CLI 相关为最后 4 个提交(8bfe862…c6463e2)。

🤖 Generated with Claude Code

Add a Python-written CLI to python-office with zero invasion of existing
code (see #1 for the design, #2 for the supply-chain audit done first):

- office/cli/: declarative registry (commands.py), argparse builder +
  lazy dispatch (main.py), text/--json output layer (output.py)
- office/__main__.py: `python -m office` alias
- setup.cfg: console_scripts entry `po = office.cli.main:main`
- first batch covers pdf (11 cmds), excel (7 cmds), tools (4 cmds);
  Windows-only modules are platform-guarded with clean errors
- import-time banners and library prints are redirected to stderr in
  --json mode so stdout stays parseable
- tests/test_code/test_cli.py: 33 tests (registry integrity, parser,
  dispatch via stubbed po* deps, json/error paths, platform guard);
  runs without any po* packages installed
Tier 1 (cross-platform): image (8), file (8), markdown (1), finance (1),
video (4), web (1), plus tools lottery8ticket & create_article.

Tier 2 (Windows-only, platform-guarded): word (5), ppt (3), wechat (7) —
blocked with a clean error on macOS/Linux before any import happens.

Also adds a BOOL arg type (--dir-rename false) for api params whose
library default is True, where a store_true flag would silently invert
the default. finance.t0 is pure stdlib so its dispatch test computes for
real; the rest of the new dispatch tests run against stubbed po* deps.

Tests: 63 passed (was 33).
Per review: showing both languages in every help line is neither one
nor the other — international CLI convention is a single language,
defaulting to English. Convert all registry help strings, parser
description, --json/-V help, the platform-guard error and the
interrupted message to English. Registry data defaults that mirror the
api (e.g. wechat recipient defaults) are unchanged. Locale-based
switching can be layered on later by upgrading help values to
{cn, en} dicts without touching dispatch. Tests still 63 passed.
Scope the CLI to the core cross-platform modules whose deps are light
and installable: pdf, excel, tools, image, file, markdown, finance
(42 commands). The non-core modules (video / web / word / ppt / wechat,
20 commands) move to #3 — the full implementation remains retrievable
from commit e5dbc8a.

office/__init__.py now lazy-loads the api submodules via PEP 562
__getattr__ (eager fallback kept for Python 3.6). `import office` no
longer drags in the whole po* dependency tree — using pdf requires only
popdf, not povideo/moviepy/poocr/etc. All existing usages keep working:
`import office; office.pdf.f()`, `from office import pdf`, star-import
(guaranteed via __all__), dir(); unknown attributes still raise
AttributeError. This resolves the "known cost" deferred in #1 and is
what lets the core CLI run (and be tested) with core deps only.

Add .x-cmd/test.sh — E2E smoke runner with two modes: real (deps
installed: actual file transforms, output verified) and stub (MagicMock
deps: parser/help/guard/json plumbing). Real mode passes 15/15 on macOS
with only the core deps installed; stub mode 10/10 anywhere.

pytest: 37 passed (adds a lazy-import regression test).
@edwinjhlee

Copy link
Copy Markdown
Contributor Author

Docker 干净环境验证(python:3.10-slim + arm64)

pip install git+https://github.com/x-cmd-sourcecode/python-office@x-cmd-261025-po-cli
验证项 结果
po 入口安装 ✅ /usr/local/bin/po,po --version → po 1.0.6 (python-office)
po --help ✅ 7 模块 42 命令
po pdf txt2pdf ✅ 真 PDF 40KB,PyMuPDF 提取文本逐字还原
po pdf merge2pdf ✅ 1+1 页 → 2 页
po excel fake2excel ✅ 真 xlsx,中文模拟数据(陈燕/倩市…)
po --json finance t0 ✅ {"ok": true, "result": "88.9000"}
po --json tools passwordtools ✅ 真随机密码
错误路径 ✅ 依赖缺失时干净单行报错(po: error: ImportError: ...)而非裸 traceback

slim 镜像部署前置(PyMuPDF 需要,非本 PR 缺陷):apt-get install -y libgl1 libglib2.0-0。国内网络建议 apt/pip 均走 aliyun 镜像,否则全量依赖安装会很慢。

python:3.10-slim + aliyun apt/pip mirrors + the two system libs PyMuPDF
needs on headless images (libgl1, libglib2.0-0) + the local checkout with
core deps only (--no-deps plus popdf/poexcel/poimage/pofile/wftools/
pomarkdown/pocode, keeping the heavy tail of install_requires out — the
point of the lazy office/__init__).

Default CMD runs .x-cmd/test.sh; also usable for
pytest tests/test_code/test_cli.py or an interactive po shell.

Verified on arm64: E2E real mode 15/15, pytest 37 passed inside the image.
CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
新增 office/cli 子包,提供 `po` 命令行入口与 `python -m office` 别名:

- office/cli/commands.py: 62 个命令的统一注册表
- office/cli/main.py: argparse 解析与分发
- office/cli/output.py: 文本 / JSON 双输出
- office/__init__.py: PEP 562 懒加载,import office 不再拖入整套 po* 依赖
- office/__main__.py: python -m office 入口
- setup.cfg: 注册 console script `po = office.cli.main:main`
- tests/test_code/test_cli.py: 303 行自包含测试(内置 po* stub,无需重型依赖)
- .x-cmd/: 干净环境 Docker 测试镜像 + E2E smoke 脚本

Co-authored-by: Li Junhao <edwin.jh.lee@gmail.com>
@CoderWanFeng

Copy link
Copy Markdown
Owner

已合入 develop,感谢贡献!🎉

合入 commit:f297919 — feat(cli): 第一波 CLI 实现 —— po 命令 (#184)(5 个 commit 已 squash 成 1 个,保留了你的署名 Co-authored-by)

验证情况:

  • 你自带的 tests/test_code/test_cli.py 37 条用例全部通过
  • .x-cmd/test.sh(stub 模式)10 passed / 0 failed / 4 skipped,4 条 skip 是需要真实 po* 依赖的用例,属预期
  • 既有测试集无回归(与合入前基线逐条比对一致)
  • 懒加载改造的兼容性我额外验证了:import office、office.pdf 属性访问、from office import pdf/excel/word/image、from office import *、from office.api import finance 全部正常;且 import office 后确实没有加载任何 po* 模块,访问 office.pdf 才按需加载——懒加载真实生效

关于 office/__init__.py 的 PEP 562 懒加载,保留意见(供后续参考,不影响本次合入):
这个改造的收益是实打实的——import office 不再拖入 poexcel/popdf/povideo 这类重型依赖树,对 CLI 启动速度和安装体积都有帮助。但它有一个隐性代价:__init__.py 里不再有真实的 from office.api import ... 语句,IDE 的「跳转定义」「自动补全」会退化(需要装 python -m pip install pylint + pylint --load-plugins=pylint.extensions.docparams 之类的插件才能恢复一部分)。目前是 core 目录里少有的非 API 层改动,先按你的方案合入,后续如果社区反馈跳转体验下降,可以考虑把懒加载下沉到 office/cli/ 内部,core 保持原样。

.x-cmd/ 目录也一并保留了。这是你为 CLI 准备的干净环境测试基建(Dockerfile + E2E smoke),对后续维护 CLI 有实际价值,不是无用文件。

几个做得好的地方:

  • 5 个 commit 拆得清楚(注册表 / 解析分发 / 输出 / 懒加载 / 文档),squash 后结构依然完整
  • 测试用内置 stub 预加载 po* 依赖,不装重型依赖也能跑 CI,这个思路很聪明
  • --json 模式保证 stdout 只有一行 JSON,还专门写了用例验证「库函数自己 print 时 stdout 仍干净」——这个边界很多人会漏
  • 平台守卫(win-only 模块在非 Windows 上干净报错、不触发 import)也覆盖了

后续如果想推进第二波 CLI(补上 win-only 模块、或者把 GUI 也接到 CLI 下),欢迎继续提 PR!

CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
本次版本包含 CLI(`po` 命令)与 6 个新 Skill,属于功能性新增,按语义化版本升 minor。

- setup.cfg / office/__init__.py: 1.0.6 → 1.1.0
- wiki 文档中的版本号引用同步更新(开发者指南、快速入门、安装指南、代码结构说明)
- 代码结构说明:API 模块 13→14(markdown/ocr 早已在列),并把
  「按序导入 13 个 API 模块」改为「PEP 562 懒加载 14 个 API 模块」,
  与 #184 的实现对齐
- 代码结构说明:Skill 数 73→81、贡献者 12→15、pdf.py 434→433 行、
  finance.py 51→53 行,均按当前代码实际统计修正
CoderWanFeng pushed a commit that referenced this pull request Oct 26, 2026
发布 1.1.0:合入 #183 / #184 / #185 三个 PR,以及自 1.0.6 以来的全部修复。

主要内容:
- feat(cli): 第一波 CLI 实现 —— `po` 命令 (#184)
- feat(contributors): 5 个 Excel 数据核对与整理方法 (#185)
- feat(contributor): batch_rename_by_excel (#183)
- fix(excel): 拆分时保留 0 值 (#180)
- fix(finance): t0 的 Decimal/float TypeError (#181)
- fix(excel): 拆分输出工作簿有效性 (#182)
- 及其他 1.0.6 以来的修复与文档更新
- 合并 main 独有的 #141 / #155

版本号: 1.0.6 → 1.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants