Skip to content

Commit

Permalink
Link people and add PEP content (#9)
Browse files Browse the repository at this point in the history
Resolves #2
Resolves #3
  • Loading branch information
daneah committed May 13, 2024
1 parent a98303d commit f6b68af
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ output/

__pycache__/
*.egg-info/

.DS_Store
81 changes: 69 additions & 12 deletions connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import shutil
from collections import defaultdict
from pathlib import Path
from typing import Optional

from tqdm.rich import tqdm
from rst_to_myst import rst_to_myst


PEP_FILENAME_PATTERN = re.compile(r"pep-([0-9]{3,4}).(txt|rst)")
Expand Down Expand Up @@ -76,55 +80,108 @@ def get_topics(pep_content: str) -> set[str]:
return set()


def get_authors(pep_content: str) -> set[str]:
"""Get the authors of the PEP."""
authors = re.findall(r"Author: (.*)", pep_content)
if authors:
return {author.split(" <")[0].strip() for author in authors[0].split(", ")}
return set()


def get_delegate(pep_content: str) -> Optional[str]:
"""Get the delegate of the PEP."""
bdfl_delegates = re.findall(r"BDFL-Delegate: (.*)", pep_content)
pep_delegates = re.findall(r"PEP-Delegate: (.*)", pep_content)
return (bdfl_delegates or pep_delegates)[0].split(" <")[0].strip() if bdfl_delegates or pep_delegates else None


def create_clean_output_dir() -> None:
shutil.rmtree("output", ignore_errors=True)
Path("output").mkdir()


def output_markdown(connects: dict[str, dict[str, str | set[str]]]) -> None:
def output_markdown(connects: dict[str, dict[str, str | set[str] | None]]) -> None:
"""Output PEP connections in a useful format for Obsidian, including status and topic tags
Example:
#status/active
---
status: active
---
# PEP 0000: Some title
## Mentioned
- [[0391]]
- [[0456]]
## Authors
- [[Author Name]]
## Content
This is the content of the PEP.
"""

for pep, info in connects.items():
output_path = Path(f"output/{pep}.md")
output_path.touch()

with output_path.open("a") as output_file:
output_file.write(f"#status/{info['status']}\n")
output_file.write(f"#type/{info['type']}\n")

for topic in info["topics"]:
output_file.write(f"#topic/{topic}\n")
output_file.write("---\n")
output_file.write(f"status: {info['status']}\n")
output_file.write(f"type: {info['type']}\n")
if info["topics"]:
output_file.write("topics:\n")
for topic in info["topics"]:
output_file.write(f"- {topic}\n")
output_file.write("---\n")

output_file.write(f"\n# PEP {pep}: {info['title']}\n\n")
output_file.write("## Mentions\n\n")

for mention in info["mentions"]:
output_file.write(f"- [[{mention}]]\n")
if info["mentions"]:
output_file.write("## Mentions\n\n")

for mention in info["mentions"]:
output_file.write(f"- [[{mention}]]\n")

if info["authors"]:
output_file.write("\n## Authors\n\n")
for author in info["authors"]:
Path(f"output/{author}.md").touch()
output_file.write(f"- [[{author}]]\n")

if info["delegate"]:
Path(f"output/{info['delegate']}.md").touch()
output_file.write("\n## Delegate\n\n")
output_file.write(f"[[{info['delegate']}]]\n")

output_file.write("\n## Content\n\n")
for line in str(info["markdown"]).splitlines():
output_file.write(re.sub(r'^#', '###', line))
output_file.write("\n")


if __name__ == "__main__":
connects: dict[str, dict[str, str | set[str]]] = defaultdict(lambda: dict())
connects: dict[str, dict[str, str | set[str] | None]] = defaultdict(lambda: dict())

for pep_path in Path(".").glob("pep-*"):
for pep_path in tqdm(sorted(Path(".").glob("pep-*"))):
with pep_path.open() as pep_file:
pep_content = pep_file.read()
pep_identifier = get_identifier(pep_content)
try:
connects[pep_identifier]["markdown"] = rst_to_myst(pep_content).text
except ValueError:
connects[pep_identifier]["markdown"] = "Error converting to markdown."
connects[pep_identifier]["mentions"] = get_mentioned_peps(pep_content)
connects[pep_identifier]["status"] = get_status(pep_content)
connects[pep_identifier]["topics"] = get_topics(pep_content)
connects[pep_identifier]["title"] = get_title(pep_content)
connects[pep_identifier]["type"] = get_type(pep_content)
connects[pep_identifier]["authors"] = get_authors(pep_content)
connects[pep_identifier]["delegate"] = get_delegate(pep_content)

create_clean_output_dir()
output_markdown(connects)
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "pep-connections"
version = "0.0.1"
dependencies = [
"rich>=13.6.0",
"rst-to-myst[sphinx]>=0.4.0",
"tqdm>=4.66.4",
]

0 comments on commit f6b68af

Please sign in to comment.