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.
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="'"<&>"><p>Hello World!</p><p>äöüß</p><p><&></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="'"<&>"><p>Hello World!</p><p>äöüß</p><p><&></p><raw/>text<br/>012345</section>"""
True
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: |
|
---|---|
Returns: | The created decorater function. In its attribute builder the builder created is accessible. |
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: |
|
---|---|
Returns: | The decorated function with it’s namespace extented with the element creators defined by the vocabulary. |