leetcode-local-cli 0.10.1__tar.gz → 0.11.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.
Files changed (61) hide show
  1. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/PKG-INFO +40 -13
  2. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/README.md +39 -12
  3. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/pyproject.toml +1 -1
  4. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/account.py +13 -9
  5. leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/common.py +40 -0
  6. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/problems.py +15 -8
  7. leetcode_local_cli-0.10.1/src/leetcode_local_cli/ui.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/rendering.py +22 -21
  8. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/setup.py +16 -5
  9. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/submission.py +4 -4
  10. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/testing.py +15 -13
  11. leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/__init__.py +1 -0
  12. leetcode_local_cli-0.10.1/src/leetcode_local_cli/_test_runner.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/runner.py +5 -5
  13. leetcode_local_cli-0.10.1/src/leetcode_local_cli/workspace.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/worker.py +8 -251
  14. leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/__init__.py +1 -0
  15. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations}/browser.py +11 -8
  16. leetcode_local_cli-0.10.1/src/leetcode_local_cli/auth.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/devtools.py +1 -133
  17. leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/editor.py +23 -0
  18. leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/leetcode.py +399 -0
  19. leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/problem_parser.py +84 -0
  20. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/__init__.py +1 -0
  21. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/account.py +33 -0
  22. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/diagnostics.py +26 -0
  23. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/execution.py +26 -0
  24. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/problem.py +88 -0
  25. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/result.py +46 -0
  26. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/session.py +39 -0
  27. leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/solution.py +32 -0
  28. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/models}/submission.py +1 -4
  29. leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/__init__.py +1 -0
  30. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/config.py +92 -165
  31. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/paths.py +92 -19
  32. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/safe_files.py +33 -4
  33. leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/session.py +88 -0
  34. leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/solution.py +182 -0
  35. {leetcode_local_cli-0.10.1/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/solution_source.py +0 -1
  36. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/account.py +39 -0
  37. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/common.py +66 -0
  38. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/diagnostics.py +129 -0
  39. leetcode_local_cli-0.10.1/src/leetcode_local_cli/doctor.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/doctor_checks.py +48 -98
  40. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/errors.py +27 -0
  41. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/local_test.py +42 -0
  42. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/use_cases/login.py +44 -27
  43. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/use_cases/problems.py +53 -42
  44. leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/setup.py +199 -0
  45. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/use_cases/submission.py +23 -21
  46. leetcode_local_cli-0.10.1/src/leetcode_local_cli/client.py +0 -453
  47. leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/common.py +0 -26
  48. leetcode_local_cli-0.10.1/src/leetcode_local_cli/problem.py +0 -166
  49. leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/account.py +0 -32
  50. leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/common.py +0 -86
  51. leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/diagnostics.py +0 -54
  52. leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/local_test.py +0 -36
  53. leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/setup.py +0 -34
  54. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/LICENSE +0 -0
  55. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/__init__.py +0 -0
  56. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/__main__.py +0 -0
  57. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/cli.py +0 -0
  58. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/commands/__init__.py +0 -0
  59. leetcode_local_cli-0.10.1/src/leetcode_local_cli/local_testing.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/protocol.py +1 -1
  60. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/use_cases/__init__.py +0 -0
  61. {leetcode_local_cli-0.10.1 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/version.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: leetcode-local-cli
3
- Version: 0.10.1
3
+ Version: 0.11.0
4
4
  Summary: Local coding workflow CLI for LeetCode CN.
5
5
  Keywords: cli,leetcode,leetcode-cn,uv
6
6
  Author: Aetherialter
@@ -26,7 +26,9 @@ Description-Content-Type: text/markdown
26
26
 
27
27
  面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
28
28
 
29
- 当前已发布版本:`v0.10.1`。
29
+ 本文对应版本:`v0.11.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
30
+
31
+ 开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
30
32
 
31
33
  ## 安装
32
34
 
@@ -35,13 +37,13 @@ Description-Content-Type: text/markdown
35
37
  Linux / macOS:
36
38
 
37
39
  ```bash
38
- curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
40
+ curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.sh | sh
39
41
  ```
40
42
 
41
43
  Windows PowerShell:
42
44
 
43
45
  ```powershell
44
- powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
46
+ powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.ps1 | iex"
45
47
  ```
46
48
 
47
49
  也可以直接安装并初始化:
@@ -64,13 +66,18 @@ uv tool upgrade leetcode-local-cli
64
66
  uv tool uninstall leetcode-local-cli
65
67
  ```
66
68
 
69
+ 从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
70
+
67
71
  ## 快速开始
68
72
 
69
73
  ```shell
70
- lc init
71
74
  lc login
72
- lc doctor
75
+ lc status
76
+ lc profile
77
+ lc show
73
78
  lc get 1
79
+ lc init
80
+ lc doctor
74
81
  lc solve 1
75
82
  lc test
76
83
  lc submit
@@ -86,26 +93,41 @@ lc check <Submission ID>
86
93
  | 命令 | 作用 |
87
94
  | --- | --- |
88
95
  | `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
96
+ | `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
89
97
  | `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
90
98
  | `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
91
99
  | `lc status` / `lc profile` | 检查登录态或展示账号信息 |
92
100
  | `lc show` / `lc get <题号>` | 查看题目列表或详情 |
93
101
  | `lc solve <题号>` | 生成并打开 `solution.py` |
102
+ | `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
94
103
  | `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
95
104
  | `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
96
105
  | `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
97
106
  | `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
98
107
  | `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
99
108
 
109
+ `login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。账号和题目命令需要有效 Session;`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
110
+
111
+ 解法保存成功但系统无法打开文件时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
112
+
113
+ 用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
114
+
115
+ ```powershell
116
+ uv run lc init D:/Projects/leetcode-local-cli --repair --yes
117
+ ```
118
+
119
+ 修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
120
+
100
121
  ## 浏览器登录
101
122
 
102
- 执行 `lc login` 后:
123
+ 首次使用浏览器自动登录前:
103
124
 
104
- 1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
105
- 2. 浏览器会打开 Remote debugging 页面与 LeetCode。
106
- 3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
107
- 4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
108
- 5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
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。
109
131
 
110
132
  CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
111
133
 
@@ -125,6 +147,8 @@ CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也
125
147
  printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
126
148
  ```
127
149
 
150
+ `--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
151
+
128
152
  `lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
129
153
 
130
154
  等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
@@ -132,7 +156,10 @@ printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
132
156
  ## 安全与限制
133
157
 
134
158
  - 当前只支持 LeetCode 中文站和 Python3 提交。
135
- - Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
159
+ - HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
160
+ - 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`。不得提交、上传、同步或共享。
161
+ - 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
162
+ - 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
136
163
  - `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
137
164
  - `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
138
165
  - 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
@@ -2,7 +2,9 @@
2
2
 
3
3
  面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
4
4
 
5
- 当前已发布版本:`v0.10.1`。
5
+ 本文对应版本:`v0.11.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
6
+
7
+ 开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
6
8
 
7
9
  ## 安装
8
10
 
@@ -11,13 +13,13 @@
11
13
  Linux / macOS:
12
14
 
13
15
  ```bash
14
- curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
16
+ curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.sh | sh
15
17
  ```
16
18
 
17
19
  Windows PowerShell:
18
20
 
19
21
  ```powershell
20
- powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
22
+ powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.ps1 | iex"
21
23
  ```
22
24
 
23
25
  也可以直接安装并初始化:
@@ -40,13 +42,18 @@ uv tool upgrade leetcode-local-cli
40
42
  uv tool uninstall leetcode-local-cli
41
43
  ```
42
44
 
45
+ 从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
46
+
43
47
  ## 快速开始
44
48
 
45
49
  ```shell
46
- lc init
47
50
  lc login
48
- lc doctor
51
+ lc status
52
+ lc profile
53
+ lc show
49
54
  lc get 1
55
+ lc init
56
+ lc doctor
50
57
  lc solve 1
51
58
  lc test
52
59
  lc submit
@@ -62,26 +69,41 @@ lc check <Submission ID>
62
69
  | 命令 | 作用 |
63
70
  | --- | --- |
64
71
  | `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
72
+ | `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
65
73
  | `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
66
74
  | `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
67
75
  | `lc status` / `lc profile` | 检查登录态或展示账号信息 |
68
76
  | `lc show` / `lc get <题号>` | 查看题目列表或详情 |
69
77
  | `lc solve <题号>` | 生成并打开 `solution.py` |
78
+ | `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
70
79
  | `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
71
80
  | `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
72
81
  | `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
73
82
  | `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
74
83
  | `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
75
84
 
85
+ `login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。账号和题目命令需要有效 Session;`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
86
+
87
+ 解法保存成功但系统无法打开文件时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
88
+
89
+ 用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
90
+
91
+ ```powershell
92
+ uv run lc init D:/Projects/leetcode-local-cli --repair --yes
93
+ ```
94
+
95
+ 修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
96
+
76
97
  ## 浏览器登录
77
98
 
78
- 执行 `lc login` 后:
99
+ 首次使用浏览器自动登录前:
79
100
 
80
- 1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
81
- 2. 浏览器会打开 Remote debugging 页面与 LeetCode。
82
- 3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
83
- 4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
84
- 5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
101
+ 1. Chrome 打开 `chrome://inspect/#remote-debugging`;Edge 打开 `edge://inspect/#remote-debugging`。
102
+ 2. 勾选 **Allow remote debugging for this browser instance**,再执行 `lc login`。
103
+ 3. CLI 优先尝试日常 Chrome,再尝试日常 Edge;如果设置页面没有自动显示,请手动打开上述地址。
104
+ 4. 出现 **Allow remote debugging?** 连接确认框时选择 **Allow**,并保持 LeetCode 页面已登录。
105
+ 5. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
106
+ 6. 自动登录失败时,回退到隐藏输入的手动 Cookie。
85
107
 
86
108
  CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
87
109
 
@@ -101,6 +123,8 @@ CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也
101
123
  printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
102
124
  ```
103
125
 
126
+ `--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
127
+
104
128
  `lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
105
129
 
106
130
  等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
@@ -108,7 +132,10 @@ printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
108
132
  ## 安全与限制
109
133
 
110
134
  - 当前只支持 LeetCode 中文站和 Python3 提交。
111
- - Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
135
+ - HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
136
+ - 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`。不得提交、上传、同步或共享。
137
+ - 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
138
+ - 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
112
139
  - `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
113
140
  - `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
114
141
  - 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "leetcode-local-cli"
3
- version = "0.10.1"
3
+ version = "0.11.0"
4
4
  description = "Local coding workflow CLI for LeetCode CN."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,27 +1,27 @@
1
+ from getpass import getpass
1
2
  from pathlib import Path
2
3
  from typing import Annotated
3
4
 
4
5
  from typer import BadParameter, Exit, Option
5
6
 
6
- from leetcode_local_cli.auth import get_cookies_from_input
7
- from leetcode_local_cli.browser import BrowserKind
8
7
  from leetcode_local_cli.commands import common
9
- from leetcode_local_cli.ui import (
8
+ from leetcode_local_cli.commands.rendering import (
10
9
  info,
11
10
  loading,
12
11
  render_profile,
13
12
  success,
14
13
  warning,
15
14
  )
15
+ from leetcode_local_cli.integrations.browser import BrowserKind
16
+ from leetcode_local_cli.integrations.devtools import parse_cookie_header
16
17
  from leetcode_local_cli.use_cases.account import get_account_profile, get_user_status
17
- from leetcode_local_cli.use_cases.common import UseCaseError
18
+ from leetcode_local_cli.use_cases.errors import UseCaseError
18
19
  from leetcode_local_cli.use_cases.login import (
19
20
  LoginReporter,
20
21
  try_automatic_login,
21
22
  validate_and_save_login,
22
23
  )
23
24
 
24
-
25
25
  LOGIN_REPORTER = LoginReporter(info=info, warning=warning, loading=loading)
26
26
 
27
27
 
@@ -43,7 +43,7 @@ def login(
43
43
  ),
44
44
  ] = None,
45
45
  ) -> None:
46
- paths = common.require_app_paths()
46
+ paths = common.get_user_paths()
47
47
  if devtools_port is not None and browser is BrowserKind.AUTO:
48
48
  raise BadParameter(
49
49
  "--devtools-port 必须与 --browser chrome 或 --browser edge 一起使用",
@@ -85,17 +85,21 @@ def _login_manually(session_file: Path) -> None:
85
85
 
86
86
  def status() -> None:
87
87
  try:
88
- user_status = get_user_status(common.require_app_paths())
88
+ user_status = get_user_status(common.get_user_paths())
89
89
  except UseCaseError as exc:
90
90
  common.exit_for_use_case_error(exc)
91
- username = user_status.get("username", "未知用户")
91
+ username = user_status.username or "未知用户"
92
92
  success(f"在线状态: 当前账号 {username}")
93
93
 
94
94
 
95
95
  def profile() -> None:
96
96
  try:
97
97
  with loading("正在获取账户信息..."):
98
- account_profile = get_account_profile(common.require_app_paths())
98
+ account_profile = get_account_profile(common.get_user_paths())
99
99
  except UseCaseError as exc:
100
100
  common.exit_for_use_case_error(exc)
101
101
  render_profile(account_profile)
102
+
103
+
104
+ def get_cookies_from_input() -> dict[str, str] | None:
105
+ return parse_cookie_header(getpass("请粘贴 Cookie(输入内容不会回显):\n"))
@@ -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
@@ -1,10 +1,12 @@
1
1
  from leetcode_local_cli.commands import common
2
- from leetcode_local_cli.ui import (
2
+ from leetcode_local_cli.commands.rendering import (
3
3
  loading,
4
4
  render_problem_detail,
5
5
  render_problem_list,
6
+ success,
7
+ warning,
6
8
  )
7
- from leetcode_local_cli.use_cases.common import UseCaseError
9
+ from leetcode_local_cli.use_cases.errors import UseCaseError
8
10
  from leetcode_local_cli.use_cases.problems import (
9
11
  get_problem_detail_by_question_id,
10
12
  get_problem_summaries,
@@ -15,7 +17,7 @@ from leetcode_local_cli.use_cases.problems import (
15
17
  def get_problem(question_id: str) -> None:
16
18
  try:
17
19
  problem_detail = get_problem_detail_by_question_id(
18
- common.require_app_paths(),
20
+ common.get_user_paths(),
19
21
  question_id,
20
22
  progress=loading,
21
23
  )
@@ -27,7 +29,7 @@ def get_problem(question_id: str) -> None:
27
29
  def show(limit: int = 50, skip: int = 0) -> None:
28
30
  try:
29
31
  problem_summaries = get_problem_summaries(
30
- common.require_app_paths(),
32
+ common.get_user_paths(),
31
33
  limit=limit,
32
34
  skip=skip,
33
35
  progress=loading,
@@ -37,11 +39,11 @@ def show(limit: int = 50, skip: int = 0) -> None:
37
39
  render_problem_list(problem_summaries)
38
40
 
39
41
 
40
- def solve(question_id: str) -> None:
41
- paths = common.require_app_paths()
42
+ def solve(question_id: str, no_open: bool = False) -> None:
42
43
  try:
44
+ paths = common.require_app_paths()
43
45
  problem_detail = get_problem_detail_by_question_id(
44
- paths,
46
+ paths.user,
45
47
  question_id,
46
48
  progress=loading,
47
49
  )
@@ -49,6 +51,11 @@ def solve(question_id: str) -> None:
49
51
  common.exit_for_use_case_error(exc)
50
52
  render_problem_detail(problem_detail)
51
53
  try:
52
- write_problem_solution(paths, problem_detail)
54
+ result = write_problem_solution(
55
+ paths.workspace, problem_detail, open_editor=not no_open
56
+ )
57
+ success(f"解法已保存:{result.path}")
58
+ if result.open_warning:
59
+ warning(result.open_warning)
53
60
  except UseCaseError as exc:
54
61
  common.exit_for_use_case_error(exc)
@@ -1,18 +1,20 @@
1
- from collections.abc import Iterator
1
+ import re
2
+ import shutil
3
+ from collections.abc import Iterator, Sequence
2
4
  from contextlib import contextmanager
3
5
  from html import unescape
4
6
  from html.parser import HTMLParser
5
- import re
6
- import shutil
7
- from typing import Any
8
7
 
9
8
  from rich.console import Console
10
9
  from rich.panel import Panel
11
10
  from rich.table import Table
12
11
  from rich.text import Text
13
12
 
14
- from leetcode_local_cli.doctor import DoctorReport, DoctorStatus
15
- from leetcode_local_cli.submission import (
13
+ from leetcode_local_cli.models.account import AccountProfile
14
+ from leetcode_local_cli.models.diagnostics import DoctorReport, DoctorStatus
15
+ from leetcode_local_cli.models.problem import ProblemDetail, ProblemSummary
16
+ from leetcode_local_cli.models.solution import ProblemMetadata
17
+ from leetcode_local_cli.models.submission import (
16
18
  SubmissionOutcome,
17
19
  SubmissionPending,
18
20
  SubmissionPollingFailed,
@@ -151,38 +153,36 @@ def render_local_execution_error(
151
153
  )
152
154
 
153
155
 
154
- def render_profile(profile: dict[str, Any]) -> None:
155
- solved = profile["solved"]
156
- total = profile["total"]
156
+ def render_profile(profile: AccountProfile) -> None:
157
+ solved = profile.stats.solved
158
+ total = profile.stats.total
157
159
 
158
160
  table = Table(title="LeetCode CN Profile")
159
161
  table.add_column("Item", style="cyan", no_wrap=True)
160
162
  table.add_column("Value", style="white")
161
163
 
162
- table.add_row("Username", _external_text(profile.get("username") or "-"))
163
- table.add_row("Real Name", _external_text(profile.get("real_name") or "-"))
164
- table.add_row(
165
- "Premium", _external_text("Yes" if profile.get("is_premium") else "No")
166
- )
164
+ table.add_row("Username", _external_text(profile.user.username or "-"))
165
+ table.add_row("Real Name", _external_text(profile.user.real_name or "-"))
166
+ table.add_row("Premium", _external_text("Yes" if profile.user.premium else "No"))
167
167
  table.add_row(
168
168
  "Solved",
169
169
  _external_text(
170
- f"All {solved['All']} | Easy {solved['Easy']} | "
171
- f"Medium {solved['Medium']} | Hard {solved['Hard']}"
170
+ f"All {solved.total} | Easy {solved.easy} | "
171
+ f"Medium {solved.medium} | Hard {solved.hard}"
172
172
  ),
173
173
  )
174
174
  table.add_row(
175
175
  "Total",
176
176
  _external_text(
177
- f"All {total['All']} | Easy {total['Easy']} | "
178
- f"Medium {total['Medium']} | Hard {total['Hard']}"
177
+ f"All {total.total} | Easy {total.easy} | "
178
+ f"Medium {total.medium} | Hard {total.hard}"
179
179
  ),
180
180
  )
181
181
 
182
182
  console.print(Panel(table, border_style="green", width=_terminal_width()))
183
183
 
184
184
 
185
- def render_problem_list(problems: list[Any]) -> None:
185
+ def render_problem_list(problems: Sequence[ProblemSummary]) -> None:
186
186
  if not problems:
187
187
  warning("没有可展示的题目")
188
188
  return
@@ -227,7 +227,7 @@ def render_problem_list(problems: list[Any]) -> None:
227
227
  console.print(table)
228
228
 
229
229
 
230
- def render_problem_detail(problem: Any) -> None:
230
+ def render_problem_detail(problem: ProblemDetail) -> None:
231
231
  tags = ", ".join(problem.tags) if problem.tags else "-"
232
232
 
233
233
  meta = Table.grid(padding=(0, 2))
@@ -257,7 +257,7 @@ def render_problem_detail(problem: Any) -> None:
257
257
  warning("未找到 Python3 代码模板")
258
258
 
259
259
 
260
- def render_submission_target(metadata: Any) -> None:
260
+ def render_submission_target(metadata: ProblemMetadata) -> None:
261
261
  console.print(
262
262
  _external_text(
263
263
  f"当前提交目标:{metadata.problem_id}. "
@@ -281,6 +281,7 @@ def render_doctor_report(report: DoctorReport) -> None:
281
281
  "session": "Session 文件",
282
282
  "connectivity": "LeetCode 接口",
283
283
  "authentication": "Cookie 登录态",
284
+ "workspace": "工作区",
284
285
  "solution": "solution.py",
285
286
  }
286
287
  status_labels = {
@@ -1,16 +1,16 @@
1
1
  from pathlib import Path
2
2
  from typing import Annotated
3
3
 
4
- from typer import Argument, Exit, Option, confirm, prompt
4
+ from typer import Argument, BadParameter, Exit, Option, confirm, prompt
5
5
 
6
6
  from leetcode_local_cli.commands.common import exit_for_use_case_error
7
- from leetcode_local_cli.paths import (
7
+ from leetcode_local_cli.commands.rendering import error, info, success, warning
8
+ from leetcode_local_cli.storage.paths import (
8
9
  APP_DIRECTORY_NAME,
9
10
  get_user_config_file,
10
11
  normalize_workspace_path,
11
12
  )
12
- from leetcode_local_cli.ui import error, info, success, warning
13
- from leetcode_local_cli.use_cases.common import UseCaseError
13
+ from leetcode_local_cli.use_cases.errors import UseCaseError
14
14
  from leetcode_local_cli.use_cases.setup import (
15
15
  configure_workspace,
16
16
  resolve_existing_workspace,
@@ -26,9 +26,16 @@ def init_workspace(
26
26
  bool,
27
27
  Option("--yes", help="使用显式路径初始化并跳过交互确认"),
28
28
  ] = False,
29
+ repair: Annotated[
30
+ bool, Option("--repair", help="备份并恢复损坏的用户配置和目标工作区标记")
31
+ ] = False,
29
32
  ) -> None:
30
33
  """配置默认工作区,并安全创建工作区基础文件。"""
31
34
  config_file = get_user_config_file()
35
+ if repair and path is None:
36
+ raise BadParameter(
37
+ "--repair 必须与工作区完整路径一起使用", param_hint="--repair"
38
+ )
32
39
  if path is None:
33
40
  try:
34
41
  existing_paths = resolve_existing_workspace(config_file)
@@ -55,10 +62,14 @@ def init_workspace(
55
62
  return
56
63
 
57
64
  try:
58
- result = configure_workspace(workspace_root, config_file=config_file)
65
+ result = configure_workspace(
66
+ workspace_root, config_file=config_file, repair=repair
67
+ )
59
68
  except UseCaseError as exc:
60
69
  exit_for_use_case_error(exc)
61
70
 
71
+ for backup in result.backups:
72
+ info(f"原始配置已备份:{backup}")
62
73
  if result.reused:
63
74
  success(f"工作区已配置,现有文件保持不变:{result.paths.workspace_root}")
64
75
  else:
@@ -4,13 +4,13 @@ from typing import Annotated
4
4
  from typer import Argument, BadParameter, Exit, Option
5
5
 
6
6
  from leetcode_local_cli.commands import common
7
- from leetcode_local_cli.submission import SubmissionJudged
8
- from leetcode_local_cli.ui import (
7
+ from leetcode_local_cli.commands.rendering import (
9
8
  render_submission_result,
10
9
  render_submission_submitted,
11
10
  render_submission_target,
12
11
  )
13
- from leetcode_local_cli.use_cases.common import UseCaseError
12
+ from leetcode_local_cli.models.submission import SubmissionJudged
13
+ from leetcode_local_cli.use_cases.errors import UseCaseError
14
14
  from leetcode_local_cli.use_cases.submission import (
15
15
  DEFAULT_SUBMISSION_WAIT_TIMEOUT_SECONDS,
16
16
  check_existing_submission,
@@ -62,7 +62,7 @@ def check(
62
62
  )
63
63
  try:
64
64
  result = check_existing_submission(
65
- common.require_app_paths(),
65
+ common.get_user_paths(),
66
66
  submission_id,
67
67
  )
68
68
  except UseCaseError as exc:
@@ -6,11 +6,7 @@ from typing import Annotated
6
6
  from typer import BadParameter, Exit, Option, echo
7
7
 
8
8
  from leetcode_local_cli.commands import common
9
- from leetcode_local_cli.local_testing import (
10
- LocalTestInputError,
11
- parse_parameter_assignments,
12
- )
13
- from leetcode_local_cli.ui import (
9
+ from leetcode_local_cli.commands.rendering import (
14
10
  error,
15
11
  info,
16
12
  loading,
@@ -20,14 +16,18 @@ from leetcode_local_cli.ui import (
20
16
  success,
21
17
  warning,
22
18
  )
23
- from leetcode_local_cli.use_cases.common import UseCaseError
24
- from leetcode_local_cli.use_cases.diagnostics import get_doctor_report
25
- from leetcode_local_cli.use_cases.local_test import start_local_test
26
- from leetcode_local_cli.workspace import (
19
+ from leetcode_local_cli.execution.protocol import (
20
+ LocalTestInputError,
21
+ parse_parameter_assignments,
22
+ )
23
+ from leetcode_local_cli.execution.worker import LocalExecutionWorker
24
+ from leetcode_local_cli.models.execution import (
27
25
  LocalExecutionResult,
28
26
  LocalExecutionStatus,
29
- LocalExecutionWorker,
30
27
  )
28
+ from leetcode_local_cli.use_cases.diagnostics import get_doctor_report
29
+ from leetcode_local_cli.use_cases.errors import UseCaseError
30
+ from leetcode_local_cli.use_cases.local_test import start_local_test
31
31
 
32
32
 
33
33
  def test(
@@ -48,12 +48,14 @@ def test(
48
48
 
49
49
  try:
50
50
  worker = start_local_test(
51
- common.require_app_paths().solution_file,
51
+ common.require_workspace_paths().solution_file,
52
52
  timeout=timeout,
53
53
  )
54
54
  except UseCaseError as exc:
55
55
  if stdin:
56
- _write_stdin_test_event({"kind": "startup_error", "error": exc.message})
56
+ _write_stdin_test_event(
57
+ {"kind": "startup_error", "code": exc.code.value, "error": exc.message}
58
+ )
57
59
  raise Exit(1) from exc
58
60
  common.exit_for_use_case_error(exc)
59
61
 
@@ -225,7 +227,7 @@ def doctor(
225
227
  ),
226
228
  ] = False,
227
229
  ) -> None:
228
- paths = common.require_app_paths()
230
+ paths = common.get_user_paths()
229
231
  with loading("正在检查本地环境与 LeetCode 连接..."):
230
232
  report = get_doctor_report(paths, run_solution=run_solution)
231
233
  render_doctor_report(report)