|
| 1 | +"""Generate schema for tox configuration, respecting the current plugins.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import sys |
| 7 | +import typing |
| 8 | +from pathlib import Path |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +import packaging.requirements |
| 12 | +import packaging.version |
| 13 | + |
| 14 | +import tox.config.set_env |
| 15 | +import tox.config.types |
| 16 | +import tox.tox_env.python.pip.req_file |
| 17 | +from tox.plugin import impl |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from tox.config.cli.parser import ToxParser |
| 21 | + from tox.config.sets import ConfigSet |
| 22 | + from tox.session.state import State |
| 23 | + |
| 24 | + |
| 25 | +@impl |
| 26 | +def tox_add_option(parser: ToxParser) -> None: |
| 27 | + parser.add_command("schema", [], "Generate schema for tox configuration", gen_schema) |
| 28 | + |
| 29 | + |
| 30 | +def _process_type(of_type: typing.Any) -> dict[str, typing.Any]: # noqa: PLR0911 |
| 31 | + if of_type in { |
| 32 | + Path, |
| 33 | + str, |
| 34 | + packaging.version.Version, |
| 35 | + packaging.requirements.Requirement, |
| 36 | + tox.tox_env.python.pip.req_file.PythonDeps, |
| 37 | + }: |
| 38 | + return {"type": "string"} |
| 39 | + if of_type is bool: |
| 40 | + return {"type": "boolean"} |
| 41 | + if of_type is float: |
| 42 | + return {"type": "number"} |
| 43 | + if of_type is tox.config.types.Command: |
| 44 | + return {"type": "array", "items": {"#ref": "#/definitions/command"}} |
| 45 | + if of_type is tox.config.types.EnvList: |
| 46 | + return {"type": "array", "items": {"type": "string"}} |
| 47 | + if typing.get_origin(of_type) in {list, set}: |
| 48 | + return {"type": "array", "items": _process_type(typing.get_args(of_type)[0])} |
| 49 | + if of_type is tox.config.set_env.SetEnv: |
| 50 | + return { |
| 51 | + "type": "object", |
| 52 | + "additionalProperties": {"type": "array", "items": {"type": "string"}}, |
| 53 | + } |
| 54 | + if typing.get_origin(of_type) is dict: |
| 55 | + return { |
| 56 | + "type": "object", |
| 57 | + "additionalProperties": {"type": "array", "items": _process_type(typing.get_args(of_type)[1])}, |
| 58 | + } |
| 59 | + msg = f"Unknown type: {of_type}" |
| 60 | + raise ValueError(msg) |
| 61 | + |
| 62 | + |
| 63 | +def _get_schema(conf: ConfigSet, path: str) -> dict[str, dict[str, typing.Any]]: |
| 64 | + properties = {} |
| 65 | + for x in conf.get_configs(): |
| 66 | + name, *aliases = x.keys |
| 67 | + of_type = getattr(x, "of_type", None) |
| 68 | + if of_type is None: |
| 69 | + continue |
| 70 | + desc = getattr(x, "desc", None) |
| 71 | + try: |
| 72 | + properties[name] = {**_process_type(of_type), "description": desc} |
| 73 | + except ValueError: |
| 74 | + print(name, "has unrecoginsed type:", of_type, file=sys.stderr) # noqa: T201 |
| 75 | + for alias in aliases: |
| 76 | + properties[alias] = {"$ref": f"{path}/{name}"} |
| 77 | + return properties |
| 78 | + |
| 79 | + |
| 80 | +def gen_schema(state: State) -> int: |
| 81 | + core = state.conf.core |
| 82 | + |
| 83 | + properties = _get_schema(core, path="#/properties") |
| 84 | + |
| 85 | + env_properties = _get_schema(state.envs["3.13"].conf, path="#/properties/env_run_base/properties") |
| 86 | + |
| 87 | + json_schema = { |
| 88 | + "$schema": "http://json-schema.org/draft-07/schema", |
| 89 | + "$id": "https://github.com/tox-dev/tox/blob/main/src/tox/util/tox.schema.json", |
| 90 | + "type": "object", |
| 91 | + "properties": { |
| 92 | + **properties, |
| 93 | + "env_run_base": {"type": "object", "properties": env_properties}, |
| 94 | + "env_pkg_base": {"$ref": "#/properties/env_run_base"}, |
| 95 | + "env": {"type": "object", "patternProperties": {"^.*$": {"$ref": "#/properties/env_run_base"}}}, |
| 96 | + }, |
| 97 | + "#definitions": { |
| 98 | + "command": { |
| 99 | + "oneOf": [ |
| 100 | + {"type": "string"}, |
| 101 | + { |
| 102 | + "type": "object", |
| 103 | + "properties": { |
| 104 | + "replace": {"type": "string"}, |
| 105 | + "default": {"type": "array", "items": {"type": "string"}}, |
| 106 | + "extend": {"type": "boolean"}, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ], |
| 110 | + } |
| 111 | + }, |
| 112 | + } |
| 113 | + print(json.dumps(json_schema, indent=2)) # noqa: T201 |
| 114 | + return 0 |
0 commit comments