Repository navigation
feat(cli): 第一波 CLI 实现 —— po 命令(核心模块 + 测试跑通) - #184
edwinjhlee wants to merge 5 commits into
Conversation
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).
Docker 干净环境验证(python:3.10-slim + arm64)pip install git+https://github.com/x-cmd-sourcecode/python-office@x-cmd-261025-po-cli
slim 镜像部署前置(PyMuPDF 需要,非本 PR 缺陷): |
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.
新增 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>
|
已合入 合入 commit: 验证情况:
关于
几个做得好的地方:
后续如果想推进第二波 CLI(补上 win-only 模块、或者把 GUI 也接到 CLI 下),欢迎继续提 PR! |
本次版本包含 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 行,均按当前代码实际统计修正
发布 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
这是第一波 CLI 实现
目标:给 python-office 增加 Python 编写的命令行
po,本波先跑通部分功能和测试——7 个核心模块 42 个子命令全链路可用,其余模块后续分波补齐。用法
本波内容
office/cli/新子包:声明式注册表 + argparse 构建 + 延迟分发 +--json输出层;只调用公开的office.api.*,对现有代码零侵入(setup.cfg加 entry_points、新增office/__main__.py)office/__init__.py懒加载(PEP 562):import office不再拖入整个 po* 依赖树(只用 pdf 只需装 popdf);office.pdf.f()/from office import pdf/ star-import / AttributeError 语义全部不变,Python 3.6 走 eager 回退pytest tests/test_code/test_cli.py:37 passed(stub 依赖,无需安装 po* 即可跑;含懒加载回归).x-cmd/test.shreal 模式:15/15(真转换 + 产物校验:txt2pdf→merge2pdf 页数、fake2excel 单元格内容、excel2markdown 表格、url2ip 真实 DNS)后续分波计划
video / web / word / ppt / wechat(20 命令)与更多真转换测试已拆成独立 issue 逐步跟进(见 x-cmd-sourcecode#3-#7),注册表结构就绪,加入只是数据条目。
🤖 Generated with Claude Code