Skip to content
Myr

docs.py

This file

Path

scripts/docs.py

158 lines · 4.6 KiB · Python

Last changed by

🔨 Add Typer script to generate example variants for Python files (#1452)Sebastián Ramírezbefore adoption4b302df

History of this file

Take it

The walk stopped after 200 commits: lines older than 9cdc5b5 point at it.

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 1–8why, one step back
1import logging
2import os3import re4import subprocess5from http.server import HTTPServer, SimpleHTTPRequestHandler6from pathlib import Path78import typer

🔨 Add Typer script to generate example variants for Python files (#1452)Sebastián Ramírezbefore adoption4b302df

line 9why, one step back
9from ruff.__main__ import find_ruff_bin

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 10–20why, one step back
10
11logging.basicConfig(level=logging.INFO)1213mkdocs_name = "mkdocs.yml"14en_docs_path = Path("")1516app = typer.Typer()171819@app.callback()20def callback() -> None:

🔧 Upgrade Material for MkDocs and remove insiders (#1416)Sebastián Ramírezbefore adoptionf327548

line 21why, one step back
21 # For MacOS with Cairo

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 22–118why, one step back
22 os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib"
232425def generate_readme_content() -> str:26 en_index = en_docs_path / "docs" / "index.md"27 content = en_index.read_text("utf-8")28 match_pre = re.search(r"</style>\n\n", content)29 if not match_pre:30 raise RuntimeError("Couldn't find pre section (<style>) in index.md")31 frontmatter_end = match_pre.end()32 new_content = content[frontmatter_end:]33 # Remove content between <!-- only-mkdocs --> and <!-- /only-mkdocs -->34 new_content = re.sub(35 r"<!-- only-mkdocs -->.*?<!-- /only-mkdocs -->",36 "",37 new_content,38 flags=re.DOTALL,39 )40 return new_content414243@app.command()44def generate_readme() -> None:45 """46 Generate README.md content from main index.md47 """48 typer.echo("Generating README")49 readme_path = Path("README.md")50 new_content = generate_readme_content()51 readme_path.write_text(new_content, encoding="utf-8")525354@app.command()55def verify_readme() -> None:56 """57 Verify README.md content from main index.md58 """59 typer.echo("Verifying README")60 readme_path = Path("README.md")61 generated_content = generate_readme_content()62 readme_content = readme_path.read_text("utf-8")63 if generated_content != readme_content:64 typer.secho(65 "README.md outdated from the latest index.md", color=typer.colors.RED66 )67 raise typer.Abort()68 typer.echo("Valid README ✅")697071@app.command()72def live(dirty: bool = False) -> None:73 """74 Serve with livereload a docs site for a specific language.7576 This only shows the actual translated files, not the placeholders created with77 build-all.7879 Takes an optional LANG argument with the name of the language to serve, by default80 en.81 """82 # Enable line numbers during local development to make it easier to highlight83 args = ["mkdocs", "serve", "--dev-addr", "127.0.0.1:8008"]84 if dirty:85 args.append("--dirty")86 subprocess.run(args, env={**os.environ, "LINENUMS": "true"}, check=True)878889@app.command()90def build() -> None:91 """92 Build the docs.93 """94 print("Building docs")95 subprocess.run(["mkdocs", "build"], check=True)96 typer.secho("Successfully built docs", color=typer.colors.GREEN)979899@app.command()100def serve() -> None:101 """102 A quick server to preview a built site.103104 For development, prefer the command live (or just mkdocs serve).105106 This is here only to preview the documentation site.107108 Make sure you run the build command first.109 """110 typer.echo("Warning: this is a very simple server.")111 typer.echo("For development, use the command live instead.")112 typer.echo("This is here only to preview the documentation site.")113 typer.echo("Make sure you run the build command first.")114 os.chdir("site")115 server_address = ("", 8008)116 server = HTTPServer(server_address, SimpleHTTPRequestHandler)117 typer.echo("Serving at: http://127.0.0.1:8008")118 server.serve_forever()

🔨 Add Typer script to generate example variants for Python files (#1452)Sebastián Ramírezbefore adoption4b302df

lines 119–154why, one step back
119
120121@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")

📝 Update release notesgithub-actions[bot]before adoption9cdc5b5

lines 155–158why, one step back
155
156157if __name__ == "__main__":158 app()