ecoxipy.decorators - Decorators for Shorter XML-Creation Code

The decorators in the module ecoxipy.decorators make the code to create XML using ecoxipy.MarkupBuilder shorter.

Important: The decorators use tinkerpy.namespace() to modify the globals of a function, so on certain Python runtimes (e.g. CPython 2.7) updates to the global dictionary after application of the decorator are not available in the function, on other runtimes (e.g. CPython 3.3 or PyPy) this restriction does not apply.

Examples

markup_builder_namespace() creates a tinkerpy.namespace() decorator to create XML using a newly created ecoxipy.MarkupBuilder instance. It takes an ecoxipy.Output implementation, the name under which the builder is available and a list of allowed element names:

>>> from ecoxipy.string_output import StringOutput
>>> builds_html = markup_builder_namespace(StringOutput, '_b', 'section', 'p', 'br')
>>> @builds_html
... def view(value):
...     return section(
...         p(value),
...         None,
...         p(u'äöüß'),
...         p('<&>'),
...         _b('<raw/>text'),
...         _b(br, (str(i) for i in range(3))),
...         (str(i) for i in range(3, 6)),
...         attr='\'"<&>'
...     )
...
>>> view('Hello World!') == u"""<section attr="'&quot;&lt;&amp;&gt;"><p>Hello World!</p><p>äöüß</p><p>&lt;&amp;&gt;</p><raw/>text<br/>012345</section>"""
True

xml_string_namespace() is a shorthand of markup_builder_namespace() using ecoxipy.string_output.StringOutput as the ecoxipy.Output implementation:

>>> builds_html = xml_string_namespace('_b', {'section', 'p', 'br'})
>>> @builds_html
... def view(value):
...     return section(
...         p(value),
...         None,
...         p(u'äöüß'),
...         p('<&>'),
...         _b('<raw/>text', br, (str(i) for i in range(3))),
...         (str(i) for i in range(3, 6)),
...         attr='\'"<&>'
...     )
...
>>> view('Hello World!') == u"""<section attr="'&quot;&lt;&amp;&gt;"><p>Hello World!</p><p>äöüß</p><p>&lt;&amp;&gt;</p><raw/>text<br/>012345</section>"""
True

Decorator Creation

ecoxipy.decorators.markup_builder_namespace(output, builder_name, *element_names, **kargs)[source]

A function creating a tinkerpy.namespace() decorator. It has all in element_names defined names bound to the appropriate virtual methods of a new ecoxipy.MarkupBuilder instance, which uses a new instance of the given output class.

Parameters:
  • output (ecoxipy.Output) – The output class to use.
  • builder_name – The name the MarkupBuilder instance is available under.
  • element_names – The names to bind to the appropriate virtual builder methods.
  • kargs – Arguments passed to the output constructor.
Returns:

The created decorater function. In its attribute builder the builder created is accessible.

ecoxipy.decorators.xml_string_namespace(builder_name, vocabulary)

Uses markup_builder_namespace() with the given vocabulary and ecoxipy.string_output.StringOutput to create a decorator, that allows for creation of UTF8-encoded XML strings.

Parameters:
  • builder_name – The name the ecoxipy.MarkupBuilder instance is available under.
  • vocabulary – An iterable of element names.
Returns:

The decorated function with it’s namespace extented with the element creators defined by the vocabulary.

Table Of Contents

Previous topic

ecoxipy - The ECoXiPy API

Next topic

ecoxipy.parsing - Parsing XML