@hfyjq/crannia 1.0.0 → 1.1.0

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 (221) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +45 -45
  3. package/THIRD-PARTY-FRONTEND.md +5266 -5266
  4. package/THIRD-PARTY-PYTHON.md +162 -153
  5. package/THIRD-PARTY.md +316 -316
  6. package/apps/desktop/quotes.txt +56 -56
  7. package/bus/core.py +671 -671
  8. package/bus/loaders/channel_loader.py +64 -64
  9. package/bus/loaders/skill_md.py +676 -676
  10. package/bus/skill_state.py +124 -124
  11. package/channels/cli_channel/manifest.yaml +5 -5
  12. package/channels/dingtalk_channel/manifest.yaml +12 -12
  13. package/channels/feishu_channel/manifest.yaml +12 -12
  14. package/channels/qq_bot_channel/__init__.py +1 -1
  15. package/channels/qq_bot_channel/handler.py +881 -881
  16. package/channels/qq_bot_channel/manifest.yaml +4 -4
  17. package/channels/telegram_channel/__init__.py +1 -1
  18. package/channels/telegram_channel/handler.py +813 -813
  19. package/channels/telegram_channel/manifest.yaml +4 -4
  20. package/channels/telegram_channel/media.py +231 -231
  21. package/channels/textfmt.py +58 -58
  22. package/channels/wechat_channel/__init__.py +1 -1
  23. package/channels/wechat_channel/auth.py +23 -5
  24. package/channels/wechat_channel/manifest.yaml +5 -5
  25. package/channels/wechat_channel/media.py +178 -178
  26. package/config.yaml +2 -2
  27. package/core/activity.py +3 -0
  28. package/core/agent_loop.py +390 -55
  29. package/core/approval.py +17 -6
  30. package/core/artifacts.py +7 -0
  31. package/core/auto_review.py +23 -5
  32. package/core/channel_configs.py +34 -2
  33. package/core/channel_turns.py +759 -759
  34. package/core/child_approval.py +150 -0
  35. package/core/config_requirements.py +205 -0
  36. package/core/cron/runner.py +118 -6
  37. package/core/cron/store.py +14 -5
  38. package/core/cron/tools.py +17 -5
  39. package/core/endpoint.py +141 -0
  40. package/core/events.py +23 -0
  41. package/core/i18n.py +329 -329
  42. package/core/ice/store.py +191 -191
  43. package/core/inbound.py +193 -193
  44. package/core/jobs.py +19 -3
  45. package/core/last_good.py +150 -150
  46. package/core/llm_errors.py +227 -227
  47. package/core/llm_retry.py +230 -230
  48. package/core/memory/compressor.py +1214 -1214
  49. package/core/memory/manager.py +14 -0
  50. package/core/model_configs.py +80 -0
  51. package/core/pipeline/scheduler.py +2 -2
  52. package/core/pipeline/stages/agent_loop.py +24 -4
  53. package/core/pipeline/stages/pre_process.py +58 -7
  54. package/core/prompt_assembler.py +680 -680
  55. package/core/rg.py +255 -255
  56. package/core/security.py +1544 -1544
  57. package/core/settings_baseline.py +70 -1
  58. package/core/shell.py +5 -17
  59. package/core/skill_settings.py +7 -2
  60. package/core/state_registry.py +19 -0
  61. package/core/subagent.py +64 -3
  62. package/core/task_tool.py +1 -1
  63. package/core/tasks_notify.py +69 -4
  64. package/core/tiers.py +9 -3
  65. package/core/tool_outcome.py +108 -0
  66. package/core/update_tui.py +158 -0
  67. package/core/version.py +2 -2
  68. package/core/web_cache.py +152 -0
  69. package/crannia_tray.py +278 -278
  70. package/creators/models.json +4 -4
  71. package/data/settings.example.json +67 -0
  72. package/licenses/GPL-3.0.txt +619 -619
  73. package/licenses/LGPL-2.1.txt +502 -502
  74. package/licenses/LGPL-3.0.txt +165 -165
  75. package/licenses/MIT-chat-on-wechat.txt +18 -18
  76. package/licenses/MIT-deepseek-harness.txt +21 -21
  77. package/licenses/Molio-LICENSE.txt +44 -44
  78. package/locales/en.json +9 -4
  79. package/main.py +154 -154
  80. package/mcpx/manager.py +522 -522
  81. package/mcpx/servers/__init__.py +22 -22
  82. package/mcpx/servers/web_search.py +321 -33
  83. package/package.json +14 -9
  84. package/providers/anthropic/client.py +772 -772
  85. package/providers/cost_table.json +3779 -3779
  86. package/providers/deepseek/client.py +20 -20
  87. package/providers/deepseek/manifest.yaml +2 -2
  88. package/providers/factory.py +136 -136
  89. package/providers/lmstudio/client.py +2 -2
  90. package/providers/metadata.py +80 -80
  91. package/providers/model_capabilities.json +6665 -6665
  92. package/providers/moonshot/client.py +12 -12
  93. package/providers/ollama/client.py +2 -2
  94. package/providers/openai/client.py +13 -13
  95. package/providers/openrouter/client.py +2 -2
  96. package/providers/qwen/__init__.py +1 -1
  97. package/providers/qwen/client.py +2 -2
  98. package/providers/qwen/manifest.yaml +16 -16
  99. package/providers/rate_limiter.py +144 -144
  100. package/providers/siliconflow/client.py +2 -2
  101. package/providers/validation.py +171 -171
  102. package/providers/window_table.json +8909 -8909
  103. package/sbom.json +4413 -4296
  104. package/skills/entertainment/SKILL.md +35 -35
  105. package/skills/entertainment/__init__.py +1 -1
  106. package/skills/entertainment/handler.py +91 -91
  107. package/skills/entertainment/manifest.yaml +26 -26
  108. package/skills/file_parser/GUIDANCE.md +10 -10
  109. package/skills/file_parser/handler.py +177 -4
  110. package/skills/file_parser/manifest.yaml +22 -1
  111. package/skills/github/SKILL.md +32 -32
  112. package/skills/github/__init__.py +1 -1
  113. package/skills/github/manifest.yaml +5 -0
  114. package/skills/mail_parser/SKILL.md +68 -68
  115. package/skills/mail_parser/__init__.py +1 -1
  116. package/skills/mail_parser/handler.py +806 -806
  117. package/skills/mail_parser/manifest.yaml +5 -0
  118. package/skills/media_gen/GUIDANCE.md +19 -19
  119. package/skills/media_gen/SKILL.md +43 -43
  120. package/skills/media_gen/__init__.py +1 -1
  121. package/skills/media_gen/handler.py +430 -430
  122. package/skills/media_gen/manifest.yaml +93 -88
  123. package/skills/office_parser/cell_validation.py +127 -127
  124. package/skills/office_parser/excel_transform.py +936 -936
  125. package/skills/office_parser/layout_spec.py +243 -243
  126. package/skills/office_parser/pptx_export/console_encoding.py +27 -27
  127. package/skills/office_parser/pptx_export/resource_paths.py +88 -88
  128. package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
  129. package/skills/office_parser/pptx_export/svg_finalize/align_embed_images.py +575 -575
  130. package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
  131. package/skills/office_parser/pptx_export/svg_finalize/embed_icons.py +498 -498
  132. package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
  133. package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
  134. package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
  135. package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
  136. package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
  137. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
  138. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
  139. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
  140. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/elements.py +2490 -2490
  141. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
  142. package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/styles.py +656 -656
  143. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
  144. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
  145. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
  146. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
  147. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/media.py +141 -141
  148. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
  149. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
  150. package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
  151. package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
  152. package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
  153. package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
  154. package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
  155. package/skills/office_parser/references/layout_library.md +816 -816
  156. package/skills/office_parser/references/ppt-master/LICENSE +21 -21
  157. package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
  158. package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
  159. package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
  160. package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
  161. package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
  162. package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
  163. package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
  164. package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
  165. package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
  166. package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
  167. package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
  168. package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
  169. package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
  170. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
  171. package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
  172. package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
  173. package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
  174. package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
  175. package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
  176. package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
  177. package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
  178. package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
  179. package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
  180. package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
  181. package/skills/office_parser/references/spec_lock.md +235 -235
  182. package/skills/office_parser/soffice.py +68 -68
  183. package/skills/office_parser/word_tools.py +2909 -2909
  184. package/tui/crannia.cmd +134 -0
  185. package/tui/crannia_tui/__init__.py +1 -0
  186. package/tui/crannia_tui/__main__.py +223 -0
  187. package/tui/crannia_tui/app.py +1318 -0
  188. package/tui/crannia_tui/app.tcss +153 -0
  189. package/tui/crannia_tui/approval.py +85 -0
  190. package/tui/crannia_tui/backend.py +279 -0
  191. package/tui/crannia_tui/channel.py +472 -0
  192. package/tui/crannia_tui/client.py +125 -0
  193. package/tui/crannia_tui/clipboard.py +62 -0
  194. package/tui/crannia_tui/commands.py +65 -0
  195. package/tui/crannia_tui/doctor.py +304 -0
  196. package/tui/crannia_tui/events.py +84 -0
  197. package/tui/crannia_tui/headless.py +152 -0
  198. package/tui/crannia_tui/i18n.py +164 -0
  199. package/tui/crannia_tui/locales/en.json +263 -0
  200. package/tui/crannia_tui/outcome.py +62 -0
  201. package/tui/crannia_tui/paths.py +181 -0
  202. package/tui/crannia_tui/probe.py +53 -0
  203. package/tui/crannia_tui/profile.py +169 -0
  204. package/tui/crannia_tui/render.py +209 -0
  205. package/tui/crannia_tui/settings.py +134 -0
  206. package/tui/crannia_tui/setup.py +380 -0
  207. package/tui/crannia_tui/term.py +116 -0
  208. package/tui/crannia_tui/ui.py +1236 -0
  209. package/tui/crannia_tui/update.py +389 -0
  210. package/vendor/ripgrep/COPYING.txt +3 -3
  211. package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
  212. package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
  213. package/vendor/ripgrep/README.md +51 -51
  214. package/vendor/ripgrep/UNLICENSE.txt +24 -24
  215. package/vendor/ripgrep/package-source.json +19 -19
  216. package/web/__main__.py +15 -0
  217. package/web/server.py +168 -19
  218. package/web/static/img/channel-telegram.svg +1 -1
  219. package/web/static/img/channel-wechat.svg +1 -1
  220. package/web/templates/logs.html +49 -49
  221. package/web/templates/models.html +46 -46
