From a80626a5f605b5afbfedac344140c16eeae5adce Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Sat, 6 Jul 2019 23:37:34 +0100 Subject: Implement new YAML resource system --- pydis_site/utils/resources.py | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pydis_site/utils/resources.py (limited to 'pydis_site/utils/resources.py') diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py new file mode 100644 index 00000000..1053bbc5 --- /dev/null +++ b/pydis_site/utils/resources.py @@ -0,0 +1,79 @@ +from __future__ import annotations + +import yaml +import typing +import glob +from dataclasses import dataclass + + +@dataclass +class URL: + icon: str + title: str + url: str + + +class Resource: + description: str + name: str + payment: str + payment_description: typing.Optional[str] + urls: typing.List[URL] + + def __repr__(self): + return f"" + + @classmethod + def construct_from_yaml(cls, yaml_data: str) -> Resource: + 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: + resources: typing.List[Resource] + name: str + description: str + + def __repr__(self): + return f"" + + @classmethod + def construct_from_directory(cls, directory: str) -> Category: + 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]: + categories = [] + for cat in order: + direc = "pydis_site/apps/home/resources/" + cat + categories.append(Category.construct_from_directory(direc)) + + return categories -- cgit v1.2.3 From 530585f775da5ee975235b7e8655dcb7688ca065 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Sat, 6 Jul 2019 23:41:27 +0100 Subject: noqa the definition lines inside resources.py because flake8 does not support postponed evaluation --- pydis_site/utils/resources.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/utils/resources.py') diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py index 1053bbc5..582df7f7 100644 --- a/pydis_site/utils/resources.py +++ b/pydis_site/utils/resources.py @@ -24,7 +24,7 @@ class Resource: return f"" @classmethod - def construct_from_yaml(cls, yaml_data: str) -> Resource: + def construct_from_yaml(cls, yaml_data: str) -> Resource: # noqa resource = cls() loaded = yaml.safe_load(yaml_data) @@ -48,7 +48,7 @@ class Category: return f"" @classmethod - def construct_from_directory(cls, directory: str) -> Category: + def construct_from_directory(cls, directory: str) -> Category: # noqa category = cls() with open(f"{directory}/category_info.yaml") as category_info: -- cgit v1.2.3 From 73171d5a49c018e029f63ff6b5eb3736bbc709fd Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Sat, 6 Jul 2019 23:51:09 +0100 Subject: Docstrings & prefix category info files with underscore for prominence --- pydis_site/apps/home/resources/books/_category_info.yaml | 2 ++ pydis_site/apps/home/resources/books/category_info.yaml | 2 -- .../apps/home/resources/communities/_category_info.yaml | 2 ++ .../apps/home/resources/communities/category_info.yaml | 2 -- pydis_site/apps/home/resources/editors/_category_info.yaml | 2 ++ pydis_site/apps/home/resources/editors/category_info.yaml | 2 -- pydis_site/apps/home/resources/ides/_category_info.yaml | 2 ++ pydis_site/apps/home/resources/ides/category_info.yaml | 2 -- .../interactive_learning_tools/_category_info.yaml | 3 +++ .../interactive_learning_tools/category_info.yaml | 3 --- pydis_site/apps/home/resources/misc/_category_info.yaml | 2 ++ pydis_site/apps/home/resources/misc/category_info.yaml | 2 -- .../apps/home/resources/podcasts/_category_info.yaml | 2 ++ pydis_site/apps/home/resources/podcasts/category_info.yaml | 2 -- .../apps/home/resources/tutorials/_category_info.yaml | 3 +++ .../apps/home/resources/tutorials/category_info.yaml | 3 --- pydis_site/utils/resources.py | 14 ++++++++++++-- 17 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 pydis_site/apps/home/resources/books/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/books/category_info.yaml create mode 100644 pydis_site/apps/home/resources/communities/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/communities/category_info.yaml create mode 100644 pydis_site/apps/home/resources/editors/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/editors/category_info.yaml create mode 100644 pydis_site/apps/home/resources/ides/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/ides/category_info.yaml create mode 100644 pydis_site/apps/home/resources/interactive_learning_tools/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/interactive_learning_tools/category_info.yaml create mode 100644 pydis_site/apps/home/resources/misc/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/misc/category_info.yaml create mode 100644 pydis_site/apps/home/resources/podcasts/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/podcasts/category_info.yaml create mode 100644 pydis_site/apps/home/resources/tutorials/_category_info.yaml delete mode 100644 pydis_site/apps/home/resources/tutorials/category_info.yaml (limited to 'pydis_site/utils/resources.py') diff --git a/pydis_site/apps/home/resources/books/_category_info.yaml b/pydis_site/apps/home/resources/books/_category_info.yaml new file mode 100644 index 00000000..e3b89ad3 --- /dev/null +++ b/pydis_site/apps/home/resources/books/_category_info.yaml @@ -0,0 +1,2 @@ +description: The best books for learning Python or Python Frameworks +name: Books diff --git a/pydis_site/apps/home/resources/books/category_info.yaml b/pydis_site/apps/home/resources/books/category_info.yaml deleted file mode 100644 index e3b89ad3..00000000 --- a/pydis_site/apps/home/resources/books/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: The best books for learning Python or Python Frameworks -name: Books diff --git a/pydis_site/apps/home/resources/communities/_category_info.yaml b/pydis_site/apps/home/resources/communities/_category_info.yaml new file mode 100644 index 00000000..eccb8b80 --- /dev/null +++ b/pydis_site/apps/home/resources/communities/_category_info.yaml @@ -0,0 +1,2 @@ +description: Partnered communities that share part of our mission +name: Communities diff --git a/pydis_site/apps/home/resources/communities/category_info.yaml b/pydis_site/apps/home/resources/communities/category_info.yaml deleted file mode 100644 index eccb8b80..00000000 --- a/pydis_site/apps/home/resources/communities/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: Partnered communities that share part of our mission -name: Communities diff --git a/pydis_site/apps/home/resources/editors/_category_info.yaml b/pydis_site/apps/home/resources/editors/_category_info.yaml new file mode 100644 index 00000000..f8dc1413 --- /dev/null +++ b/pydis_site/apps/home/resources/editors/_category_info.yaml @@ -0,0 +1,2 @@ +description: Lightweight code editors supporting Python +name: Editors diff --git a/pydis_site/apps/home/resources/editors/category_info.yaml b/pydis_site/apps/home/resources/editors/category_info.yaml deleted file mode 100644 index f8dc1413..00000000 --- a/pydis_site/apps/home/resources/editors/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: Lightweight code editors supporting Python -name: Editors diff --git a/pydis_site/apps/home/resources/ides/_category_info.yaml b/pydis_site/apps/home/resources/ides/_category_info.yaml new file mode 100644 index 00000000..d331c95d --- /dev/null +++ b/pydis_site/apps/home/resources/ides/_category_info.yaml @@ -0,0 +1,2 @@ +description: Fully-integrated development environments for serious Python work +name: IDEs diff --git a/pydis_site/apps/home/resources/ides/category_info.yaml b/pydis_site/apps/home/resources/ides/category_info.yaml deleted file mode 100644 index d331c95d..00000000 --- a/pydis_site/apps/home/resources/ides/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: Fully-integrated development environments for serious Python work -name: IDEs diff --git a/pydis_site/apps/home/resources/interactive_learning_tools/_category_info.yaml b/pydis_site/apps/home/resources/interactive_learning_tools/_category_info.yaml new file mode 100644 index 00000000..08501627 --- /dev/null +++ b/pydis_site/apps/home/resources/interactive_learning_tools/_category_info.yaml @@ -0,0 +1,3 @@ +description: Learn Python with interactive content like courses, games and programming + challenges. +name: Interactive Learning Tools diff --git a/pydis_site/apps/home/resources/interactive_learning_tools/category_info.yaml b/pydis_site/apps/home/resources/interactive_learning_tools/category_info.yaml deleted file mode 100644 index 08501627..00000000 --- a/pydis_site/apps/home/resources/interactive_learning_tools/category_info.yaml +++ /dev/null @@ -1,3 +0,0 @@ -description: Learn Python with interactive content like courses, games and programming - challenges. -name: Interactive Learning Tools diff --git a/pydis_site/apps/home/resources/misc/_category_info.yaml b/pydis_site/apps/home/resources/misc/_category_info.yaml new file mode 100644 index 00000000..4fdc4bf7 --- /dev/null +++ b/pydis_site/apps/home/resources/misc/_category_info.yaml @@ -0,0 +1,2 @@ +description: Resources which do not fit into the other categories +name: Miscellaneous diff --git a/pydis_site/apps/home/resources/misc/category_info.yaml b/pydis_site/apps/home/resources/misc/category_info.yaml deleted file mode 100644 index 4fdc4bf7..00000000 --- a/pydis_site/apps/home/resources/misc/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: Resources which do not fit into the other categories -name: Miscellaneous diff --git a/pydis_site/apps/home/resources/podcasts/_category_info.yaml b/pydis_site/apps/home/resources/podcasts/_category_info.yaml new file mode 100644 index 00000000..a0f9025c --- /dev/null +++ b/pydis_site/apps/home/resources/podcasts/_category_info.yaml @@ -0,0 +1,2 @@ +description: Notable podcasts about the Python ecosystem +name: Podcasts diff --git a/pydis_site/apps/home/resources/podcasts/category_info.yaml b/pydis_site/apps/home/resources/podcasts/category_info.yaml deleted file mode 100644 index a0f9025c..00000000 --- a/pydis_site/apps/home/resources/podcasts/category_info.yaml +++ /dev/null @@ -1,2 +0,0 @@ -description: Notable podcasts about the Python ecosystem -name: Podcasts diff --git a/pydis_site/apps/home/resources/tutorials/_category_info.yaml b/pydis_site/apps/home/resources/tutorials/_category_info.yaml new file mode 100644 index 00000000..a9adc106 --- /dev/null +++ b/pydis_site/apps/home/resources/tutorials/_category_info.yaml @@ -0,0 +1,3 @@ +description: Tutorials and references for those that are just getting started with + python +name: Tutorials diff --git a/pydis_site/apps/home/resources/tutorials/category_info.yaml b/pydis_site/apps/home/resources/tutorials/category_info.yaml deleted file mode 100644 index a9adc106..00000000 --- a/pydis_site/apps/home/resources/tutorials/category_info.yaml +++ /dev/null @@ -1,3 +0,0 @@ -description: Tutorials and references for those that are just getting started with - python -name: Tutorials diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py index 582df7f7..875c0dc9 100644 --- a/pydis_site/utils/resources.py +++ b/pydis_site/utils/resources.py @@ -14,6 +14,9 @@ class URL: class Resource: + """ + A class representing a resource on the resource page + """ description: str name: str payment: str @@ -40,6 +43,9 @@ class Resource: class Category: + """ + A class representing a resource on the resources page + """ resources: typing.List[Resource] name: str description: str @@ -51,7 +57,7 @@ class Category: def construct_from_directory(cls, directory: str) -> Category: # noqa category = cls() - with open(f"{directory}/category_info.yaml") as category_info: + with open(f"{directory}/_category_info.yaml") as category_info: category_data = yaml.safe_load(category_info) category.__dict__.update(category_data) @@ -59,7 +65,7 @@ class Category: category.resources = [] for resource in glob.glob(f"{directory}/*.yaml"): - if resource == f"{directory}/category_info.yaml": + if resource == f"{directory}/_category_info.yaml": continue with open(resource) as res_file: @@ -71,6 +77,10 @@ class Category: def load_categories(order: typing.List[str]) -> typing.List[Category]: + """ + Load the categories specified in the order list and return them + as a list. + """ categories = [] for cat in order: direc = "pydis_site/apps/home/resources/" + cat -- cgit v1.2.3 From b467d74ed86e109e7b4fac62634219b7b7955414 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Sun, 7 Jul 2019 00:18:46 +0100 Subject: pin pycodestyle to 3.0.0 and fix linting errors --- Pipfile | 5 +++-- Pipfile.lock | 9 ++++++--- pydis_site/utils/resources.py | 21 +++++++++------------ 3 files changed, 18 insertions(+), 17 deletions(-) (limited to 'pydis_site/utils/resources.py') diff --git a/Pipfile b/Pipfile index e68abcad..036f079c 100644 --- a/Pipfile +++ b/Pipfile @@ -4,7 +4,7 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] -flake8 = "*" +flake8 = "~=3.7.7" flake8-bandit = "==1.0.2" flake8-bugbear = "~=19.3.0" flake8-import-order = "~=0.18.1" @@ -14,7 +14,8 @@ mccabe = "~=0.6.1" pep8-naming = "~=0.8.2" coverage = "~=4.5.3" unittest-xml-reporting = "~=2.5.1" -flake8-docstrings = "*" +flake8-docstrings = "~=1.3.0" +pydocstyle = "==3.0.0" [packages] django = "~=2.2" diff --git a/Pipfile.lock b/Pipfile.lock index 1da50999..b40b06c1 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "a7e8ef5026516c05a7a24abc75904ff8608a429e1714bb01f6420d5f0a91a8cd" + "sha256": "2f1eaa80a71489a59f71001ad4af39e8e570f8d7ebca33b475d2c530811ad084" }, "pipfile-spec": 6, "requires": { @@ -404,9 +404,12 @@ }, "pydocstyle": { "hashes": [ - "sha256:58c421dd605eec0bce65df8b8e5371bb7ae421582cdf0ba8d9435ac5b0ffc36a" + "sha256:2258f9b0df68b97bf3a6c29003edc5238ff8879f1efb6f1999988d934e432bd8", + "sha256:5741c85e408f9e0ddf873611085e819b809fca90b619f5fd7f34bd4959da3dd4", + "sha256:ed79d4ec5e92655eccc21eb0c6cf512e69512b4a97d215ace46d17e4990f2039" ], - "version": "==4.0.0" + "index": "pypi", + "version": "==3.0.0" }, "pyflakes": { "hashes": [ diff --git a/pydis_site/utils/resources.py b/pydis_site/utils/resources.py index 875c0dc9..ab0df9d7 100644 --- a/pydis_site/utils/resources.py +++ b/pydis_site/utils/resources.py @@ -1,22 +1,22 @@ from __future__ import annotations -import yaml -import typing 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 - """ + """A class representing a resource on the resource page""" description: str name: str payment: str @@ -24,6 +24,7 @@ class Resource: urls: typing.List[URL] def __repr__(self): + """Return a representation of the resource""" return f"" @classmethod @@ -43,14 +44,13 @@ class Resource: class Category: - """ - A class representing a resource on the resources page - """ + """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"" @classmethod @@ -77,10 +77,7 @@ class Category: def load_categories(order: typing.List[str]) -> typing.List[Category]: - """ - Load the categories specified in the order list and return them - as a list. - """ + """Load the categories specified in the order list and return them""" categories = [] for cat in order: direc = "pydis_site/apps/home/resources/" + cat -- cgit v1.2.3