WhakerPy 0.8

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

Module whakerpy.webapp

Class WebSiteData

Description

Storage class of a webapp configuration, extracted from a JSON file.

For each dynamic page of a webapp, this class contains the filename of the page - the one of the URL, its title and the local filename of its body->main content.

Below is an example of a page description in the JSON parsed file: "index.html": { "title": "Home", "main": "index.htm", "header": true, "footer": true }

Constructor

Create a WebSiteData instance.

Parameters
  • json_filename: (str) Configuration filename.
View Source
def __init__(self, json_filename=DEFAULT_CONFIG_FILE):
    """Create a WebSiteData instance.

    :param json_filename: (str) Configuration filename.

    """
    self._main_path = ''
    self._default = ''
    self._pages = dict()
    with codecs.open(json_filename, 'r', 'utf-8') as json_file:
        data = json.load(json_file)
        self._main_path = data['pagespath']
        for key in data:
            if key != 'pagespath':
                self._pages[key] = data[key]
                if len(self._default) == 0:
                    self._default = key

Public functions

get_default_page

Return the name of the default page.

View Source
def get_default_page(self) -> str:
    """Return the name of the default page."""
    return self._default

filename

Return the filename of a given page.

Parameters
  • page: (str) Name of an HTML page
Returns
  • (str)
View Source
def filename(self, page: str) -> str:
    """Return the filename of a given page.

        :param page: (str) Name of an HTML page
        :return: (str)

        """
    if page in self._pages:
        main_name = self._pages[page]['main']
        return os.path.join(self._main_path, main_name)
    return ''

title

Return the title of a given page.

Parameters
  • page: (str) Name of an HTML page
Returns
  • (str)
View Source
def title(self, page: str) -> str:
    """Return the title of a given page.

        :param page: (str) Name of an HTML page
        :return: (str)

        """
    if page in self._pages:
        if 'title' in self._pages[page]:
            return self._pages[page]['title']
    return ''

has_header

Return True if the given page should have the header.

Parameters
  • page: (str) Name of an HTML page
Returns
  • (bool)
View Source
def has_header(self, page: str) -> bool:
    """Return True if the given page should have the header.

        :param page: (str) Name of an HTML page
        :return: (bool)

        """
    if page in self._pages:
        if 'header' in self._pages[page].keys():
            return self._pages[page]['header']
    return False

has_footer

Return True if the given page should have the footer.

Parameters
  • page: (str) Name of an HTML page
Returns
  • (bool)
View Source
def has_footer(self, page: str) -> bool:
    """Return True if the given page should have the footer.

        :param page: (str) Name of an HTML page
        :return: (bool)

        """
    if page in self._pages:
        if 'footer' in self._pages[page]:
            return self._pages[page]['footer']
    return False

create_pages

Instantiate all pages response from the json.

Parameters
  • web_response: (BaseResponseRecipe) the class to used to create the pages, WebSiteResponse class used by default
  • default_path: (str) None by default, the default path for all pages
Returns
  • (dict) a dictionary with key = page name and value = the response object
View Source
def create_pages(self, web_response=WebSiteResponse, default_path: str='') -> dict:
    """Instantiate all pages response from the json.

        :param web_response: (BaseResponseRecipe) the class to used to create the pages,
                            WebSiteResponse class used by default
        :param default_path: (str) None by default, the default path for all pages

        :return: (dict) a dictionary with key = page name and value = the response object

        """
    pages = dict()
    tree = HTMLTree('sample')
    for page_name in self._pages:
        page_path = os.path.join(default_path, self.filename(page_name))
        pages[page_name] = web_response(page_path, tree)
    return pages

bake_response

Return the bakery system to create the page dynamically.

To be overridden by subclasses.

Parameters
  • page_name: (str) Name of an HTML page
  • default: (str) The default path
Returns
  • (BaseResponseRecipe)
View Source
def bake_response(self, page_name: str, default: str='') -> BaseResponseRecipe | None:
    """Return the bakery system to create the page dynamically.

        To be overridden by subclasses.

        :param page_name: (str) Name of an HTML page
        :param default: (str) The default path
        :return: (BaseResponseRecipe)

        """
    raise NotImplementedError

Overloads

__format__

View Source
def __format__(self, fmt):
    return str(self).__format__(fmt)

__iter__

View Source
def __iter__(self):
    for a in self._pages:
        yield a

__len__

View Source
def __len__(self):
    return len(self._pages)

__contains__

Value is a page name.

Parameters
  • value
View Source
def __contains__(self, value):
    """Value is a page name."""
    return value in self._pages