Skip to content

Commit

Permalink
feat: Added __defaults.yaml support
Browse files Browse the repository at this point in the history
  • Loading branch information
KareemMAX committed Nov 29, 2023
1 parent f7264fa commit 2d77586
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The database tree looks like this:
tool-category-1/
├─ tool.yaml
tool-category-2/
├─ __index.yaml
├─ tool.yaml
scripts/
├─ script.py
Expand All @@ -20,5 +21,6 @@ tags.yaml

- Tool category folders: For each tool a category is assigned, this is only organizational and doesn't reflect in the user interface.
- `tool.yaml`: Actual database entries, they should follow the format found in [`_sample.yaml`](_sample.yaml). Note that files that starts with `.` or `_` won't be rendered (e.g. `_sample.yaml`).
- `__default.yaml`: A special file that holds placeholder values for entries in the same folder, unless overridden by the actual entry. This is useful for assigning placeholder images, descriptions for entries that miss them.
- [`_sample.yaml`](_sample.yaml): A sample yaml file explaining the current schema of entries.
- [`tags.yaml`](tags.yaml): List of currently available tags, if you introducted new tags you should add them in this file.
19 changes: 19 additions & 0 deletions scripts/database_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import re

TAGS_PATH = "tags.yaml"
# Index file is used to specify tags for all entries in a directory,
# unless they are specified in the entry file
DEFAULT_FILE_NAME = "__default.yaml"
strict = False


Expand Down Expand Up @@ -57,6 +60,20 @@ def fix_entry_image(entry: dict[str, list[str]]):
def load_database(tags: list[str]) -> dict[str, list[str]]:
database = {}
for root, _, files in os.walk("."):
# Load index file
defaults = {}
if DEFAULT_FILE_NAME in files:
logging.debug("Loading %s defaults", root)
with open(
os.path.join(root, DEFAULT_FILE_NAME), "r", encoding="utf-8"
) as f:
defaults = yaml.safe_load(f)
if "tags" in defaults:
check_tags(tags, defaults["tags"])

if "image_url" in defaults:
fix_entry_image(defaults)

for file in files:
if (
file != TAGS_PATH
Expand All @@ -68,6 +85,8 @@ def load_database(tags: list[str]) -> dict[str, list[str]]:
entry = yaml.safe_load(f)
check_tags(tags, list(entry.values())[0]["tags"])
fix_entry_image(entry)
entry_key = list(entry.keys())[0]
entry[entry_key] = {**defaults, **entry[entry_key]}

database.update(entry)

Expand Down

0 comments on commit 2d77586

Please sign in to comment.