WhakerPy 0.8

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

Module whakerpy.htmlmaker

Class BaseNode

Description

A base class for any node in an HTML tree.

An HTML element without content is called an empty node. It has a start tag but neither a content nor an end tag. It has only attributes.

The BaseNode() class is a base class for any of these HTML elements. It is intended to be overridden.

Constructor

Create a new base node.

Parameters
  • parent: (str) Parent identifier
  • identifier: (str) This node identifier
Raises
  • NodeInvalidIdentifierError: if 'identifier' contains invalid characters or if invalid length
View Source
def __init__(self, parent: str=None, identifier: str=None, **kwargs):
    """Create a new base node.

    :param parent: (str) Parent identifier
    :param identifier: (str) This node identifier
    :raises: NodeInvalidIdentifierError: if 'identifier' contains invalid characters or if invalid length

    """
    if identifier is not None:
        ident = BaseNode.validate_identifier(identifier)
        self.__identifier = ident
    else:
        self.__identifier = str(uuid.uuid1())
    self._parent = None
    self.set_parent(parent)

Public functions

validate_identifier

Return the given identifier if it matches the requirements.

An identifier should contain at least 1 character and no whitespace.

Parameters
  • identifier: (str) Key to be validated
Raises
  • NodeInvalidIdentifierError: if it contains invalid characters
  • NodeInvalidIdentifierError: if invalid length
Returns
  • (str) Validated identifier
View Source
@staticmethod
def validate_identifier(identifier: str) -> str:
    """Return the given identifier if it matches the requirements.

        An identifier should contain at least 1 character and no whitespace.

        :param identifier: (str) Key to be validated
        :raises: NodeInvalidIdentifierError: if it contains invalid characters
        :raises: NodeInvalidIdentifierError: if invalid length
        :return: (str) Validated identifier

        """
    entry = BaseNode.full_strip(identifier)
    if len(entry) != len(identifier):
        raise NodeInvalidIdentifierError(identifier)
    if len(identifier) == 0:
        raise NodeInvalidIdentifierError(identifier)
    return identifier

full_strip

Fully strip the string: multiple whitespace, tab and CR/LF.

Returns
  • (str) Cleaned string
Parameters
  • entry
View Source
@staticmethod
def full_strip(entry):
    """Fully strip the string: multiple whitespace, tab and CR/LF.

        :return: (str) Cleaned string

        """
    e = re.sub('[\\s]+', '', entry)
    e = re.sub('[\t]+', '', e)
    e = re.sub('[\n]+', '', e)
    e = re.sub('[\r]+', '', e)
    if '\ufeff' in e:
        e = re.sub('\ufeff', '', e)
    return e

identifier

Return the (supposed-) unique ID of the node within the scope of a tree.

View Source
@property
def identifier(self) -> str:
    """Return the (supposed-) unique ID of the node within the scope of a tree. """
    return self.__identifier

is_leaf

Return true if node has no children.

View Source
def is_leaf(self) -> bool:
    """Return true if node has no children."""
    return True

is_root

Return true if node has no parent, i.e. like root.

View Source
def is_root(self) -> bool:
    """Return true if node has no parent, i.e. like root."""
    return self._parent is None

get_parent

The parent identifier.

Returns
  • (str) node identifier
View Source
def get_parent(self) -> str:
    """The parent identifier.

        :return: (str) node identifier

        """
    return self._parent

set_parent

Set the parent identifier.

Parameters
  • node_id: (str) Identifier of the parent
View Source
def set_parent(self, node_id: str) -> None:
    """Set the parent identifier.

        :param node_id: (str) Identifier of the parent

        """
    if self.__identifier == node_id:
        raise NodeKeyError(self.__identifier, node_id)
    self._parent = node_id

has_child

To be overriden. Return True if the given node ID is a direct child.

Parameters
  • node_id: (str) Identifier of the node
Returns
  • (bool) True if given identifier is a direct child.
View Source
def has_child(self, node_id: str) -> bool:
    """To be overriden. Return True if the given node ID is a direct child.

        :param node_id: (str) Identifier of the node
        :return: (bool) True if given identifier is a direct child.

        """
    return not self.is_leaf()

serialize

To be overriden. Serialize the node into HTML.

Parameters
  • nbs: (int) Number of spaces for the indentation
Returns
  • (str)
View Source
def serialize(self, nbs: int=4) -> str:
    """To be overriden. Serialize the node into HTML.

        :param nbs: (int) Number of spaces for the indentation
        :return: (str)

        """
    return ''

Overloads

__repr__

View Source
def __repr__(self):
    return self.serialize()

__str__

View Source
def __str__(self):
    return 'Node ({:s})'.format(self.identifier)