Skip to content

Commit e92d085

Browse files
authored
Infra: Refactor peps.json logic into PEP class (#2585)
1 parent f7c9e62 commit e92d085

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

pep_sphinx_extensions/pep_zero_generator/parser.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ def details(self) -> dict[str, str | int]:
127127
"authors": ", ".join(author.nick for author in self.authors),
128128
}
129129

130+
@property
131+
def full_details(self) -> dict[str, str]:
132+
"""Returns all headers of the PEP as a dict."""
133+
return {
134+
"title": self.title,
135+
"authors": ", ".join(author.nick for author in self.authors),
136+
"discussions_to": self.discussions_to,
137+
"status": self.status,
138+
"type": self.pep_type,
139+
"created": self.created,
140+
"python_version": self.python_version,
141+
"post_history": self.post_history,
142+
"resolution": self.resolution,
143+
"requires": self.requires,
144+
"replaces": self.replaces,
145+
"superseded_by": self.superseded_by,
146+
"url": f"https://peps.python.org/pep-{self.number:0>4}/",
147+
}
148+
130149

131150
def _raise_pep_error(pep: PEP, msg: str, pep_num: bool = False) -> None:
132151
if pep_num:

pep_sphinx_extensions/pep_zero_generator/pep_index_generator.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,7 @@
3232

3333

3434
def create_pep_json(peps: list[parser.PEP]) -> str:
35-
pep_dict = {
36-
pep.number: {
37-
"title": pep.title,
38-
"authors": ", ".join(pep.authors.nick for pep.authors in pep.authors),
39-
"discussions_to": pep.discussions_to,
40-
"status": pep.status,
41-
"type": pep.pep_type,
42-
"created": pep.created,
43-
"python_version": pep.python_version,
44-
"post_history": pep.post_history,
45-
"resolution": pep.resolution,
46-
"requires": pep.requires,
47-
"replaces": pep.replaces,
48-
"superseded_by": pep.superseded_by,
49-
"url": f"https://peps.python.org/pep-{pep.number:0>4}/",
50-
}
51-
for pep in sorted(peps)
52-
}
53-
return json.dumps(pep_dict, indent=1)
35+
return json.dumps({pep.number: pep.full_details for pep in peps}, indent=1)
5436

5537

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

80-
pep0_text = writer.PEPZeroWriter().write_pep0(sorted(peps))
62+
peps = sorted(peps)
63+
64+
pep0_text = writer.PEPZeroWriter().write_pep0(peps)
8165
pep0_path = Path(f"{pep_zero_filename}.rst")
8266
pep0_path.write_text(pep0_text, encoding="utf-8")
8367

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

9175
# Create peps.json
92-
pep0_json = create_pep_json(peps)
93-
out_dir = Path(app.outdir) / "api"
94-
out_dir.mkdir(exist_ok=True)
95-
Path(out_dir, "peps.json").write_text(pep0_json, encoding="utf-8")
76+
json_path = Path(app.outdir, "api", "peps.json").resolve()
77+
json_path.parent.mkdir(exist_ok=True)
78+
json_path.write_text(create_pep_json(peps), encoding="utf-8")

0 commit comments

Comments
 (0)