Skip to content

Commit

Permalink
fix: no duplicates use random.shuffle instead of random.choices
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmx committed Jun 8, 2023
1 parent 5e5b6a2 commit 5f13d7c
Show file tree
Hide file tree
Showing 45 changed files with 25 additions and 3,490 deletions.
455 changes: 0 additions & 455 deletions .github/README.md

This file was deleted.

2 changes: 2 additions & 0 deletions .github/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ download = https://download-directory.github.io
spacing = 1
choose = 3
dry = false
browse = true
exclude = tile:devicons
42 changes: 22 additions & 20 deletions .github/docgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from os import listdir
from os.path import isfile
from pathlib import Path
from random import choices
from random import shuffle
from typing import Callable


Expand All @@ -15,13 +15,14 @@ def get_config(config_path: Path = Path("./.github/config.ini")) -> dict[str, st
return dict(parser.defaults())


def categorical_wallpapers() -> dict[str, list[Path]]:
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)
if not category.startswith(".") and not isfile(category) and category not in exclude
}


Expand All @@ -31,9 +32,14 @@ def get_templates() -> dict[str, str]:

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


def prime_templates(
Expand All @@ -47,23 +53,21 @@ def prime_templates(
}


def transform_shuffled(category: str, shuffled_paths: list[Path]) -> dict[str, str]:
results = {}
for index in range(len(shuffled_paths)):
results[f"shuffled_{index}"] = str(f"../{category}" / shuffled_paths[index])
results[f"shuffled_{index}_stem"] = shuffled_paths[index].stem
return results


# Handlers {{{
def handle_body(_, string: str, config: dict[str, str]) -> str:
shuffled = generate_shuffled(config)
shuffled = generate_shuffled(config, categorical_wallpapers(config["exclude"]))
results = []
spacing = "\n" * int(config["spacing"])
for category, pictures in shuffled.items():
merged = {"category": category}
merged = merged | config | transform_shuffled(category, pictures)
results.append(string.format(**merged))
return ("\n" * int(config["spacing"])).join(results)
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]:
Expand All @@ -76,8 +80,6 @@ def handle_category(_, string: str, config: dict[str, str]) -> dict[str, str]:
merged = config | {"filepath": str(picture), "filename": picture.stem}
results[readme] = f"{results[readme]}{string.format(**merged)}{spacing}"
return results


# }}}


Expand Down
10 changes: 1 addition & 9 deletions .github/templates/body.category.md
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
## {category}

<a href="{shuffled_0}"><img alt="{shuffled_0_stem}" src="{shuffled_0}"></a><br/><br/>

<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)
<a href="../{category}/{random}"><img alt="{random_stem}" src="../{category}/{random}"></a><br/><br/>
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 5f13d7c

Please sign in to comment.