|
| 1 | +"""Automatically create PEP 0 (the PEP index), |
| 2 | +
|
| 3 | +This file generates and writes the PEP index to disk, ready for later |
| 4 | +processing by Sphinx. Firstly, we parse the individual PEP files, getting the |
| 5 | +RFC2822 header, and parsing and then validating that metadata. |
| 6 | +
|
| 7 | +After collecting and validating all the PEP data, the creation of the index |
| 8 | +itself is in three steps: |
| 9 | +
|
| 10 | + 1. Output static text. |
| 11 | + 2. Format an entry for the PEP. |
| 12 | + 3. Output the PEP (both by the category and numerical index). |
| 13 | +
|
| 14 | +We then add the newly created PEP 0 file to two Sphinx environment variables |
| 15 | +to allow it to be processed as normal. |
| 16 | +
|
| 17 | +""" |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import csv |
| 21 | +import re |
| 22 | +from pathlib import Path |
| 23 | +from typing import TYPE_CHECKING |
| 24 | + |
| 25 | +from pep_sphinx_extensions.pep_zero_generator import pep_0_parser |
| 26 | +from pep_sphinx_extensions.pep_zero_generator import pep_0_writer |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from sphinx.application import Sphinx |
| 30 | + from sphinx.environment import BuildEnvironment |
| 31 | + |
| 32 | + |
| 33 | +def create_pep_zero(_: Sphinx, env: BuildEnvironment, docnames: list[str]) -> None: |
| 34 | + # Sphinx app object is unneeded by this function |
| 35 | + |
| 36 | + # Read from root directory |
| 37 | + path = Path(".") |
| 38 | + |
| 39 | + pep_zero_filename = "pep-0000" |
| 40 | + peps: list[pep_0_parser.PEP] = [] |
| 41 | + pep_pat = re.compile(r"pep-\d{4}") # Path.match() doesn't support regular expressions |
| 42 | + title_length = pep_0_writer.title_length |
| 43 | + |
| 44 | + # AUTHORS.csv is an exception file for PEP0 name parsing |
| 45 | + with open("AUTHORS.csv", "r", encoding="utf-8") as f: |
| 46 | + read = csv.DictReader(f, quotechar='"', skipinitialspace=True) |
| 47 | + author_exception_data = {} |
| 48 | + for line in read: |
| 49 | + full_name = line.pop("Full Name").strip() |
| 50 | + details = {k.strip(): v.strip() for k, v in line.items()} |
| 51 | + author_exception_data[full_name] = details |
| 52 | + |
| 53 | + for file_path in path.iterdir(): |
| 54 | + if not file_path.is_file(): |
| 55 | + continue # Skip directories etc. |
| 56 | + if file_path.match("pep-0000*"): |
| 57 | + continue # Skip pre-existing PEP 0 files |
| 58 | + if pep_pat.match(str(file_path)) and file_path.suffix in {".txt", ".rst"}: |
| 59 | + pep = pep_0_parser.PEP(path.joinpath(file_path).absolute(), author_exception_data, title_length) |
| 60 | + peps.append(pep) |
| 61 | + peps.sort(key=lambda pep: pep.number) |
| 62 | + |
| 63 | + pep_writer = pep_0_writer.PEPZeroWriter() |
| 64 | + pep0_text = pep_writer.write_pep0(peps) |
| 65 | + Path(f"{pep_zero_filename}.rst").write_text(pep0_text, encoding="utf-8") |
| 66 | + |
| 67 | + # Add to files for builder |
| 68 | + docnames.insert(1, pep_zero_filename) |
| 69 | + # Add to files for writer |
| 70 | + env.found_docs.add(pep_zero_filename) |
0 commit comments