蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

读取本机扩展清单核对上下文入口

PYTHONWindows、CPython 3.12.14、VS Code 扩展 26.903.61454。ide_lab.py
ide_lab.py56 行
"""Inspect an installed extension's public command declarations without launching it."""
from hashlib import sha256
import json
from pathlib import Path
import sys
import tomllib


def main():
    if len(sys.argv) > 1:
        manifest = Path(sys.argv[1])
    else:
        candidates = sorted((Path.home() / ".vscode/extensions").glob("openai.chatgpt-*/package.json"))
        if len(candidates) != 1:
            raise SystemExit("Pass the package.json path for the installed Codex extension.")
        manifest = candidates[0]

    raw = manifest.read_bytes()
    extension = json.loads(raw)
    contribution = extension["contributes"]
    commands = {command["command"] for command in contribution["commands"]}
    context_commands = {item["command"] for item in contribution["menus"].get("editor/context", [])}
    bindings = [item for item in contribution.get("keybindings", []) if item["command"].startswith("chatgpt.")]
    cases = []

    def check(name, data, operation, expected, actual):
        assert expected == actual, (name, expected, actual)
        cases.append(dict(name="ide_" + name, input=data, operation=operation, expected=expected, actual=actual))

    check("identity", "package.json publisher and name", "Read extension package metadata", "openai.chatgpt",
          extension["publisher"] + "." + extension["name"])
    ids = ["chatgpt.openSidebar", "chatgpt.addToThread", "chatgpt.addFileToThread"]
    check("commands", ids, "Find sidebar, selected range, and selected file commands", [True, True, True],
          [command in commands for command in ids])
    check("context", ["chatgpt.addToThread", "chatgpt.addFileToThread"],
          "Distinguish the commands contributed to this editor context menu", [True, False],
          [command in context_commands for command in ids[1:]])
    old = ["cmd+shift+c", "cmd+shift+enter", "cmd+shift+r", "cmd+shift+e"]
    mac = {item.get("mac", item["key"]).lower() for item in bindings}
    check("old_keys", old, "Compare the four historical macOS keys with package-declared defaults",
          [False] * len(old), [key in mac for key in old])
    keys = [item for item in bindings if item["command"] == "chatgpt.newChat"]
    check("new_chat_keys", "chatgpt.newChat", "Read defaults and the required context condition",
          [["ctrl+n", "cmd+n", "chatgpt.supportsNewChatKeyShortcut"]],
          [[item["key"], item["mac"], item["when"]] for item in keys])
    source = 'model = "sample-model"\nfile_opener = "vscode"\n'
    config = tomllib.loads(source)
    check("toml", source, "Parse a standalone configuration example with Python",
          ["sample-model", "vscode"], [config["model"], config["file_opener"]])
    print(json.dumps(dict(version=dict(python=sys.version.split()[0], extension=extension["version"]),
                          manifest_sha256=sha256(raw).hexdigest(), cases=cases)))


if __name__ == "__main__":
    main()
本例需在 Windows、CPython 3.12.14、VS Code 扩展 26.903.61454。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows、CPython 3.12.14、VS Code 扩展 26.903.61454。

入口文件:projects/codex-ide/ide_lab.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 ide_lab.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows、CPython 3.12.14、VS Code 扩展 26.903.61454。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a107.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习