diff options
author | 2021-04-01 21:15:36 +0800 | |
---|---|---|
committer | 2021-04-01 21:15:36 +0800 | |
commit | 5f77e80691179ba54ddabf8500dfa704e9514430 (patch) | |
tree | d5cf47421e50fad39eed628e61b20f79a7667ca2 /pydis_site/apps/resources/utils.py | |
parent | Merge pull request #393 from ks129/guides-app (diff) | |
parent | Change typo postion -> position in Real Python podcast resource (diff) |
Merge pull request #421 from ks129/resources-lists
Dewikification - Display resources based on YAML files
Diffstat (limited to 'pydis_site/apps/resources/utils.py')
-rw-r--r-- | pydis_site/apps/resources/utils.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/pydis_site/apps/resources/utils.py b/pydis_site/apps/resources/utils.py new file mode 100644 index 00000000..1855fc80 --- /dev/null +++ b/pydis_site/apps/resources/utils.py @@ -0,0 +1,42 @@ +import typing as t +from pathlib import Path + +import yaml + + +def get_resources(path: Path) -> t.List[t.Dict]: + """Loads resource YAMLs from provided path.""" + resources = [] + + for item in path.iterdir(): + if item.is_file() and item.suffix == ".yaml" and item.name != "_category_info.yaml": + resources.append(yaml.safe_load(item.read_text())) + + return resources + + +def get_subcategories(path: Path) -> t.List[t.Dict]: + """Loads resources subcategories with their resources by provided path.""" + subcategories = [] + + for item in path.iterdir(): + if item.is_dir() and item.joinpath("_category_info.yaml").exists(): + subcategories.append({ + "category_info": { + **yaml.safe_load( + item.joinpath("_category_info.yaml").read_text() + ), + "raw_name": item.name + }, + "resources": [ + yaml.safe_load(subitem.read_text()) + for subitem in item.iterdir() + if ( + subitem.is_file() + and subitem.suffix == ".yaml" + and subitem.name != "_category_info.yaml" + ) + ] + }) + + return subcategories |