Files
aigc-home/docs/hel-892/krea-story-document-evidence/schema_extract.py
T

35 lines
1.6 KiB
Python

"""Read literal declarations as AST text, without importing upstream."""
import ast
def schema_for(node):
("Extract declarations a" + "s text without evaluat" + "ing any upstream expre" + "ssion.")
result = {}
for d in ast.walk(node):
if isinstance(d, ast.Dict):
for k, v in zip(d.keys, d.values, strict=True):
if (
isinstance(k, ast.Constant)
and isinstance(k.value, str)
and isinstance(v, ast.Tuple)
):
default = "无显式default;不从源现值或声明输入推补schema默认"
for opts in v.elts[1:]:
if isinstance(opts, ast.Dict):
for dk, dv in zip(opts.keys, opts.values, strict=True):
if isinstance(dk, ast.Constant) and dk.value == "default":
default = ast.unparse(dv)
result[k.value] = (default, ast.unparse(v))
if isinstance(d, ast.Call) and isinstance(d.func, ast.Attribute) and d.func.attr == "Input":
arg = (
d.args[0] if d.args else next((k.value for k in d.keywords if k.arg == "id"), None)
)
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
default = next(
(ast.unparse(k.value) for k in d.keywords if k.arg == "default"),
"无显式default;枚举条件独立,不以现值认证默认",
)
result[arg.value] = (default, ast.unparse(d))
return result