diff options
Diffstat (limited to 'pydis_site/apps/resources/utils.py')
-rw-r--r-- | pydis_site/apps/resources/utils.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/pydis_site/apps/resources/utils.py b/pydis_site/apps/resources/utils.py index bb812ec0..7f238431 100644 --- a/pydis_site/apps/resources/utils.py +++ b/pydis_site/apps/resources/utils.py @@ -15,7 +15,28 @@ default_categories = [ ] -def get_resources() -> list[dict[str, t.Union[list[str], str]]]: +def yaml_file_matches_search(yaml_data: dict[str, t.Union[list[str], str]], search_terms: list[str]) -> bool: + match_count = 0 + search_len = len(search_terms) + for search in search_terms: + for _, values in yaml_data["tags"].items(): + if search.lower() in values: + match_count += 1 + if match_count >= search_len: + return True + return False + + +def get_resources_from_search(search_categories: list[str]) -> list[dict[str, t.Union[list[str], str]]]: + out = [] + for item in RESOURCES_PATH.rglob("*.yaml"): + this_dict = yaml.safe_load(item.read_text()) + if yaml_file_matches_search(this_dict, search_categories): + out.append(this_dict) + return out + + +def get_all_resources() -> list[dict[str, t.Union[list[str], str]]]: """Loads resource YAMLs from provided path.""" return [yaml.safe_load(item.read_text()) for item in RESOURCES_PATH.rglob("*.yaml")] @@ -24,7 +45,7 @@ def get_resources_meta() -> dict[str, list[str]]: """Combines the tags from each resource into one dictionary of unique tags.""" resource_meta_tags = {x: set() for x in default_categories} - for resource in get_resources(): + for resource in get_all_resources(): for tag_key, tag_values in resource.get("tags").items(): for tag_item in tag_values: resource_meta_tags[tag_key].add(tag_item.title().replace('And', 'and', -1)) |