Skip to content
Myr

🔨 Add Typer script to generate example variants for Python files (#1452)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent e777d1d · Browse files · 1 file+37 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+37 added−0 removed

  • other:+37 added−0 removed

1 file+37 added−0 removed

modified:scripts/docs.py+37 added
4 unmodified lines
5from http.server import HTTPServer, SimpleHTTPRequestHandler6from pathlib import Path78import typer9from ruff.__main__ import find_ruff_bin1011logging.basicConfig(level=logging.INFO)1213mkdocs_name = "mkdocs.yml"
103 unmodified lines
117 typer.echo("Serving at: http://127.0.0.1:8008")118 server.serve_forever()119120121@app.command()122def generate_docs_src_versions_for_file(file_path: Path) -> None:123 target_versions = ["py39", "py310"]124 base_content = file_path.read_text(encoding="utf-8")125 previous_content = {base_content}126 for target_version in target_versions:127 version_result = subprocess.run(128 [129 find_ruff_bin(),130 "check",131 "--target-version",132 target_version,133 "--fix",134 "--unsafe-fixes",135 "-",136 ],137 input=base_content.encode("utf-8"),138 capture_output=True,139 )140 content_target = version_result.stdout.decode("utf-8")141 format_result = subprocess.run(142 [find_ruff_bin(), "format", "-"],143 input=content_target.encode("utf-8"),144 capture_output=True,145 )146 content_format = format_result.stdout.decode("utf-8")147 if content_format in previous_content:148 continue149 previous_content.add(content_format)150 version_file = file_path.with_name(151 file_path.name.replace(".py", f"_{target_version}.py")152 )153 logging.info(f"Writing to {version_file}")154 version_file.write_text(content_format, encoding="utf-8")155156157if __name__ == "__main__":158 app()