WhakerPy 0.8

https://sourceforge.net/projects/whakerpy/

Module whakerpy.webapp

Class WebSiteResponse

Description

Create an HTML response content.

Can be used when all pages of a webapp are sharing the same header, nav and footer. Then, only one tree is created for all pages, and its body->main is changed depending on the requested page.

Constructor

Create a HTTPD Response instance with a default response.

Useful when creating dynamically the HTML Tree for a webapp. The "main" part of the body is re-created every time bake() is invoked. Here, it's loaded from a static file.

Parameters
  • name: (str) Filename of the body main content.
View Source
def __init__(self, name='index.html', tree=None, **kwargs):
    """Create a HTTPD Response instance with a default response.

    Useful when creating dynamically the HTML Tree for a webapp.
    The "main" part of the body is re-created every time bake() is invoked.
    Here, it's loaded from a static file.

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

    """
    self._name = name
    super(WebSiteResponse, self).__init__(name, tree)
    self._status = HTTPDStatus()
    self._bake()

Public functions

page

Override. Return the current HTML page name.

Returns
  • (str) Name of the file containing the body->main.
View Source
def page(self) -> str:
    """Override. Return the current HTML page name.

        :return: (str) Name of the file containing the body->main.

        """
    return self._name

Private functions

_process_events

Process the given events.

The given event name must match a function of the event's manager. Processing an event may change the content of the tree. In that case, the dirty method must be turned into True: it will invalidate the deprecated content (invalidate) and re-generate a new one (bake).

Parameters
  • events (dict): key=eventname, value=eventvalue
Returns
  • (bool)
View Source
def _process_events(self, events, **kwargs) -> bool:
    """Process the given events.

        The given event name must match a function of the event's manager.
        Processing an event may change the content of the tree. In that case,
        the `dirty` method must be turned into True: it will invalidate the
        deprecated content (_invalidate) and re-generate a new one (_bake).

        :param events (dict): key=event_name, value=event_value
        :return: (bool)

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

_invalidate

Remove all children nodes of the body "main".

Delete the body main content and nothing else.

View Source
def _invalidate(self) -> None:
    """Remove all children nodes of the body "main".

        Delete the body main content and nothing else.

        """
    node = self._htree.body_main
    node.clear_children()

_bake

Create the dynamic page content in HTML.

Load the body->main content from a file and add it to the tree.

View Source
def _bake(self) -> None:
    """Create the dynamic page content in HTML.

        Load the body->main content from a file and add it to the tree.

        """
    logging.debug(' -> Set {:s} content to the body->main'.format(self._name))
    with codecs.open(self._name, 'r', 'utf-8') as fp:
        lines = fp.readlines()
        if self._htree.get_body_main() is not None:
            self._htree.body_main.set_value(' '.join(lines))