WhakerKit 2.0

https://sourceforge.net/projects/whakerkit/

Module whakerkit.responses

Class WhakerKitResponse

Description

Create a Response system for dynamic pages.

The page can or cannot have a static body->main content.

Constructor

Create a HTTPD Response instance with a default response.

Parameters
  • name: (str) Filename of the body main content.
View Source
def __init__(self, name: str | None=None, tree: HTMLNode | None=None, title: str=whakerkit.sg.name):
    """Create a HTTPD Response instance with a default response.

    :param name: (str) Filename of the body main content.

    """
    self._title = title
    self._name = name
    if name is not None:
        self._page_name = os.path.basename(name)
    else:
        name = 'undefined'
        self._page_name = ''
    super(WhakerKitResponse, self).__init__(name, tree)
    self._unittest_files = list()

Public functions

page

Return the current HTML body->main filename or an empty string.

Parameters
  • cls
View Source
@classmethod
def page(cls):
    """Return the current HTML body->main filename or an empty string."""
    return cls._name

get_pagename

Return the name of the HTML page as seen in the URL.

View Source
def get_pagename(self) -> str:
    """Return the name of the HTML page as seen in the URL."""
    return self._page_name

set_pagename

Set the name of this page as seen in the url.

Parameters
  • page_name: (str) Name of the HTML page.
View Source
def set_pagename(self, page_name: str):
    """Set the name of this page as seen in the url.

        :param page_name: (str) Name of the HTML page.

        """
    self._page_name = page_name
    self._htree.body_nav.set_nav_current(page_name)

create

To be overridden. Create the page tree.

Create the head and body_nav of the dynamic tree.

View Source
def create(self):
    """To be overridden. Create the page tree.

        Create the head and body_nav of the dynamic tree.

        """
    self._htree.head = WhakerKitHeadNode(self._htree.identifier, title=self._title)
    self._htree.body_main.set_attribute('id', 'main-content')
    self._htree.body_nav = WhakerKitNavNode(self._htree.identifier)
    if len(self._page_name) > 0:
        self._htree.body_nav.set_nav_current(self._page_name)

create_tree_header

Create the body_header of the dynamic tree.

Parameters
  • header_filename
View Source
def create_tree_header(self, header_filename: str | None=None):
    """Create the body_header of the dynamic tree."""
    self._htree.body_header = WhakerKitHeaderNode(self._htree.identifier, header_filename)

create_tree_footer

Create the body_footer of the dynamic tree.

Parameters
  • footer_filename
View Source
def create_tree_footer(self, footer_filename: str | None=None):
    """Create the body_footer of the dynamic tree."""
    self._htree.body_footer = WhakerKitFooterNode(self._htree.identifier, footer_filename)

enable_components

Wrapper of the enable_component method in the head.

Parameters
  • components: (list) List of component names
View Source
def enable_components(self, components: list) -> None:
    """Wrapper of the enable_component method in the head.

        :param components: (list) List of component names

        """
    for component in components:
        if hasattr(self._htree.head, 'enable_component') and callable(self._htree.head.enable_component):
            self._htree.head.enable_component(component)
        else:
            logging.warning(f'Components {component} are not enabled: the head node does not implement this.')

enable_unittests

Import unit test files append before in the html head.

View Source
def enable_unittests(self) -> None:
    """Import unit test files append before in the html head.

        """
    serialize_head = self._htree.head.serialize()
    if 'UnitTest.js' not in serialize_head:
        self._htree.add_script_file(whakerkit.sg.whakerexa + 'js/tests/UnitTest.js')
    for file_path in self._unittest_files:
        if os.path.basename(file_path) not in serialize_head:
            self._htree.add_script_file(file_path)

add_unittest_file

Add a new unit test file to the list.

If you want to use this files when the webapp starts, call the 'enableunittests' method.

Parameters
  • file_path: The path of the unit test file to add
Raises
  • FileNotFoundError: If the given file path doesn't exist
View Source
def add_unittest_file(self, file_path: str) -> None:
    """Add a new unit test file to the list.

        If you want to use this files when the webapp starts, call the
        'enable_unit_tests' method.

        :param file_path: The path of the unit test file to add
        :raises: FileNotFoundError: If the given file path doesn't exist

        """
    if os.path.exists(file_path):
        self._unittest_files.append(file_path)
    else:
        raise FileNotFoundError(f"The given file: {file_path} doesn't exist!")

Private functions

_process_events

Process the given events coming from the POST of any form.

Parameters
  • events (dict): key=eventname, value=eventvalue
  • kwargs: (dict) the keyword arguments
Returns
  • (bool) True if the whole page must be baked, False otherwise.
View Source
def _process_events(self, events: dict, **kwargs) -> bool:
    """Process the given events coming from the POST of any form.

        :param events (dict): key=event_name, value=event_value
        :param kwargs: (dict) the keyword arguments
        :return: (bool) True if the whole page must be baked, False otherwise.

        """
    self._status.code = 200
    return True

_invalidate

Override. Remove children nodes of the body->main.

View Source
def _invalidate(self):
    """Override. Remove children nodes of the body->main."""
    self._htree.body_main.clear_children()

_bake

Create the dynamic content of body->main.

Load the content from a file.

View Source
def _bake(self):
    """Create the dynamic content of body->main.

        Load the content from a file.

        """
    if self._name is not None and self._name != 'Undefined':
        filename = whakerkit.sg.get_root_path() + '/' + self._name
        if os.path.exists(filename) is True and os.path.isfile(filename) is True:
            with codecs.open(filename, 'r', 'utf-8') as fp:
                lines = fp.readlines()
                self._htree.body_main.set_value(' '.join(lines))
        else:
            self._status.code = 404