leetcode-local-cli 0.10.1__tar.gz → 0.12.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- leetcode_local_cli-0.12.0/PKG-INFO +219 -0
- leetcode_local_cli-0.12.0/README.md +195 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/pyproject.toml +1 -1
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/__init__.py +2 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/account.py +32 -9
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/commands/common.py +40 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/problems.py +30 -8
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/ui.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/commands/rendering.py +32 -23
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/commands/settings.py +50 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/setup.py +16 -5
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/submission.py +4 -4
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/testing.py +40 -13
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/execution/__init__.py +1 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/execution/nodes.py +266 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/local_testing.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/execution/protocol.py +9 -2
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/_test_runner.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/execution/runner.py +66 -16
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/workspace.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/execution/worker.py +32 -249
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations/__init__.py +1 -0
- {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations}/browser.py +11 -8
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/auth.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations/devtools.py +3 -134
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations/editor.py +47 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations/leetcode.py +439 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/integrations/problem_parser.py +109 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/__init__.py +1 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/account.py +33 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/diagnostics.py +26 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/editor.py +20 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/execution.py +37 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/nodes.py +16 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/problem.py +88 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/result.py +48 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/session.py +39 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/models/solution.py +32 -0
- {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.12.0/src/leetcode_local_cli/models}/submission.py +1 -4
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage/__init__.py +1 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage/config.py +305 -0
- {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage}/paths.py +92 -19
- {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage}/safe_files.py +33 -4
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage/session.py +88 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage/solution.py +228 -0
- {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.12.0/src/leetcode_local_cli/storage}/solution_source.py +0 -1
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/account.py +39 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/common.py +70 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/diagnostics.py +129 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/doctor.py → leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/doctor_checks.py +65 -98
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/errors.py +27 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/local_test.py +59 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/use_cases/login.py +85 -36
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/use_cases/problems.py +58 -42
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/settings.py +54 -0
- leetcode_local_cli-0.12.0/src/leetcode_local_cli/use_cases/setup.py +211 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/use_cases/submission.py +23 -21
- leetcode_local_cli-0.10.1/PKG-INFO +0 -151
- leetcode_local_cli-0.10.1/README.md +0 -127
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/client.py +0 -453
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/common.py +0 -26
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/config.py +0 -317
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/problem.py +0 -166
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/account.py +0 -32
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/common.py +0 -86
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/diagnostics.py +0 -54
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/local_test.py +0 -36
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/setup.py +0 -34
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/LICENSE +0 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/__init__.py +0 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/__main__.py +0 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/cli.py +0 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/use_cases/__init__.py +0 -0
- {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/version.py +0 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leetcode-local-cli
|
|
3
|
+
Version: 0.12.0
|
|
4
|
+
Summary: Local coding workflow CLI for LeetCode CN.
|
|
5
|
+
Keywords: cli,leetcode,leetcode-cn,uv
|
|
6
|
+
Author: Aetherialter
|
|
7
|
+
Author-email: Aetherialter <Aetherialter@outlook.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Dist: httpx>=0.28.1
|
|
16
|
+
Requires-Dist: rich>=15.0.0
|
|
17
|
+
Requires-Dist: typer>=0.25.1
|
|
18
|
+
Requires-Dist: websockets>=15.0
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Homepage, https://github.com/Aetherialter/leetcode-local-cli
|
|
21
|
+
Project-URL: Issues, https://github.com/Aetherialter/leetcode-local-cli/issues
|
|
22
|
+
Project-URL: Repository, https://github.com/Aetherialter/leetcode-local-cli
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# leetcode-local-cli
|
|
26
|
+
|
|
27
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
28
|
+
|
|
29
|
+
本文对应版本:`v0.12.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
|
|
30
|
+
|
|
31
|
+
开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
|
|
32
|
+
|
|
33
|
+
## 安装
|
|
34
|
+
|
|
35
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
36
|
+
|
|
37
|
+
Linux / macOS:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.12.0/scripts/install.sh | sh
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Windows PowerShell:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.12.0/scripts/install.ps1 | iex"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
也可以直接安装并初始化:
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
uv tool install leetcode-local-cli
|
|
53
|
+
lc init
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
自动化验收可以显式指定完整工作区路径:
|
|
57
|
+
|
|
58
|
+
```shell
|
|
59
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
升级或卸载:
|
|
63
|
+
|
|
64
|
+
```shell
|
|
65
|
+
uv tool upgrade leetcode-local-cli
|
|
66
|
+
uv tool uninstall leetcode-local-cli
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
|
|
70
|
+
|
|
71
|
+
## 快速开始
|
|
72
|
+
|
|
73
|
+
```shell
|
|
74
|
+
lc login
|
|
75
|
+
lc status
|
|
76
|
+
lc profile
|
|
77
|
+
lc show
|
|
78
|
+
lc get 1
|
|
79
|
+
lc config editor zed
|
|
80
|
+
lc init
|
|
81
|
+
lc doctor
|
|
82
|
+
lc solve 1
|
|
83
|
+
lc test
|
|
84
|
+
lc submit
|
|
85
|
+
lc check <Submission ID>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
89
|
+
|
|
90
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
91
|
+
|
|
92
|
+
## 命令
|
|
93
|
+
|
|
94
|
+
| 命令 | 作用 |
|
|
95
|
+
| --- | --- |
|
|
96
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
97
|
+
| `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
|
|
98
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
99
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
100
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
101
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
102
|
+
| `lc config editor [程序] [--arg=参数]` | 查看或设置用户编辑器;`--arg` 可重复,`--clear` 移除设置 |
|
|
103
|
+
| `lc solve <题号> [--editor 程序]` | 生成 `solution.py`,使用已配置或临时指定的编辑器 |
|
|
104
|
+
| `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
|
|
105
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
106
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
107
|
+
| `lc test --verbose` | 显示完整异常调用栈;也可配合 `--stdin` |
|
|
108
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
109
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
110
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
111
|
+
|
|
112
|
+
`config editor`、`login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。`status`、`profile`、`show`、`get`、`solve`、`submit` 和 `check` 都需要本地存在格式完整的 `session.json`;其中题目命令(`show`、`get`、`solve`)不预先验证远端登录态,公开题目查询成功不能证明当前登录仍有效。如需确认远端登录状态及当前 Cookie 实际对应的账号,请使用 `lc status`、`lc profile` 或 `lc doctor`(浏览器当前登录账号、`session.json` 记录的用户名,以及 Cookie 实际对应的远端账号可能不一致)。`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
|
|
113
|
+
|
|
114
|
+
解法保存成功但未配置编辑器或启动失败时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
|
|
115
|
+
|
|
116
|
+
用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
|
|
117
|
+
|
|
118
|
+
```powershell
|
|
119
|
+
uv run lc init D:/Projects/leetcode-local-cli --repair --yes
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
|
|
123
|
+
|
|
124
|
+
## 编辑器配置
|
|
125
|
+
|
|
126
|
+
编辑器属于当前用户的个人设置,存入用户配置目录的 `config.toml`,不存入工作区 marker 或 Session。尚未初始化时也可设置;之后初始化或切换默认工作区会保留它。
|
|
127
|
+
|
|
128
|
+
用户配置格式现为 v2,工作区 marker 仍为 v1,Session 格式不变。已有 v1 用户配置(含内测编辑器设置)可继续读取,读取不改写文件;显式设置/清除编辑器或执行 `init` 时才保存为 v2。也可用 `lc config editor zed` 保存并升级。升级后旧版 `lc` 会拒绝该配置,不能用旧版 `init --repair` 降级。
|
|
129
|
+
|
|
130
|
+
```shell
|
|
131
|
+
lc config editor zed
|
|
132
|
+
lc config editor code --arg=--reuse-window
|
|
133
|
+
lc config editor
|
|
134
|
+
lc solve 1 --editor zed
|
|
135
|
+
lc solve 1 --no-open
|
|
136
|
+
lc config editor --clear
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`zed`、`code` 和别名 `vscode` 从 PATH 查找,也可指定可执行文件绝对路径。可重复的 `--arg` 按参数数组原样传递,路径最后追加;不支持 Shell 命令串或文件占位符。配置不自动安装编辑器。
|
|
140
|
+
|
|
141
|
+
优先级为:`--no-open` 禁止打开(并跳过 `--editor` 校验);否则 `--editor` 覆盖用户配置,且不继承旧编辑器的参数(非法 `--editor` 参数按用法错误返回退出码 2);没有临时参数时使用用户设置。不增加工作区或环境变量覆盖层。
|
|
142
|
+
|
|
143
|
+
未配置时只保存并提示,不使用系统 `.py` 文件关联,避免关联到 Python/uv 后意外执行代码。显式配置失败也不静默回退。Windows 的 VS Code 标准 `code.cmd` 入口会解析为安装目录的 `Code.exe`,其他 `.cmd`/`.bat` 编辑器入口拒绝执行。启动后短时间内非零退出会警告;GUI 持续运行视为请求已发出,不强行终止,也不保证窗口一定可见。
|
|
144
|
+
|
|
145
|
+
## 浏览器登录
|
|
146
|
+
|
|
147
|
+
首次使用浏览器自动登录前:
|
|
148
|
+
|
|
149
|
+
1. Chrome 打开 `chrome://inspect/#remote-debugging`;Edge 打开 `edge://inspect/#remote-debugging`。
|
|
150
|
+
2. 勾选 **Allow remote debugging for this browser instance**,再执行 `lc login`。
|
|
151
|
+
3. CLI 优先尝试日常 Chrome,再尝试日常 Edge;如果设置页面没有自动显示,请手动打开上述地址。
|
|
152
|
+
4. 出现 **Allow remote debugging?** 连接确认框时选择 **Allow**,并保持 LeetCode 页面已登录。
|
|
153
|
+
5. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
154
|
+
6. 自动登录失败时,在交互式终端中回退到隐藏输入的手动 Cookie;若处于管道、后台脚本等非交互式终端,则立即提示并返回退出码 1,不阻塞等待输入、不从管道读取明文 Cookie,也不破坏已有 `session.json`。
|
|
155
|
+
|
|
156
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。单次浏览器尝试共享 180 秒单调时钟预算(涵盖连接、授权等待、重试与 Cookie 轮询,不含连接关闭超时与随后的在线账号校验)。
|
|
157
|
+
|
|
158
|
+
## 本地调用
|
|
159
|
+
|
|
160
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
161
|
+
|
|
162
|
+
```text
|
|
163
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。
|
|
167
|
+
|
|
168
|
+
支持按入口类型注解转换节点:`ListNode`、`TreeNode`、`Optional[...]`、`... | None`,以及相应的字符串注解。普通 `list` 保持列表含义,不根据参数名猜类型;节点参数缺少注解时请补齐。嵌套节点集合及混合节点类型注解会明确拒绝。
|
|
169
|
+
|
|
170
|
+
启用 `from __future__ import annotations` 不改变这些规则,`list["ListNode"]` 同样拒绝。`Literal["ListNode"]` 中的字符串是取值,`Annotated[int, "ListNode"]` 中的字符串是元数据,均不触发节点转换;`Annotated[ListNode, ...]` 等节点包装暂不支持。
|
|
171
|
+
|
|
172
|
+
```text
|
|
173
|
+
l1 = [2,4,3], l2 = [5,6,4]
|
|
174
|
+
root = [1,null,2,3]
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
链表用整数数组;树用力扣式层序数组,`null` 与 `None` 都表示空位(字符串 `"null"` 不受影响)。空数组或 `null`/`None` 表示空节点;树中无法连接到根的非空值会被拒绝。返回节点转换回同类数组,树尾多余空位移除;有节点返回注解时空节点输出 `[]`。普通方法返回 `None` 时,展示调用后的参数,节点参数也转回数组。
|
|
178
|
+
|
|
179
|
+
生成模板提供位于提交区域外的本地节点定义,不将这些辅助定义发送给 LeetCode。第一版只支持整数节点的无环单链表和普通二叉树,不支持环、共享节点身份、随机指针、N 叉树、嵌套节点集合及设计题多方法调用。单个节点数组输入最多 100,000 项,输出最多 100,000 个节点;超过限制明确失败,不截断为成功。
|
|
180
|
+
|
|
181
|
+
非交互示例:
|
|
182
|
+
|
|
183
|
+
```shell
|
|
184
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
|
|
188
|
+
|
|
189
|
+
异常默认显示类型、原因和可定位的 `solution.py` 行号,并保留 stdout/stderr;`--verbose` 增加完整调用栈,不采集局部变量。JSON 模式通过 `error_line` 和可选 `traceback` 字段表达,不混入 Rich 文本。调用栈仍可能包含源码片段、路径及异常内容,共享前请自行检查。
|
|
190
|
+
|
|
191
|
+
某组超时后,下一组会重启 worker;若重新加载失败,该组报告失败并保留行号及可选调用栈,后续输入仍可继续尝试。
|
|
192
|
+
|
|
193
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。遇到明确认证失效(HTTP 401 或已确认 GraphQL 鉴权错误码)会提示执行 `lc login`;遇到权限拒绝(HTTP 403 或对应权限错误码)会提示检查账号权限或登录状态。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
194
|
+
|
|
195
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果;若服务端返回未完成中间态(对仍在处理、跨账号不可见或不存在的提交,接口可能返回相同状态),CLI 会提示暂未取得可确认的判题结果,请确认提交 ID 和当前账号,并返回退出码 1(若提交确仍在处理可稍后重查);只有结果为 Accepted 时返回退出码 0。
|
|
196
|
+
|
|
197
|
+
## 安全与限制
|
|
198
|
+
|
|
199
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
200
|
+
- HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
|
|
201
|
+
- Session 包含真实 Cookie,保存在当前系统用户的本地状态目录:Windows 为 `%LOCALAPPDATA%\leetcode-local-cli\session.json`,macOS 为 `~/Library/Application Support/leetcode-local-cli/session.json`,Linux 为 `${XDG_STATE_HOME:-~/.local/state}/leetcode-local-cli/session.json`。不得提交、上传、同步或共享。
|
|
202
|
+
- 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
|
|
203
|
+
- 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
|
|
204
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
205
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
206
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
207
|
+
|
|
208
|
+
## 维护者文档
|
|
209
|
+
|
|
210
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
211
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
212
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
213
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
214
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
215
|
+
- [发布流程](docs/RELEASING.md)
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# leetcode-local-cli
|
|
2
|
+
|
|
3
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
4
|
+
|
|
5
|
+
本文对应版本:`v0.12.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
|
|
6
|
+
|
|
7
|
+
开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
12
|
+
|
|
13
|
+
Linux / macOS:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.12.0/scripts/install.sh | sh
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Windows PowerShell:
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.12.0/scripts/install.ps1 | iex"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
也可以直接安装并初始化:
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
uv tool install leetcode-local-cli
|
|
29
|
+
lc init
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
自动化验收可以显式指定完整工作区路径:
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
升级或卸载:
|
|
39
|
+
|
|
40
|
+
```shell
|
|
41
|
+
uv tool upgrade leetcode-local-cli
|
|
42
|
+
uv tool uninstall leetcode-local-cli
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
|
|
46
|
+
|
|
47
|
+
## 快速开始
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
lc login
|
|
51
|
+
lc status
|
|
52
|
+
lc profile
|
|
53
|
+
lc show
|
|
54
|
+
lc get 1
|
|
55
|
+
lc config editor zed
|
|
56
|
+
lc init
|
|
57
|
+
lc doctor
|
|
58
|
+
lc solve 1
|
|
59
|
+
lc test
|
|
60
|
+
lc submit
|
|
61
|
+
lc check <Submission ID>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
65
|
+
|
|
66
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
67
|
+
|
|
68
|
+
## 命令
|
|
69
|
+
|
|
70
|
+
| 命令 | 作用 |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
73
|
+
| `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
|
|
74
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
75
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
76
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
77
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
78
|
+
| `lc config editor [程序] [--arg=参数]` | 查看或设置用户编辑器;`--arg` 可重复,`--clear` 移除设置 |
|
|
79
|
+
| `lc solve <题号> [--editor 程序]` | 生成 `solution.py`,使用已配置或临时指定的编辑器 |
|
|
80
|
+
| `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
|
|
81
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
82
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
83
|
+
| `lc test --verbose` | 显示完整异常调用栈;也可配合 `--stdin` |
|
|
84
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
85
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
86
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
87
|
+
|
|
88
|
+
`config editor`、`login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。`status`、`profile`、`show`、`get`、`solve`、`submit` 和 `check` 都需要本地存在格式完整的 `session.json`;其中题目命令(`show`、`get`、`solve`)不预先验证远端登录态,公开题目查询成功不能证明当前登录仍有效。如需确认远端登录状态及当前 Cookie 实际对应的账号,请使用 `lc status`、`lc profile` 或 `lc doctor`(浏览器当前登录账号、`session.json` 记录的用户名,以及 Cookie 实际对应的远端账号可能不一致)。`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
|
|
89
|
+
|
|
90
|
+
解法保存成功但未配置编辑器或启动失败时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
|
|
91
|
+
|
|
92
|
+
用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
|
|
93
|
+
|
|
94
|
+
```powershell
|
|
95
|
+
uv run lc init D:/Projects/leetcode-local-cli --repair --yes
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
|
|
99
|
+
|
|
100
|
+
## 编辑器配置
|
|
101
|
+
|
|
102
|
+
编辑器属于当前用户的个人设置,存入用户配置目录的 `config.toml`,不存入工作区 marker 或 Session。尚未初始化时也可设置;之后初始化或切换默认工作区会保留它。
|
|
103
|
+
|
|
104
|
+
用户配置格式现为 v2,工作区 marker 仍为 v1,Session 格式不变。已有 v1 用户配置(含内测编辑器设置)可继续读取,读取不改写文件;显式设置/清除编辑器或执行 `init` 时才保存为 v2。也可用 `lc config editor zed` 保存并升级。升级后旧版 `lc` 会拒绝该配置,不能用旧版 `init --repair` 降级。
|
|
105
|
+
|
|
106
|
+
```shell
|
|
107
|
+
lc config editor zed
|
|
108
|
+
lc config editor code --arg=--reuse-window
|
|
109
|
+
lc config editor
|
|
110
|
+
lc solve 1 --editor zed
|
|
111
|
+
lc solve 1 --no-open
|
|
112
|
+
lc config editor --clear
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`zed`、`code` 和别名 `vscode` 从 PATH 查找,也可指定可执行文件绝对路径。可重复的 `--arg` 按参数数组原样传递,路径最后追加;不支持 Shell 命令串或文件占位符。配置不自动安装编辑器。
|
|
116
|
+
|
|
117
|
+
优先级为:`--no-open` 禁止打开(并跳过 `--editor` 校验);否则 `--editor` 覆盖用户配置,且不继承旧编辑器的参数(非法 `--editor` 参数按用法错误返回退出码 2);没有临时参数时使用用户设置。不增加工作区或环境变量覆盖层。
|
|
118
|
+
|
|
119
|
+
未配置时只保存并提示,不使用系统 `.py` 文件关联,避免关联到 Python/uv 后意外执行代码。显式配置失败也不静默回退。Windows 的 VS Code 标准 `code.cmd` 入口会解析为安装目录的 `Code.exe`,其他 `.cmd`/`.bat` 编辑器入口拒绝执行。启动后短时间内非零退出会警告;GUI 持续运行视为请求已发出,不强行终止,也不保证窗口一定可见。
|
|
120
|
+
|
|
121
|
+
## 浏览器登录
|
|
122
|
+
|
|
123
|
+
首次使用浏览器自动登录前:
|
|
124
|
+
|
|
125
|
+
1. Chrome 打开 `chrome://inspect/#remote-debugging`;Edge 打开 `edge://inspect/#remote-debugging`。
|
|
126
|
+
2. 勾选 **Allow remote debugging for this browser instance**,再执行 `lc login`。
|
|
127
|
+
3. CLI 优先尝试日常 Chrome,再尝试日常 Edge;如果设置页面没有自动显示,请手动打开上述地址。
|
|
128
|
+
4. 出现 **Allow remote debugging?** 连接确认框时选择 **Allow**,并保持 LeetCode 页面已登录。
|
|
129
|
+
5. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
130
|
+
6. 自动登录失败时,在交互式终端中回退到隐藏输入的手动 Cookie;若处于管道、后台脚本等非交互式终端,则立即提示并返回退出码 1,不阻塞等待输入、不从管道读取明文 Cookie,也不破坏已有 `session.json`。
|
|
131
|
+
|
|
132
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。单次浏览器尝试共享 180 秒单调时钟预算(涵盖连接、授权等待、重试与 Cookie 轮询,不含连接关闭超时与随后的在线账号校验)。
|
|
133
|
+
|
|
134
|
+
## 本地调用
|
|
135
|
+
|
|
136
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
137
|
+
|
|
138
|
+
```text
|
|
139
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。
|
|
143
|
+
|
|
144
|
+
支持按入口类型注解转换节点:`ListNode`、`TreeNode`、`Optional[...]`、`... | None`,以及相应的字符串注解。普通 `list` 保持列表含义,不根据参数名猜类型;节点参数缺少注解时请补齐。嵌套节点集合及混合节点类型注解会明确拒绝。
|
|
145
|
+
|
|
146
|
+
启用 `from __future__ import annotations` 不改变这些规则,`list["ListNode"]` 同样拒绝。`Literal["ListNode"]` 中的字符串是取值,`Annotated[int, "ListNode"]` 中的字符串是元数据,均不触发节点转换;`Annotated[ListNode, ...]` 等节点包装暂不支持。
|
|
147
|
+
|
|
148
|
+
```text
|
|
149
|
+
l1 = [2,4,3], l2 = [5,6,4]
|
|
150
|
+
root = [1,null,2,3]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
链表用整数数组;树用力扣式层序数组,`null` 与 `None` 都表示空位(字符串 `"null"` 不受影响)。空数组或 `null`/`None` 表示空节点;树中无法连接到根的非空值会被拒绝。返回节点转换回同类数组,树尾多余空位移除;有节点返回注解时空节点输出 `[]`。普通方法返回 `None` 时,展示调用后的参数,节点参数也转回数组。
|
|
154
|
+
|
|
155
|
+
生成模板提供位于提交区域外的本地节点定义,不将这些辅助定义发送给 LeetCode。第一版只支持整数节点的无环单链表和普通二叉树,不支持环、共享节点身份、随机指针、N 叉树、嵌套节点集合及设计题多方法调用。单个节点数组输入最多 100,000 项,输出最多 100,000 个节点;超过限制明确失败,不截断为成功。
|
|
156
|
+
|
|
157
|
+
非交互示例:
|
|
158
|
+
|
|
159
|
+
```shell
|
|
160
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
`--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
|
|
164
|
+
|
|
165
|
+
异常默认显示类型、原因和可定位的 `solution.py` 行号,并保留 stdout/stderr;`--verbose` 增加完整调用栈,不采集局部变量。JSON 模式通过 `error_line` 和可选 `traceback` 字段表达,不混入 Rich 文本。调用栈仍可能包含源码片段、路径及异常内容,共享前请自行检查。
|
|
166
|
+
|
|
167
|
+
某组超时后,下一组会重启 worker;若重新加载失败,该组报告失败并保留行号及可选调用栈,后续输入仍可继续尝试。
|
|
168
|
+
|
|
169
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。遇到明确认证失效(HTTP 401 或已确认 GraphQL 鉴权错误码)会提示执行 `lc login`;遇到权限拒绝(HTTP 403 或对应权限错误码)会提示检查账号权限或登录状态。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
170
|
+
|
|
171
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果;若服务端返回未完成中间态(对仍在处理、跨账号不可见或不存在的提交,接口可能返回相同状态),CLI 会提示暂未取得可确认的判题结果,请确认提交 ID 和当前账号,并返回退出码 1(若提交确仍在处理可稍后重查);只有结果为 Accepted 时返回退出码 0。
|
|
172
|
+
|
|
173
|
+
## 安全与限制
|
|
174
|
+
|
|
175
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
176
|
+
- HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
|
|
177
|
+
- Session 包含真实 Cookie,保存在当前系统用户的本地状态目录:Windows 为 `%LOCALAPPDATA%\leetcode-local-cli\session.json`,macOS 为 `~/Library/Application Support/leetcode-local-cli/session.json`,Linux 为 `${XDG_STATE_HOME:-~/.local/state}/leetcode-local-cli/session.json`。不得提交、上传、同步或共享。
|
|
178
|
+
- 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
|
|
179
|
+
- 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
|
|
180
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
181
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
182
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
183
|
+
|
|
184
|
+
## 维护者文档
|
|
185
|
+
|
|
186
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
187
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
188
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
189
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
190
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
191
|
+
- [发布流程](docs/RELEASING.md)
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
[MIT License](LICENSE)
|
{leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/__init__.py
RENAMED
|
@@ -2,6 +2,7 @@ from typer import Typer
|
|
|
2
2
|
|
|
3
3
|
from leetcode_local_cli.commands.account import login, profile, status
|
|
4
4
|
from leetcode_local_cli.commands.problems import get_problem, show, solve
|
|
5
|
+
from leetcode_local_cli.commands.settings import config_app
|
|
5
6
|
from leetcode_local_cli.commands.setup import init_workspace
|
|
6
7
|
from leetcode_local_cli.commands.submission import check, submit
|
|
7
8
|
from leetcode_local_cli.commands.testing import doctor, test
|
|
@@ -10,6 +11,7 @@ from leetcode_local_cli.commands.testing import doctor, test
|
|
|
10
11
|
def register_commands(app: Typer) -> None:
|
|
11
12
|
"""Register every public CLI command without coupling handlers to the app."""
|
|
12
13
|
app.command("init")(init_workspace)
|
|
14
|
+
app.add_typer(config_app, name="config")
|
|
13
15
|
app.command()(login)
|
|
14
16
|
app.command()(status)
|
|
15
17
|
app.command()(profile)
|
{leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/account.py
RENAMED
|
@@ -1,27 +1,28 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from getpass import getpass
|
|
1
3
|
from pathlib import Path
|
|
2
4
|
from typing import Annotated
|
|
3
5
|
|
|
4
6
|
from typer import BadParameter, Exit, Option
|
|
5
7
|
|
|
6
|
-
from leetcode_local_cli.auth import get_cookies_from_input
|
|
7
|
-
from leetcode_local_cli.browser import BrowserKind
|
|
8
8
|
from leetcode_local_cli.commands import common
|
|
9
|
-
from leetcode_local_cli.
|
|
9
|
+
from leetcode_local_cli.commands.rendering import (
|
|
10
10
|
info,
|
|
11
11
|
loading,
|
|
12
12
|
render_profile,
|
|
13
13
|
success,
|
|
14
14
|
warning,
|
|
15
15
|
)
|
|
16
|
+
from leetcode_local_cli.integrations.browser import BrowserKind
|
|
17
|
+
from leetcode_local_cli.integrations.devtools import parse_cookie_header
|
|
16
18
|
from leetcode_local_cli.use_cases.account import get_account_profile, get_user_status
|
|
17
|
-
from leetcode_local_cli.use_cases.
|
|
19
|
+
from leetcode_local_cli.use_cases.errors import UseCaseError
|
|
18
20
|
from leetcode_local_cli.use_cases.login import (
|
|
19
21
|
LoginReporter,
|
|
20
22
|
try_automatic_login,
|
|
21
23
|
validate_and_save_login,
|
|
22
24
|
)
|
|
23
25
|
|
|
24
|
-
|
|
25
26
|
LOGIN_REPORTER = LoginReporter(info=info, warning=warning, loading=loading)
|
|
26
27
|
|
|
27
28
|
|
|
@@ -43,7 +44,7 @@ def login(
|
|
|
43
44
|
),
|
|
44
45
|
] = None,
|
|
45
46
|
) -> None:
|
|
46
|
-
paths = common.
|
|
47
|
+
paths = common.get_user_paths()
|
|
47
48
|
if devtools_port is not None and browser is BrowserKind.AUTO:
|
|
48
49
|
raise BadParameter(
|
|
49
50
|
"--devtools-port 必须与 --browser chrome 或 --browser edge 一起使用",
|
|
@@ -63,7 +64,21 @@ def login(
|
|
|
63
64
|
_login_manually(paths.session_file)
|
|
64
65
|
|
|
65
66
|
|
|
67
|
+
def _is_interactive_stdin() -> bool:
|
|
68
|
+
if sys.stdin is None:
|
|
69
|
+
return False
|
|
70
|
+
try:
|
|
71
|
+
return sys.stdin.isatty()
|
|
72
|
+
except ValueError:
|
|
73
|
+
return False
|
|
74
|
+
|
|
75
|
+
|
|
66
76
|
def _login_manually(session_file: Path) -> None:
|
|
77
|
+
if not _is_interactive_stdin():
|
|
78
|
+
warning(
|
|
79
|
+
"当前为非交互式终端,无法安全读取手动 Cookie;请在交互式终端中执行 lc login"
|
|
80
|
+
)
|
|
81
|
+
raise Exit(1)
|
|
67
82
|
warning("无法自动获取 LeetCode 登录状态,请手动粘贴 Cookie")
|
|
68
83
|
manual_cookies = get_cookies_from_input()
|
|
69
84
|
if not manual_cookies:
|
|
@@ -85,17 +100,25 @@ def _login_manually(session_file: Path) -> None:
|
|
|
85
100
|
|
|
86
101
|
def status() -> None:
|
|
87
102
|
try:
|
|
88
|
-
user_status = get_user_status(common.
|
|
103
|
+
user_status = get_user_status(common.get_user_paths())
|
|
89
104
|
except UseCaseError as exc:
|
|
90
105
|
common.exit_for_use_case_error(exc)
|
|
91
|
-
username = user_status.
|
|
106
|
+
username = user_status.username or "未知用户"
|
|
92
107
|
success(f"在线状态: 当前账号 {username}")
|
|
93
108
|
|
|
94
109
|
|
|
95
110
|
def profile() -> None:
|
|
96
111
|
try:
|
|
97
112
|
with loading("正在获取账户信息..."):
|
|
98
|
-
account_profile = get_account_profile(common.
|
|
113
|
+
account_profile = get_account_profile(common.get_user_paths())
|
|
99
114
|
except UseCaseError as exc:
|
|
100
115
|
common.exit_for_use_case_error(exc)
|
|
101
116
|
render_profile(account_profile)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def get_cookies_from_input() -> dict[str, str] | None:
|
|
120
|
+
try:
|
|
121
|
+
raw_cookie = getpass("请粘贴 Cookie(输入内容不会回显):\n")
|
|
122
|
+
except EOFError:
|
|
123
|
+
return None
|
|
124
|
+
return parse_cookie_header(raw_cookie)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from typing import NoReturn
|
|
2
|
+
|
|
3
|
+
from typer import Exit
|
|
4
|
+
|
|
5
|
+
from leetcode_local_cli.commands.rendering import error, warning
|
|
6
|
+
from leetcode_local_cli.storage.config import (
|
|
7
|
+
ConfigError,
|
|
8
|
+
resolve_app_paths,
|
|
9
|
+
resolve_workspace_paths,
|
|
10
|
+
)
|
|
11
|
+
from leetcode_local_cli.storage.paths import AppPaths, UserPaths, WorkspacePaths
|
|
12
|
+
from leetcode_local_cli.use_cases.errors import ErrorCode, UseCaseError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_user_paths() -> UserPaths:
|
|
16
|
+
return UserPaths.defaults()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def require_app_paths() -> AppPaths:
|
|
20
|
+
try:
|
|
21
|
+
return resolve_app_paths()
|
|
22
|
+
except ConfigError as exc:
|
|
23
|
+
raise UseCaseError(str(exc), code=ErrorCode.WORKSPACE_CONFIG) from exc
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def require_workspace_paths() -> WorkspacePaths:
|
|
27
|
+
try:
|
|
28
|
+
return resolve_workspace_paths()
|
|
29
|
+
except ConfigError as exc:
|
|
30
|
+
raise UseCaseError(str(exc), code=ErrorCode.WORKSPACE_CONFIG) from exc
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def exit_for_use_case_error(exc: UseCaseError) -> NoReturn:
|
|
34
|
+
if exc.warning_only:
|
|
35
|
+
warning(exc.message)
|
|
36
|
+
else:
|
|
37
|
+
error(exc.message)
|
|
38
|
+
if exc.suggestion:
|
|
39
|
+
warning(exc.suggestion)
|
|
40
|
+
raise Exit(1) from exc
|
{leetcode_local_cli-0.10.1 → leetcode_local_cli-0.12.0}/src/leetcode_local_cli/commands/problems.py
RENAMED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
+
from typer import BadParameter
|
|
2
|
+
|
|
1
3
|
from leetcode_local_cli.commands import common
|
|
2
|
-
from leetcode_local_cli.
|
|
4
|
+
from leetcode_local_cli.commands.rendering import (
|
|
3
5
|
loading,
|
|
4
6
|
render_problem_detail,
|
|
5
7
|
render_problem_list,
|
|
8
|
+
success,
|
|
9
|
+
warning,
|
|
6
10
|
)
|
|
7
|
-
from leetcode_local_cli.
|
|
11
|
+
from leetcode_local_cli.models.editor import EditorConfig
|
|
12
|
+
from leetcode_local_cli.use_cases.errors import UseCaseError
|
|
8
13
|
from leetcode_local_cli.use_cases.problems import (
|
|
9
14
|
get_problem_detail_by_question_id,
|
|
10
15
|
get_problem_summaries,
|
|
11
16
|
write_problem_solution,
|
|
12
17
|
)
|
|
18
|
+
from leetcode_local_cli.use_cases.settings import get_editor
|
|
13
19
|
|
|
14
20
|
|
|
15
21
|
def get_problem(question_id: str) -> None:
|
|
16
22
|
try:
|
|
17
23
|
problem_detail = get_problem_detail_by_question_id(
|
|
18
|
-
common.
|
|
24
|
+
common.get_user_paths(),
|
|
19
25
|
question_id,
|
|
20
26
|
progress=loading,
|
|
21
27
|
)
|
|
@@ -27,7 +33,7 @@ def get_problem(question_id: str) -> None:
|
|
|
27
33
|
def show(limit: int = 50, skip: int = 0) -> None:
|
|
28
34
|
try:
|
|
29
35
|
problem_summaries = get_problem_summaries(
|
|
30
|
-
common.
|
|
36
|
+
common.get_user_paths(),
|
|
31
37
|
limit=limit,
|
|
32
38
|
skip=skip,
|
|
33
39
|
progress=loading,
|
|
@@ -37,11 +43,19 @@ def show(limit: int = 50, skip: int = 0) -> None:
|
|
|
37
43
|
render_problem_list(problem_summaries)
|
|
38
44
|
|
|
39
45
|
|
|
40
|
-
def solve(question_id: str) -> None:
|
|
41
|
-
|
|
46
|
+
def solve(question_id: str, no_open: bool = False, editor: str | None = None) -> None:
|
|
47
|
+
selected_editor: EditorConfig | None = None
|
|
48
|
+
if not no_open and editor is not None:
|
|
49
|
+
try:
|
|
50
|
+
selected_editor = EditorConfig(editor)
|
|
51
|
+
except ValueError as exc:
|
|
52
|
+
raise BadParameter(str(exc), param_hint="--editor") from exc
|
|
42
53
|
try:
|
|
54
|
+
paths = common.require_app_paths()
|
|
55
|
+
if not no_open and selected_editor is None:
|
|
56
|
+
selected_editor = get_editor(paths.user)
|
|
43
57
|
problem_detail = get_problem_detail_by_question_id(
|
|
44
|
-
paths,
|
|
58
|
+
paths.user,
|
|
45
59
|
question_id,
|
|
46
60
|
progress=loading,
|
|
47
61
|
)
|
|
@@ -49,6 +63,14 @@ def solve(question_id: str) -> None:
|
|
|
49
63
|
common.exit_for_use_case_error(exc)
|
|
50
64
|
render_problem_detail(problem_detail)
|
|
51
65
|
try:
|
|
52
|
-
write_problem_solution(
|
|
66
|
+
result = write_problem_solution(
|
|
67
|
+
paths.workspace,
|
|
68
|
+
problem_detail,
|
|
69
|
+
open_editor=not no_open,
|
|
70
|
+
editor=selected_editor,
|
|
71
|
+
)
|
|
72
|
+
success(f"解法已保存:{result.path}")
|
|
73
|
+
if result.open_warning:
|
|
74
|
+
warning(result.open_warning)
|
|
53
75
|
except UseCaseError as exc:
|
|
54
76
|
common.exit_for_use_case_error(exc)
|