API

This is the etgen package.

Inspired by Frederik Lundh’s ElementTree Builder

html

Defines an ElementTree Builder for generating HTML documents.

html2rst

Convert an etgen.html element to a reStructuredText string.

intervat

Tools for generating Belgian Intervat declarations.

odf

Tools for generating Open Document files and chunks thereof.

sepa

A set of generator tags for building SEPA documents.

utils

Some utility functions.

>>> E = Namespace('http://my.ns',
...    "bar baz bar-o-baz foo-bar class def".split())
>>> bob = E.bar_o_baz()
>>> baz = E.add_child(bob, 'baz', class_='first')
>>> print tostring(baz)
<baz xmlns="http://my.ns" class="first" />
>>> bob = E.bar_o_baz('Hello', class_='first', foo_bar="3")
>>> print tostring(bob)
<bar-o-baz xmlns="http://my.ns" class="first" foo-bar="3">Hello</bar-o-baz>

The following reproduces a pifall. Here is the initial code:

>>> E = Namespace(None, "div br".split())
>>> bob = E.div("a", E.br(), "b", E. br(), "c", E.br(), "d")
>>> print tostring(bob)
<div>a<br />b<br />c<br />d</div>

The idea is to use join_elems() to insert the <br> tags:

>>> from etgen.utils import join_elems

But surprise:

>>> elems = join_elems(["a", "b", "c", "d"], sep=E.br())
>>> print tostring(E.div(*elems))
<div>a<br />bcd<br />bcd<br />bcd</div>

What happened here is that the same <br> element instance was being inserted multiple times at different places. The correct usage is without the parentheses so that join_elems instantiates each time a new element:

>>> elems = join_elems(["a", "b", "c", "d"], sep=E.br)
>>> print tostring(E.div(*elems))
<div>a<br />b<br />c<br />d</div>

(This module’s source code is available here.)