"""Offline VS Code Extensions Marketplace metadata & Python hello.py verifier for Ch16."""
import ast
import json
import re
import subprocess
import sys
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code Extensions & Python IntelliSense verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: Marketplace extension ID validation, 6 categories, and .vscode/extensions.json
categories = ["编程语言支持", "调试工具", "版本控制", "主题和美化", "工具集成", "辅助开发"]
ext_json = {
"recommendations": [
"ms-python.python",
"eamodio.gitlens",
"esbenp.prettier-vscode",
]
}
id_pat = re.compile(r"^[a-z0-9-]+\.[a-z0-9-]+$", re.I)
all_valid = all(bool(id_pat.match(e)) for e in ext_json["recommendations"])
cases.append(
check(
"marketplace_catalog_and_extension_id",
"Verify 6 VS Code extension categories and valid publisher.name identifiers in extensions.json",
{
"category_count": 6,
"recommendation_count": 3,
"all_ids_valid": True,
"includes_ms_python": True,
},
{
"category_count": len(categories),
"recommendation_count": len(ext_json["recommendations"]),
"all_ids_valid": all_valid,
"includes_ms_python": "ms-python.python" in ext_json["recommendations"],
},
)
)
# Case 2: Write and run hello.py + extract AST function signature for IntelliSense
hello_code = (
"def say_hello(name):\n"
' print("Hello, " + name)\n\n'
'say_hello("VS Code")\n'
)
tree = ast.parse(hello_code)
funcs = [
{"name": n.name, "args": [a.arg for a in n.args.args]}
for n in tree.body
if isinstance(n, ast.FunctionDef)
]
with tempfile.TemporaryDirectory() as tmp:
py_file = Path(tmp) / "hello.py"
py_file.write_text(hello_code, encoding="utf-8")
proc = subprocess.run(
[sys.executable, "-I", str(py_file)],
capture_output=True,
encoding="utf-8",
timeout=15,
)
cases.append(
check(
"python_extension_hello_py_exec",
"Verify hello.py execution output and Python AST symbol extraction for IntelliSense",
{
"function_name": "say_hello",
"function_args": ["name"],
"returncode": 0,
"stdout": "Hello, VS Code",
},
{
"function_name": funcs[0]["name"],
"function_args": funcs[0]["args"],
"returncode": proc.returncode,
"stdout": proc.stdout.strip(),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-extensions-lab/explore-extensions.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-extensions.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2209.zip