Amara 2 writers in place

1 view
Skip to first unread message

Uche Ogbuji

unread,
Jul 13, 2008, 12:22:32 PM7/13/08
to amara...@googlegroups.com
So progress continues on Amara 2. More slowly than we've hoped, but
quite steadily. While Jeremy has been plugging away at XSLT I've been
working on the user API for writing markup. I have a demo file checked
in for generating XML and HTML using the traditional SAX-like APIs, and
also a demo using a new API I've put together to make writing markup
even simpler:

http://hg.4suite.org/amara/trunk/file/tip/demo/writing_xml.py

An excerpt:

from amara.writers.struct import *

w = structwriter().feed(
ROOT(
E(u'doc',
E(u'a', u'hello'),
E((u'b', None), u'this is unicode: \u221e'),
E(u'c', {u'parrot': u'dead', u'spam': u'eggs'}),
E(u'monty', E('spam', 'eggs')),
E(u'empty'),
E(u'func', lambda: u'this is a func'),
)
))

To generate:

<?xml version="1.0" encoding="UTF-8"?>
<doc><a>hello</a><b>this is unicode: ∞</b><c xmlns:parrot="dead"
xmlns:spam="eggs"/><monty><spam/></monty><empty/><func/></doc>

I've had such an idea for Amara for ages, but pyfo finally got me into
gear to implement it :) . Feedback on the API quite welcome, as well as
contributed test suites. Note, "raw" XML insertion APIs for the Amara
writers are planned, but not yet in place. Also, the indentation
support is pretty uneven at present.

Remember that you can try out Amara 2 without mucking with your Amara
1.x installation
using virtualenv.

http://pypi.python.org/pypi/virtualenv

I have a few notes here. See the "Test sandbox" heading:

http://wiki.xml3k.org/Amara2/Developer_notes


--
Uche Ogbuji http://uche.ogbuji.net
Founding Partner, Zepheira http://zepheira.com
Linked-in profile: http://www.linkedin.com/in/ucheogbuji
Articles: http://uche.ogbuji.net/tech/publications/

Uche Ogbuji

unread,
Jul 13, 2008, 12:40:43 PM7/13/08
to amara...@googlegroups.com
Uche Ogbuji wrote:
> from amara.writers.struct import *
>
> w = structwriter().feed(
> ROOT(
> E(u'doc',
> E(u'a', u'hello'),
> E((u'b', None), u'this is unicode: \u221e'),
> E(u'c', {u'parrot': u'dead', u'spam': u'eggs'}),
> E(u'monty', E('spam', 'eggs')),
> E(u'empty'),
> E(u'func', lambda: u'this is a func'),
> )
> ))
>
> To generate:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <doc><a>hello</a><b>this is unicode: ∞</b><c xmlns:parrot="dead"
> xmlns:spam="eggs"/><monty><spam/></monty><empty/><func/></doc>
>
Oops. I noticed above rrors and realized I'd skewed the check-in. New
tip now correctly works as follows:


$ cat /tmp/sw.py; python /tmp/sw.py
from amara.writers.struct import *

w = structwriter().feed(
ROOT(
E(u'doc',
E(u'a', u'hello'),
E((u'b', None), u'this is unicode: \u221e'),
E(u'c', {u'parrot': u'dead', u'spam': u'eggs'}),
E(u'monty', E('spam', 'eggs')),
E(u'empty'),
E(u'func', lambda: u'this is a func'),
)
))

<?xml version="1.0" encoding="UTF-8"?>
<doc><a>hello</a><b>this is unicode: ∞</b><c xmlns:parrot="dead"

xmlns:spam="eggs"/><monty><spam>eggs</spam></monty><empty/><func>this is
a func</func></doc>

Reply all
Reply to author
Forward
0 new messages