diff options
author | Daniel Schadt <kingdread@gmx.de> | 2023-01-03 14:04:10 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2023-01-03 14:04:10 +0100 |
commit | a9d43deb3a7088c150eb48b92ab8b96365636cb0 (patch) | |
tree | 1fad0f4a7fac1fd106aa946c080defade5d9fe4b | |
parent | c8482105af855ab57ddbf1a1c87239b8df9fdbc0 (diff) | |
download | fietsboek-a9d43deb3a7088c150eb48b92ab8b96365636cb0.tar.gz fietsboek-a9d43deb3a7088c150eb48b92ab8b96365636cb0.tar.bz2 fietsboek-a9d43deb3a7088c150eb48b92ab8b96365636cb0.zip |
add type hints to fietsboek.pages
-rw-r--r-- | fietsboek/pages.py | 45 |
1 files changed, 17 insertions, 28 deletions
diff --git a/fietsboek/pages.py b/fietsboek/pages.py index f11b126..8baf02b 100644 --- a/fietsboek/pages.py +++ b/fietsboek/pages.py @@ -1,9 +1,11 @@ """Module containing logic to support "static" pages.""" import enum import re +from pathlib import Path from typing import List, Optional import markdown +from pyramid.request import Request class PageException(Exception): @@ -34,7 +36,7 @@ class Page: :vartype link_name: str :ivar locale_filter: An (optional) pattern that determines whether the page should be shown to certain locales only. - :vartype locale_filter: list[re.Pattern] + :vartype locale_filter: Optional[List[re.Pattern]] :ivar user_filter: A filter that determines if the page should be shown to certain users only. :vartype user_filter: UserFilter @@ -44,13 +46,13 @@ class Page: def __init__( self, - slug, - title, - content, - link_name, - locale_filter=None, - user_filter=UserFilter.EVERYONE, - menu_index=0, + slug: str, + title: str, + content: str, + link_name: str, + locale_filter: Optional[List[re.Pattern]] = None, + user_filter: UserFilter = UserFilter.EVERYONE, + menu_index: int = 0, ): # pylint: disable=too-many-arguments self.slug = slug @@ -61,14 +63,12 @@ class Page: self.user_filter = user_filter self.menu_index = menu_index - def matches(self, request): + def matches(self, request: Request) -> bool: """Checks whether the page matches the given request regarding the locale and user filters. :param request: The request to check against. - :type request: pyramid.request.Request :return: Whether the page matches the user. - :rtype: bool """ if self.user_filter == UserFilter.LOGGED_IN and not request.identity: return False @@ -83,16 +83,14 @@ class Page: return True @classmethod - def parse(cls, text): + def parse(cls, text: str) -> "Page": """Parses a :class:`Page` from the given textual source. This populates all metadata with the metadata from the file. :raises PageException: If there are missing metadata fields. :param text: Source to parse. - :type text: str :return: The parsed page. - :rtype: Page """ # Pylint doesn't know about the Markdown extensions: # pylint: disable=no-member @@ -145,11 +143,10 @@ class Pages: def __init__(self): self.collection = [] - def load_file(self, path): + def load_file(self, path: Path): """Load a page from a file. :param path: The path of the file to load. - :type path: pathlib.Path :raises PageException: If the page is malformed. """ source = path.read_text() @@ -166,19 +163,18 @@ class Pages: self.collection.append(page) self.collection.sort(key=lambda page: page.menu_index) - def load_directory(self, path): + def load_directory(self, path: Path): """Load a directory full of pages. This attemps to load and file in the given directory ending with ".md". :param path: The path of the directory to load. - :type path: pathlib.Path :raises PageException: If a page is malformed. """ for child in path.glob("*.md"): self.load_file(child) - def find(self, slug, request=None): + def find(self, slug: str, request: Optional[Request] = None) -> Optional[Page]: """Finds the page matching the given slug. If a request is given, the filtering based on locale/logged in state is applied. @@ -187,33 +183,26 @@ class Pages: page is found, ``None`` is returned. :param slug: The slug of the page: - :type slug: str :param request: The request to filter against. - :type request: pyramid.request.Request :return: The page, if any is found. - :rtype: Page """ for page in self.collection: if page.slug == slug and (request is None or page.matches(request)): return page return None - def pre_menu_items(self, request): + def pre_menu_items(self, request: Request) -> List[Page]: """Return all items that should appear before Fietsboek's main menu. :param request: The request to filter against. - :type request: pyramid.request.Request :return: A list of menu entries to show. - :rtype: list[Page] """ return [page for page in self.collection if page.menu_index < 0 and page.matches(request)] - def post_menu_items(self, request): + def post_menu_items(self, request: Request) -> List[Page]: """Return all items that should appear after Fietsboek's main menu. :param request: The request to filter against. - :type request: pyramid.request.Request :return: A list of menu entries to show. - :rtype: list[Page] """ return [page for page in self.collection if page.menu_index > 0 and page.matches(request)] |