-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
117 lines (89 loc) · 3.4 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import re
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
from subprocess import DEVNULL, check_call, check_output
ROOT = Path(__file__).parent
INPUT_DIR = ROOT / "peps" / "peps"
OUTPUT_DIR = ROOT / "peps_epub"
COVER = ROOT / "cover.jpg"
def _convert(file: Path) -> None:
content = file.read_text()
if get_value("Status", content) not in {"Accepted", "Active", "Final"}:
return
authors = get_value("Author", content)
date = datetime.strptime(get_value("Created", content), "%d-%b-%Y").isoformat()
pep = get_value("PEP", content, with_key=True)
title = get_value("Title", content)
args = [
"--ascii",
"--table-of-contents",
*["--from", "rst"],
*["--to", "epub3"],
*["--indented-code-classes", "python"],
*["--epub-cover-image", COVER],
*["--metadata", "creator:Python Software Foundation"],
*["--metadata", f"date:{date[:10]}"],
*["--metadata", "language:en"],
*["--metadata", "subject:Python Enhancement Proposal"],
*["--metadata", f"title:{pep} ({title})"],
*["--output", output(file)],
]
for author in authors.split(","):
args.extend(["--metadata", f"contributor:{author.split('<')[0].strip()}"])
print(f">>> Processing {pep} ({title}) …", flush=True)
check_call(["pandoc", *args, file], cwd=INPUT_DIR)
def convert(file: Path) -> None:
try:
_convert(file)
except Exception:
print(f"!! Error with {file}", flush=True)
def create_parser() -> ArgumentParser:
"""Create the arguments parser."""
parser = ArgumentParser(
prog="python script.py",
description="PEP EPUB • https://github.com/BoboTiG/pep-epub",
)
parser.add_argument("--all", "-a", action="store_true", help="convert all PEP files")
parser.add_argument("--missing", "-m", action="store_true", help="convert missed PEP files")
return parser
def get_value(key: str, content: str, /, with_key: bool = False) -> str:
value = next(re.finditer(rf"{key}\s*:(.+)", content))[1].strip()
return f"{key}: {value}" if with_key else value
def output(file: Path) -> Path:
return OUTPUT_DIR / file.with_suffix(".epub").name
def update() -> None:
def call(*cmd: str) -> None:
check_call(cmd, cwd=INPUT_DIR.parent, stderr=DEVNULL, stdout=DEVNULL)
def get(*cmd: str) -> str:
return check_output(cmd, cwd=INPUT_DIR.parent, text=True)
current_rev = get("git", "log", "--oneline", "--max-count=1", "--abbrev")[:7]
print(f">>> Updating from revision {current_rev} …", flush=True)
call("git", "checkout", "main")
call("git", "pull", "--ff-only", "origin", "main")
new_rev = get("git", "log", "--oneline", "--max-count=1", "--abbrev")[:7]
call("git", "checkout", new_rev)
changes = get(
"git",
"diff",
"--name-only",
"--diff-filter=AM",
current_rev,
new_rev,
"peps/pep-[0-9]*[0-9].rst",
)
for change in changes.splitlines():
convert(INPUT_DIR.parent / change)
def main() -> int:
args = create_parser().parse_args()
if args.all or args.missing:
for file in INPUT_DIR.glob("pep-*.rst"):
if args.missing and output(file).is_file():
continue
convert(file)
else:
update()
return 0
if __name__ == "__main__":
import sys
sys.exit(main())