blob: ab0df9d789f82c352425ec81c2eeee8316c64730 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
from __future__ import annotations
import glob
import typing
from dataclasses import dataclass
import yaml
@dataclass
class URL:
"""A class representing a link to a resource"""
icon: str
title: str
url: str
class Resource:
"""A class representing a resource on the resource page"""
description: str
name: str
payment: str
payment_description: typing.Optional[str]
urls: typing.List[URL]
def __repr__(self):
"""Return a representation of the resource"""
return f"<Resource name={self.name}>"
@classmethod
def construct_from_yaml(cls, yaml_data: str) -> Resource: # noqa
resource = cls()
loaded = yaml.safe_load(yaml_data)
resource.__dict__.update(loaded)
resource.__dict__["urls"] = []
for url in loaded["urls"]:
resource.__dict__["urls"].append(URL(**url))
return resource
class Category:
"""A class representing a resource on the resources page"""
resources: typing.List[Resource]
name: str
description: str
def __repr__(self):
"""Return a representation of the category"""
return f"<Category name={self.name}>"
@classmethod
def construct_from_directory(cls, directory: str) -> Category: # noqa
category = cls()
with open(f"{directory}/_category_info.yaml") as category_info:
category_data = yaml.safe_load(category_info)
category.__dict__.update(category_data)
category.resources = []
for resource in glob.glob(f"{directory}/*.yaml"):
if resource == f"{directory}/_category_info.yaml":
continue
with open(resource) as res_file:
category.resources.append(
Resource.construct_from_yaml(res_file)
)
return category
def load_categories(order: typing.List[str]) -> typing.List[Category]:
"""Load the categories specified in the order list and return them"""
categories = []
for cat in order:
direc = "pydis_site/apps/home/resources/" + cat
categories.append(Category.construct_from_directory(direc))
return categories
|