Skip to content

Commit

Permalink
chore: added dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmx committed Jun 8, 2023
1 parent 3fd0e4f commit ef1407e
Show file tree
Hide file tree
Showing 45 changed files with 36 additions and 3,410 deletions.
373 changes: 0 additions & 373 deletions .github/README.md

This file was deleted.

3 changes: 2 additions & 1 deletion .github/config.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[DEFAULT]
source = ./.github/docgen.py
source = docgen.py
download = https://download-directory.github.io
spacing = 1
choose = 3
dry = false
62 changes: 32 additions & 30 deletions .github/docgen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/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
Expand All @@ -14,38 +15,34 @@ def get_config(config_path: Path = Path("./.github/config.ini")) -> dict[str, st
return dict(parser.defaults())


def categorical_wallpapers(repo: Path = Path(".")) -> dict[str, list[Path]]:
def categorical_wallpapers() -> dict[str, list[Path]]:
return {
# exclude categorical README.md
str(repo / category): [Path(picture) for picture in listdir(repo / category) if picture != "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(repo)
if not category.startswith(".") and not isfile(repo / category)
for category in listdir(".")
if not category.startswith(".") and not isfile(category)
}


def get_templates(repo: Path = Path(".")) -> dict[str, str]:
return {
template: Path(repo / ".github" / "templates" / template).read_text()
for template in listdir(repo / ".github" / "templates")
}
def get_templates() -> dict[str, str]:
return {template: Path(f".github/templates/{template}").read_text() for template in listdir(".github/templates")}


def generate_shuffled(
categories: dict[str, list[Path]] = categorical_wallpapers(), config: dict[str, str] = get_config()
config: dict[str, str],
categories: dict[str, list[Path]] = categorical_wallpapers()
) -> dict[str, list[Path]]:
return {category: choices(pictures, k=int(config["choose"])) for category, pictures in categories.items()}


def prime_templates(
config: dict[str, str],
handlers: dict[str, Callable],
templates: dict[str, str] = get_templates(),
config: dict[str, str] = get_config(),
):
return {
template: handlers[template](template, string, config)
if template in handlers
else string.format(**config)
template: handlers[template](template, string, config) if template in handlers else string.format(**config)
for template, string in templates.items()
}

Expand All @@ -58,8 +55,9 @@ def transform_shuffled(category: str, shuffled_paths: list[Path]) -> dict[str, s
return results


# Handlers {{{
def handle_body(_, string: str, config: dict[str, str]) -> str:
shuffled = generate_shuffled()
shuffled = generate_shuffled(config)
results = []
for category, pictures in shuffled.items():
merged = {"category": category}
Expand All @@ -68,27 +66,31 @@ def handle_body(_, string: str, config: dict[str, str]) -> str:
return ("\n" * int(config["spacing"])).join(results)


def handle_category(_, string: str, variables: dict[str, str]) -> dict[str, str]:
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 = variables | {"filepath": str(picture), "filename": picture.stem}
results[readme] += string.format(**merged) + "\n"
merged = config | {"filepath": str(picture), "filename": picture.stem}
results[readme] = f"{results[readme]}{string.format(**merged)}{spacing}"
return results


# }}}


if __name__ == "__main__":
primed = prime_templates({"body.category.md": handle_body, "category.md": handle_category})
Path(".github/README.md").write_text("\n".join([
# custom priority
primed["heading.md"],
primed["body.heading.md"],
primed["body.category.md"],
primed["sources.md"],
primed["conclusion.md"],
]))

for category, readme_string in primed["category.md"].items():
Path(category).write_text(readme_string)
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)
2 changes: 2 additions & 0 deletions .github/templates/body.category.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
<a href="{shuffled_1}"><img alt="{shuffled_1_stem}" src="{shuffled_1}"></a><br/><br/>

<a href="{shuffled_2}"><img alt="{shuffled_2_stem}" src="{shuffled_2}"></a><br/><br/>

[Browse]({category}/README.md)
84 changes: 0 additions & 84 deletions abstract/README.md

This file was deleted.

48 changes: 0 additions & 48 deletions aerial/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions animated/README.md

This file was deleted.

Loading

0 comments on commit ef1407e

Please sign in to comment.