-
Notifications
You must be signed in to change notification settings - Fork 104
/
docgen.py
executable file
·98 lines (80 loc) · 3.59 KB
/
docgen.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
#!/usr/bin/env python
from configparser import ConfigParser
from json import dumps
from os import listdir
from os.path import isfile
from pathlib import Path
from random import shuffle
from typing import Callable
def get_config(config_path: Path = Path("./.github/config.ini")) -> dict[str, str]:
parser = ConfigParser()
parser.read_string(config_path.read_text())
return dict(parser.defaults())
def categorical_wallpapers(exclude: str | list[str] = []) -> dict[str, list[Path]]:
exclude = exclude.split(":") if type(exclude) is str else exclude
return {
# exclude categorical README.md
str(category): [Path(picture) for picture in listdir(category) if picture != "README.md"]
# exclude hidden directories and README.md
for category in listdir(".")
if not category.startswith(".") and not isfile(category) and category not in exclude
}
def get_templates() -> dict[str, str]:
return {template: Path(f".github/templates/{template}").read_text() for template in listdir(".github/templates")}
def generate_shuffled(
config: dict[str, str],
categories: dict[str, list[Path]],
) -> dict[str, list[Path]]:
results = {}
choose = int(config["choose"])
for category, pictures in categories.items():
shuffle(pictures)
results[category] = pictures[:choose]
return results
def prime_templates(
config: dict[str, str],
handlers: dict[str, Callable],
templates: dict[str, str] = get_templates(),
):
return {
template: handlers[template](template, string, config) if template in handlers else string.format(**config)
for template, string in templates.items()
}
# Handlers {{{
def handle_body(_, string: str, config: dict[str, str]) -> str:
shuffled = generate_shuffled(config, categorical_wallpapers(config["exclude"]))
results = []
spacing = "\n" * int(config["spacing"])
for category, pictures in shuffled.items():
merged = {"category": category} | config
results.append(f"## {category}{spacing}")
for picture in pictures:
merged["random"] = str(picture)
merged["random_stem"] = picture.stem
results.append(string.format(**merged))
if config["browse"].casefold() == "True".casefold():
results.append(f"[Browse](../{category}/README.md){spacing}")
return spacing.join(results)
def handle_category(_, string: str, config: dict[str, str]) -> dict[str, str]:
results = {}
spacing = "\n" * int(config["spacing"])
for category, pictures in categorical_wallpapers().items():
readme = f"{category}/README.md"
results[readme] = f"# {category}\n\n"
for picture in pictures:
merged = config | {"filepath": str(picture), "filename": picture.stem}
results[readme] = f"{results[readme]}{string.format(**merged)}{spacing}"
return results
# }}}
if __name__ == "__main__":
CONFIG = get_config()
primed = prime_templates(CONFIG, {"body.category.md": handle_body, "category.md": handle_category})
full_templates = ["heading", "body.heading", "body.category", "sources", "conclusion"] # ordered
full_templates = [primed[f"{item}.md"] for item in full_templates]
partial_template = primed["category.md"]
if CONFIG["dry"].casefold() == "True".casefold():
print(dumps({"full": full_templates, "partial": partial_template})) # use this with jq/fq
else:
Path(".github/README.md").write_text(("\n" * int(CONFIG["spacing"])).join(full_templates))
for category, readme in partial_template.items():
Path(category).write_text(readme)