Skip to content

Commit

Permalink
Infra: Refactor peps.json logic into PEP class (#2585)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored Jun 8, 2022
1 parent f7c9e62 commit e92d085
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
19 changes: 19 additions & 0 deletions pep_sphinx_extensions/pep_zero_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ def details(self) -> dict[str, str | int]:
"authors": ", ".join(author.nick for author in self.authors),
}

@property
def full_details(self) -> dict[str, str]:
"""Returns all headers of the PEP as a dict."""
return {
"title": self.title,
"authors": ", ".join(author.nick for author in self.authors),
"discussions_to": self.discussions_to,
"status": self.status,
"type": self.pep_type,
"created": self.created,
"python_version": self.python_version,
"post_history": self.post_history,
"resolution": self.resolution,
"requires": self.requires,
"replaces": self.replaces,
"superseded_by": self.superseded_by,
"url": f"https://peps.python.org/pep-{self.number:0>4}/",
}


def _raise_pep_error(pep: PEP, msg: str, pep_num: bool = False) -> None:
if pep_num:
Expand Down
31 changes: 7 additions & 24 deletions pep_sphinx_extensions/pep_zero_generator/pep_index_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,7 @@


def create_pep_json(peps: list[parser.PEP]) -> str:
pep_dict = {
pep.number: {
"title": pep.title,
"authors": ", ".join(pep.authors.nick for pep.authors in pep.authors),
"discussions_to": pep.discussions_to,
"status": pep.status,
"type": pep.pep_type,
"created": pep.created,
"python_version": pep.python_version,
"post_history": pep.post_history,
"resolution": pep.resolution,
"requires": pep.requires,
"replaces": pep.replaces,
"superseded_by": pep.superseded_by,
"url": f"https://peps.python.org/pep-{pep.number:0>4}/",
}
for pep in sorted(peps)
}
return json.dumps(pep_dict, indent=1)
return json.dumps({pep.number: pep.full_details for pep in peps}, indent=1)


def create_pep_zero(app: Sphinx, env: BuildEnvironment, docnames: list[str]) -> None:
Expand All @@ -77,7 +59,9 @@ def create_pep_zero(app: Sphinx, env: BuildEnvironment, docnames: list[str]) ->
pep = parser.PEP(path.joinpath(file_path).absolute(), authors_overrides)
peps.append(pep)

pep0_text = writer.PEPZeroWriter().write_pep0(sorted(peps))
peps = sorted(peps)

pep0_text = writer.PEPZeroWriter().write_pep0(peps)
pep0_path = Path(f"{pep_zero_filename}.rst")
pep0_path.write_text(pep0_text, encoding="utf-8")

Expand All @@ -89,7 +73,6 @@ def create_pep_zero(app: Sphinx, env: BuildEnvironment, docnames: list[str]) ->
env.found_docs.add(pep_zero_filename)

# Create peps.json
pep0_json = create_pep_json(peps)
out_dir = Path(app.outdir) / "api"
out_dir.mkdir(exist_ok=True)
Path(out_dir, "peps.json").write_text(pep0_json, encoding="utf-8")
json_path = Path(app.outdir, "api", "peps.json").resolve()
json_path.parent.mkdir(exist_ok=True)
json_path.write_text(create_pep_json(peps), encoding="utf-8")

0 comments on commit e92d085

Please sign in to comment.