Package tdi :: Module nodetree :: Class Node
[frames] | no frames]

Class Node

source code

object --+
         |
        Node

User visible node object
Instance Methods
Node
__call__(self, name)
Determine direct subnodes by name
source code
Node
__getattr__(self, name)
Determine direct subnodes by name
source code
 
__setitem__(self, name, value)
Set the attribute name to value
source code
str
__getitem__(self, name)
Determine the value of attribute name
source code
 
__delitem__(self, name)
Delete attribute name
source code
 
repeat(self, callback, itemlist, *fixed, **kwargs)
Repeat the snippet len(list(itemlist)) times
source code
 
remove(self)
Remove the node from the tree
source code
iterable
iterate(self, itemlist, separate=None)
Iterate over repeated nodes
source code
Node
replace(self, callback, other, *fixed)
Replace the node (and all subnodes) with the copy of another one
source code
Node
copy(self)
Deep copy this node
source code
basestring
render(self, callback, params, **kwargs)
Render this node only and return the result as string
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods
Node
__new__(cls, node, model, ctx=None, light=False)
Construction
source code
Instance Variables
tuple ctx
The node context (None if there isn't one).
Properties
basestring or None content
Node content
bool hiddenelement
Hidden node markup?
bool closedelement
Self-closed element? (read-only)
RawNode raw
Raw node

Inherited from object: __class__

Method Details

__new__(cls, node, model, ctx=None, light=False)
Static Method

source code 
Construction
Parameters:
  • node (Node or TemplateNode) - The node to clone
  • model (ModelAdapterInterface) - The template model instance
  • ctx (tuple) - The node context
  • light (bool) - Do a light copy? (In this case just the node context is updated and the original node is returned). Do this only if node is already a Node instance and you do not need another copy!
Returns: Node
The node instance
Overrides: object.__new__

__call__(self, name)
(Call operator)

source code 

Determine direct subnodes by name

In contrast to __getattr__ this works for all names. Also the exception in case of a failed lookup is different.

Parameters:
  • name (str) - The name looked for
Returns: Node
The found node
Raises:

__getattr__(self, name)
(Qualification operator)

source code 
Determine direct subnodes by name
Parameters:
  • name (str) - The name looked for
Returns: Node
The found subnode
Raises:
  • AttributeError - The subnode was not found

__setitem__(self, name, value)
(Index assignment operator)

source code 

Set the attribute name to value

The value is encoded according to the model and the original case of name is preserved. If the attribute does not occur in the original template, the case of the passed name is taken over. Non-string values are converted to string using str(). Unicode values are passed as-is to the model encoder.

Parameters:
  • name (str) - The attribute name (case insensitive)
  • value (any) - The attribute value (may be None for short attributes). Objects that are not None and and not unicode are stored as their string representation.

__getitem__(self, name)
(Indexing operator)

source code 
Determine the value of attribute name
Parameters:
  • name (str) - The attribute name
Returns: str
The attribute (None for shorttags)
Raises:
  • KeyError - The attribute does not exist

__delitem__(self, name)
(Index deletion operator)

source code 

Delete attribute name

If the attribute does not exist, no exception is raised.

Parameters:
  • name (str) - The name of the attribute to delete (case insensitive)

repeat(self, callback, itemlist, *fixed, **kwargs)

source code 

Repeat the snippet len(list(itemlist)) times

The actually supported signature is:

repeat(self, callback, itemlist, *fixed, separate=None)

Examples:

def render_foo(self, node):
    def callback(node, item):
        ...
    node.repeat(callback, [1, 2, 3, 4])

def render_foo(self, node):
    def callback(node, item):
        ...
    def sep(node):
        ...
    node.repeat(callback, [1, 2, 3, 4], separate=sep)

def render_foo(self, node):
    def callback(node, item, foo, bar):
        ...
    node.repeat(callback, [1, 2, 3, 4], "foo", "bar")

def render_foo(self, node):
    def callback(node, item, foo, bar):
        ...
    def sep(node):
        ...
    node.repeat(callback, [1, 2, 3, 4], "foo", "bar",
                separate=sep)
Parameters:
  • callback (callable) - The callback function
  • itemlist (iterable) - The items to iterate over
  • fixed (tuple) - Fixed parameters to be passed to the repeat methods
  • separate (callable) - Alternative callback function for separator nodes. If omitted or None, self.separate_name is looked up and called if it exists.

remove(self)

source code 

Remove the node from the tree

Tells the system, that the node (and all of its subnodes) should not be rendered.

iterate(self, itemlist, separate=None)

source code 

Iterate over repeated nodes

Iteration works by repeating the original node len(list(iteritems)) times, turning the original node into a container node and appending the generated nodeset to that container. That way, the iterated nodes are virtually indented by one level, but the container node is completely hidden, so it won't be visible.

All repeated nodes are marked as DONE, so they (and their subnodes) are not processed any further (except explicit callbacks). If there is a separator node assigned, it's put between the repetitions and not marked as DONE. The callbacks to them (if any) are executed when the template system gets back to control.

Parameters:
  • itemlist (iterable) - The items to iterate over
  • separate (callable) - Alternative callback function for separator nodes. If omitted or None, self.separate_name is looked up and called if it exists.
Returns: iterable
The repeated nodes and items ([(node, item), ...])

replace(self, callback, other, *fixed)

source code 

Replace the node (and all subnodes) with the copy of another one

The replacement node is deep-copied, so use it with care (performance-wise).

Parameters:
  • callback (callable) - callback function
  • other (Node) - The replacement node
  • fixed (tuple) - Fixed parameters for the callback
Returns: Node
The replaced node (actually the node itself, but with updated parameters)

copy(self)

source code 
Deep copy this node
Returns: Node
The node copy

render(self, callback, params, **kwargs)

source code 

Render this node only and return the result as string

Note that callback and params are optional positional parameters:

render(self, decode=True, decode_errors='strict')
# or
render(self, callback, decode=True, decode_errors='strict')
# or
render(self, callback, param1, paramx, ... decode=True, ...)

is also possible.

Parameters:
  • callback (callable or None) - Optional callback function and additional parameters
  • params (tuple) - Optional extra parameters for callback
  • decode (bool) - Decode the result back to unicode? This uses the encoding of the template.
  • decode_errors (str) - Error handler if decode errors happen.
  • model (any) - New render model, if omitted or None, the current model is applied.
  • adapter (callable) - Model adapter factory, takes the model and returns a ModelAdapterInterface. If omitted or None, the current adapter is used. This parameter is ignored, if no model parameter is passed.
Returns: basestring
The rendered node, type depends on decode keyword

Instance Variable Details

ctx

The node context (None if there isn't one). Node contexts are created on repetitions for all (direct and no-direct) subnodes of the repeated node. The context is a tuple, which contains for repeated nodes the position within the loop (starting with 0), the actual item and a tuple of the fixed parameters. The last two are also passed to the repeat callback function directly. For separator nodes, ctx[1] is a tuple containing the items before the separator and after it. Separator indices are starting with 0, too.

Property Details

content

Node content

The property can be set to a unicode or str value, which will be escaped and encoded (in case of unicode). It replaces the content or child nodes of the node completely.

The property can be read and will either return the raw content of the node (it may even contain markup) - or None if the node has subnodes.

Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, content)
Type:
basestring or None

hiddenelement

Hidden node markup?
Get Method:
unreachable.fget(self)
Set Method:
unreachable.fset(self, value)
Type:
bool

closedelement

Self-closed element? (read-only)
Get Method:
unreachable.fget(self)
Type:
bool

raw

Raw node
Get Method:
unreachable.fget(self)
Type:
RawNode