@@ -1,88 +1,93 @@
1
- kind: skill
2
- name: media_gen
3
- version: "0.1.0"
4
- draft: false
5
- load_strategy: on_demand
6
- description: "生图 —— 厂商直连(OpenAI 兼容 /images/generations),产出直接进聊天区"
7
- # 技能页卡片上那句短简介,与 `description` 分开。
8
- summary: "图视频拓展"
9
-
10
- # 契约声明。
11
- # · `api` 只在破坏性变更时 +1(加能力不算);
12
- # · `requires` 里的每个名字都是 `core/capabilities.py` 的 `CAPABILITIES` 里的一项,
13
- # 缺一项就不装载,并说清缺什么(不半残)。
14
- api: 1
15
- requires:
16
- - artifacts
17
- - artifacts.inline # 产物记录带 `inline`:大图给下载行而不是硬塞 <img>
18
- - workspace # 落点走 primary_output_dir(本 skill 唯一的写路径口径)
19
-
20
- # 本技能的工具不需要自动模式再判一遍:
21
- # · 它写的文件在工作区里、可逆、一次一个;
22
- # · 花费是用户自己的事,弹窗拦不住任何真风险,只会让"画张图"每次都停一下。
23
- # 没有这一行的话,每调一次都过分类器(要花钱的 token),而且会被
24
- # `test_auto_review` 当场点名(它要求每个走到第 ④ 层的工具做过决定)。
25
- review_input: none
26
-
27
- # 配置不在这里:凭据在 `data/media_gen_credentials.json`(不进 settings.json,
28
- # 也不进导出包)。这里只声明"有哪些条目、字段长什么样"给界面用。
29
- settings:
30
- # 整份配置住在这个文件里,所以凭据天然不进 `settings.json`(导出也不带走)。
31
- # 路径相对仓库根;`core/skill_settings.py` 是读写它的唯一入口(校验 + 打码 + 原子写)。
32
- storage: data/media_gen_credentials.json
33
- type: object
34
- properties:
35
- default:
36
- type: string
37
- title: 默认条目
38
- description: 不指定 entry 时用哪一条。
39
- entries:
40
- type: object
41
- title: 配置条目
42
- description: 键是条目名;值见下面三个字段。
43
- additionalProperties:
44
- type: object
45
- properties:
46
- type:
47
- type: string
48
- title: 类型
49
- enum: [vendor, local]
50
- default: vendor
51
- description: "vendor = 厂商直连(走 /images/generations);local = 本机 SD WebUI(走 /sdapi/v1/txt2img,只允许回环地址)"
52
- base_url:
53
- type: string
54
- title: 请求地址
55
- description: 请填写至API根
56
- model:
57
- type: string
58
- title: 模型名
59
- api_key:
60
- type: string
61
- title: API Key
62
- format: password
63
- x-secret: true # 声明为敏感:界面打码、不进 settings.json
64
-
65
- tools:
66
- - name: generate_image
67
- permission: write
68
- destructive: false
69
- output_kind: structured
70
- description: >
71
- 生成一张图片并保存到工作区(产出的图会直接显示在聊天里)。
72
- 需要先在生图配置里建一条(地址 + 模型名 + API Key)。
73
- 一次只生一张;要几张就调几次。提示词写清楚画面内容,
74
- 英文通常更稳。只用它来"从无到有画一张"——读图、改图、识别不走这里。
75
- inputSchema:
76
- $schema: "https://json-schema.org/draft/2020-12/schema"
77
- type: object
78
- properties:
79
- prompt:
80
- type: string
81
- description: "画面描述(要画什么、风格、构图)"
82
- size:
83
- type: string
84
- description: "尺寸,如 1024x1024 或 2K;不填用服务默认"
85
- entry:
86
- type: string
87
- description: "用哪一条配置;不填用默认那条"
88
- required: [prompt]
1
+ kind: skill
2
+ name: media_gen
3
+ version: "0.1.0"
4
+ draft: false
5
+ load_strategy: on_demand
6
+ description: "生图 —— 厂商直连(OpenAI 兼容 /images/generations),产出直接进聊天区"
7
+ # 技能页卡片上那句短简介,与 `description` 分开。
8
+ summary: "图视频拓展"
9
+
10
+ # 契约声明。
11
+ # · `api` 只在破坏性变更时 +1(加能力不算);
12
+ # · `requires` 里的每个名字都是 `core/capabilities.py` 的 `CAPABILITIES` 里的一项,
13
+ # 缺一项就不装载,并说清缺什么(不半残)。
14
+ api: 1
15
+ requires:
16
+ - artifacts
17
+ - artifacts.inline # 产物记录带 `inline`:大图给下载行而不是硬塞 <img>
18
+ - workspace # 落点走 primary_output_dir(本 skill 唯一的写路径口径)
19
+
20
+ # 配置需求(与上面那根轴不同:`requires` 缺了不装载,这个缺了照样装,
21
+ # 只是医生报 WARN、工具失败带 `/config media_gen` 路线)。
22
+ requires_config:
23
+ - media_gen
24
+
25
+ # 本技能的工具不需要自动模式再判一遍:
26
+ # · 它写的文件在工作区里、可逆、一次一个;
27
+ # · 花费是用户自己的事,弹窗拦不住任何真风险,只会让"画张图"每次都停一下。
28
+ # 没有这一行的话,每调一次都过分类器(要花钱的 token),而且会被
29
+ # `test_auto_review` 当场点名(它要求每个走到第 ④ 层的工具做过决定)。
30
+ review_input: none
31
+
32
+ # 配置不在这里:凭据在 `data/media_gen_credentials.json`(不进 settings.json,
33
+ # 也不进导出包)。这里只声明"有哪些条目、字段长什么样"给界面用。
34
+ settings:
35
+ # 整份配置住在这个文件里,所以凭据天然不进 `settings.json`(导出也不带走)。
36
+ # 路径相对仓库根;`core/skill_settings.py` 是读写它的唯一入口(校验 + 打码 + 原子写)。
37
+ storage: data/media_gen_credentials.json
38
+ type: object
39
+ properties:
40
+ default:
41
+ type: string
42
+ title: 默认条目
43
+ description: 不指定 entry 时用哪一条。
44
+ entries:
45
+ type: object
46
+ title: 配置条目
47
+ description: 键是条目名;值见下面三个字段。
48
+ additionalProperties:
49
+ type: object
50
+ properties:
51
+ type:
52
+ type: string
53
+ title: 类型
54
+ enum: [vendor, local]
55
+ default: vendor
56
+ description: "vendor = 厂商直连(走 /images/generations);local = 本机 SD WebUI(走 /sdapi/v1/txt2img,只允许回环地址)"
57
+ base_url:
58
+ type: string
59
+ title: 请求地址
60
+ description: 请填写至API根
61
+ model:
62
+ type: string
63
+ title: 模型名
64
+ api_key:
65
+ type: string
66
+ title: API Key
67
+ format: password
68
+ x-secret: true # 声明为敏感:界面打码、不进 settings.json
69
+
70
+ tools:
71
+ - name: generate_image
72
+ permission: write
73
+ destructive: false
74
+ output_kind: structured
75
+ description: >
76
+ 生成一张图片并保存到工作区(产出的图会直接显示在聊天里)。
77
+ 需要先在生图配置里建一条(地址 + 模型名 + API Key)。
78
+ 一次只生一张;要几张就调几次。提示词写清楚画面内容,
79
+ 英文通常更稳。只用它来"从无到有画一张"——读图、改图、识别不走这里。
80
+ inputSchema:
81
+ $schema: "https://json-schema.org/draft/2020-12/schema"
82
+ type: object
83
+ properties:
84
+ prompt:
85
+ type: string
86
+ description: "画面描述(要画什么、风格、构图)"
87
+ size:
88
+ type: string
89
+ description: "尺寸,如 1024x1024 或 2K;不填用服务默认"
90
+ entry:
91
+ type: string
92
+ description: "用哪一条配置;不填用默认那条"
93
+ required: [prompt]
@@ -1,127 +1,127 @@
1
- """Excel 数据验证(下拉列表 / 数值区间 / 日期 …)的读取侧。
2
-
3
- ## 这个文件补的是一个静默空转
4
-
5
- `read_excel_data(include_validation=True)` 里逐格 import 的就是本模块的
6
- `get_data_validation_for_cell`。若这个模块缺失、异常又被一句裸
7
- `except Exception` 吞掉:
8
-
9
- cell_data["validation"] = {"has_validation": False}
10
-
11
- `include_validation=True` 不会返回任何真规则,而且
12
- 没有任何东西会告诉你 —— 它给的是一个看起来像结论的 `False`。
13
-
14
- 这与写入侧的 `add_excel_data_validation` 是一对:那边能写、这边必须读得回来。
15
-
16
- ## 两条判据(别改)
17
-
18
- 1. 读不到 ≠ 没有。「这个格子没有验证规则」与
19
- 「我没查出来」是两件事,前者回 `None`,后者抛出去让调用方记成
20
- `has_validation=None` + `error`。不许静默塌成 `False`。
21
- 2. 本模块只读,不碰 `wb.save` / 任何写标记(`read` 档的实现不许含写标记,
22
- `tests/test_permission_tiers.py` 有 AST 读者)。
23
- """
24
-
25
- from __future__ import annotations
26
-
27
- from typing import Any, Dict, List, Optional
28
-
29
- from openpyxl.utils.cell import column_index_from_string, coordinate_from_string
30
-
31
-
32
- def get_data_validation_for_cell(
33
- worksheet, cell_address: str
34
- ) -> Optional[Dict[str, Any]]:
35
- """`cell_address`(如 `"C2"`)上的验证规则。
36
-
37
- * 有,返回规则字典(`has_validation: True` + 类型 / 取值 / 提示语)
38
- * 没有,返回 `None`
39
- * 查不出来,抛异常(调用方记成未知,不许塌成 False)
40
- """
41
- col_letter, row = coordinate_from_string(cell_address)
42
- col_idx = column_index_from_string(col_letter)
43
-
44
- for dv in worksheet.data_validations.dataValidation:
45
- if _covers(row, col_idx, dv):
46
- return _extract(dv, cell_address, worksheet)
47
- return None
48
-
49
-
50
- def _covers(row: int, col: int, dv) -> bool:
51
- """这个格子在不在这条规则的 `sqref` 覆盖范围里。"""
52
- for rng in dv.sqref.ranges:
53
- if rng.min_row <= row <= rng.max_row and rng.min_col <= col <= rng.max_col:
54
- return True
55
- return False
56
-
57
-
58
- def _extract(dv, cell_address: str, worksheet) -> Dict[str, Any]:
59
- """把一条 `DataValidation` 摊成给模型看的字典。
60
-
61
- 取不到的字段不写,而不是写一个空串 —— 空串看起来像"这条规则就是空的"。
62
- """
63
- info: Dict[str, Any] = {
64
- "cell": cell_address,
65
- "has_validation": True,
66
- "validation_type": dv.type,
67
- "allow_blank": bool(dv.allowBlank),
68
- }
69
- if dv.operator:
70
- info["operator"] = dv.operator
71
- if dv.prompt:
72
- info["prompt"] = dv.prompt
73
- if dv.promptTitle:
74
- info["prompt_title"] = dv.promptTitle
75
- if dv.error:
76
- info["error_message"] = dv.error
77
- if dv.errorTitle:
78
- info["error_title"] = dv.errorTitle
79
-
80
- if dv.type == "list" and dv.formula1:
81
- info["allowed_values"] = _list_values(dv.formula1, worksheet)
82
- elif dv.formula1:
83
- info["formula1"] = dv.formula1
84
- if dv.formula2:
85
- info["formula2"] = dv.formula2
86
-
87
- return info
88
-
89
-
90
- def _list_values(formula: str, worksheet) -> List[str]:
91
- """把 `list` 类型的 `formula1` 摊成候选值。
92
-
93
- 两种写法都认(与 `add_excel_data_validation` 写出去的形状一致):
94
- * 字面选项 "合格,不合格" —— 逗号分隔
95
- * 区域引用 `=$E$1:$E$5` —— 去表里把值读出来
96
- """
97
- raw = str(formula).strip()
98
- # 去掉包裹的双引号;openpyxl 存字面选项时要求带引号
99
- if len(raw) >= 2 and raw[0] == raw[-1] == '"':
100
- raw = raw[1:-1]
101
-
102
- # 字面选项:逗号分隔
103
- if ":" not in raw and not raw.startswith("$") and not raw.startswith("="):
104
- return [v.strip() for v in raw.split(",") if v.strip()]
105
-
106
- # 区域引用:去表里读
107
- ref = raw[1:] if raw.startswith("=") else raw
108
- try:
109
- cells = worksheet[ref]
110
- except Exception:
111
- # 区域解不开时不假装空:把引用原样回给模型,让它自己判断
112
- return [f"范围: {ref}(解析不了,请自行查看)"]
113
-
114
- flat = []
115
- if hasattr(cells, "value"): # 单个格子
116
- if cells.value is not None:
117
- flat.append(str(cells.value))
118
- else:
119
- for item in cells:
120
- if hasattr(item, "value"): # 一维
121
- if item.value is not None:
122
- flat.append(str(item.value))
123
- else: # 二维
124
- for c in item:
125
- if c.value is not None:
126
- flat.append(str(c.value))
127
- return flat if flat else [f"范围: {ref}(里头没有值)"]
1
+ """Excel 数据验证(下拉列表 / 数值区间 / 日期 …)的读取侧。
2
+
3
+ ## 这个文件补的是一个静默空转
4
+
5
+ `read_excel_data(include_validation=True)` 里逐格 import 的就是本模块的
6
+ `get_data_validation_for_cell`。若这个模块缺失、异常又被一句裸
7
+ `except Exception` 吞掉:
8
+
9
+ cell_data["validation"] = {"has_validation": False}
10
+
11
+ `include_validation=True` 不会返回任何真规则,而且
12
+ 没有任何东西会告诉你 —— 它给的是一个看起来像结论的 `False`。
13
+
14
+ 这与写入侧的 `add_excel_data_validation` 是一对:那边能写、这边必须读得回来。
15
+
16
+ ## 两条判据(别改)
17
+
18
+ 1. 读不到 ≠ 没有。「这个格子没有验证规则」与
19
+ 「我没查出来」是两件事,前者回 `None`,后者抛出去让调用方记成
20
+ `has_validation=None` + `error`。不许静默塌成 `False`。
21
+ 2. 本模块只读,不碰 `wb.save` / 任何写标记(`read` 档的实现不许含写标记,
22
+ `tests/test_permission_tiers.py` 有 AST 读者)。
23
+ """
24
+
25
+ from __future__ import annotations
26
+
27
+ from typing import Any, Dict, List, Optional
28
+
29
+ from openpyxl.utils.cell import column_index_from_string, coordinate_from_string
30
+
31
+
32
+ def get_data_validation_for_cell(
33
+ worksheet, cell_address: str
34
+ ) -> Optional[Dict[str, Any]]:
35
+ """`cell_address`(如 `"C2"`)上的验证规则。
36
+
37
+ * 有,返回规则字典(`has_validation: True` + 类型 / 取值 / 提示语)
38
+ * 没有,返回 `None`
39
+ * 查不出来,抛异常(调用方记成未知,不许塌成 False)
40
+ """
41
+ col_letter, row = coordinate_from_string(cell_address)
42
+ col_idx = column_index_from_string(col_letter)
43
+
44
+ for dv in worksheet.data_validations.dataValidation:
45
+ if _covers(row, col_idx, dv):
46
+ return _extract(dv, cell_address, worksheet)
47
+ return None
48
+
49
+
50
+ def _covers(row: int, col: int, dv) -> bool:
51
+ """这个格子在不在这条规则的 `sqref` 覆盖范围里。"""
52
+ for rng in dv.sqref.ranges:
53
+ if rng.min_row <= row <= rng.max_row and rng.min_col <= col <= rng.max_col:
54
+ return True
55
+ return False
56
+
57
+
58
+ def _extract(dv, cell_address: str, worksheet) -> Dict[str, Any]:
59
+ """把一条 `DataValidation` 摊成给模型看的字典。
60
+
61
+ 取不到的字段不写,而不是写一个空串 —— 空串看起来像"这条规则就是空的"。
62
+ """
63
+ info: Dict[str, Any] = {
64
+ "cell": cell_address,
65
+ "has_validation": True,
66
+ "validation_type": dv.type,
67
+ "allow_blank": bool(dv.allowBlank),
68
+ }
69
+ if dv.operator:
70
+ info["operator"] = dv.operator
71
+ if dv.prompt:
72
+ info["prompt"] = dv.prompt
73
+ if dv.promptTitle:
74
+ info["prompt_title"] = dv.promptTitle
75
+ if dv.error:
76
+ info["error_message"] = dv.error
77
+ if dv.errorTitle:
78
+ info["error_title"] = dv.errorTitle
79
+
80
+ if dv.type == "list" and dv.formula1:
81
+ info["allowed_values"] = _list_values(dv.formula1, worksheet)
82
+ elif dv.formula1:
83
+ info["formula1"] = dv.formula1
84
+ if dv.formula2:
85
+ info["formula2"] = dv.formula2
86
+
87
+ return info
88
+
89
+
90
+ def _list_values(formula: str, worksheet) -> List[str]:
91
+ """把 `list` 类型的 `formula1` 摊成候选值。
92
+
93
+ 两种写法都认(与 `add_excel_data_validation` 写出去的形状一致):
94
+ * 字面选项 "合格,不合格" —— 逗号分隔
95
+ * 区域引用 `=$E$1:$E$5` —— 去表里把值读出来
96
+ """
97
+ raw = str(formula).strip()
98
+ # 去掉包裹的双引号;openpyxl 存字面选项时要求带引号
99
+ if len(raw) >= 2 and raw[0] == raw[-1] == '"':
100
+ raw = raw[1:-1]
101
+
102
+ # 字面选项:逗号分隔
103
+ if ":" not in raw and not raw.startswith("$") and not raw.startswith("="):
104
+ return [v.strip() for v in raw.split(",") if v.strip()]
105
+
106
+ # 区域引用:去表里读
107
+ ref = raw[1:] if raw.startswith("=") else raw
108
+ try:
109
+ cells = worksheet[ref]
110
+ except Exception:
111
+ # 区域解不开时不假装空:把引用原样回给模型,让它自己判断
112
+ return [f"范围: {ref}(解析不了,请自行查看)"]
113
+
114
+ flat = []
115
+ if hasattr(cells, "value"): # 单个格子
116
+ if cells.value is not None:
117
+ flat.append(str(cells.value))
118
+ else:
119
+ for item in cells:
120
+ if hasattr(item, "value"): # 一维
121
+ if item.value is not None:
122
+ flat.append(str(item.value))
123
+ else: # 二维
124
+ for c in item:
125
+ if c.value is not None:
126
+ flat.append(str(c.value))
127
+ return flat if flat else [f"范围: {ref}(里头没有值)"]