atomixlib

Version described in this document: 0.6.x

This project is now dead and deprecated in favour of bridge which is a much more generic XML toolkit. I will no longer update atomixlib.

Download atomixlib

XML engine support

Currently atomixlib is only fully implemented on top of Amara. ElementTree? support is partial and will not allow to try entirely the package.

Don't be put off. Try Amara instead ;)

Overview

atomixlib is a Python library providing:

  • parser:
    • transform Atom documents from string to Python objects and vice versa

Multiple engines

atomixlib generators are associated to their XML engine for Python.

atomixlib comes with two of them:

Generators return instances of the Atomix class but are not practical to allow the manipulation of the Atom elements in an engine independant fashion. That's why atomixlib provides mappers. A mapper is in-memory representation as a graph of an Atom document. It is a pure Python object completely independant from any engine. That's what amplee uses internally.

Atomixlib usage

import atomixlib
# informs atomixlib that we will be using the amara underlying engine
# also available 'elementtree' and soon 'lxml'
atomixlib.default_engine_name = 'amara'

# create a feed
# a generator is in fact the underlying engine module
generator = atomixlib.get_generator()
feed = generator.create_feed()
feed.add_id(u'...')
feed.add_title(...)

# deserialize a feed
feed = atomixlib.d_feed('/path/to.feed.atom')

# feed is now a pure Python representation of the feed source
>>> feed.id
<atom:id xmlns:atom="http://www.w3.org/2005/Atom" element at -0x48898174 />

>>> unicode(feed.id)
u'urn:uuid:b664218f-14a7-4f7e-b3ee-f67b83cbad96'

>>> feed.id.value
u'urn:uuid:b664218f-14a7-4f7e-b3ee-f67b83cbad96'

>>> feed.id.prefix
u'atom'

>>> feed.id.xmlns
u'http://www.w3.org/2005/Atom'

>>> for author in feed.authors:
       print unicode(feed.author.name)
Sylvain Hellegouarch

# Import all the mapping classes
from atomixlib.mapper import * 
feed.updated = Updated()

entry = Entry()
entry.id = ID(u'urn:uuid:...')
entry.title = PlainTextTitle(u'hello there')
feed.entries.append(entry)

Now if we want to serialize documents to XML string:

atomixlib.s_feed(feed)
# serialize only the first entry
atomixlib.s_entry(feed.entries[0])

We can also change the XML prefix for all Atom elements of the document like this:

# set the prefix of all the atom elments to 'a'
feed.prefix = u'a'
feed.sync_children()

# use the default namespace ''
feed.prefix = None
feed.sync_children()

But atomixlib can also create Atom Publishing Protocol service documents like this

from atomixlib import ATOM10_PREFIX, ATOM10_NS
s = Service()
w = Workspace()
w.title = PlainTextTitle(u'Office')
# change the prefix of this element
w.title.prefix = ATOM10_NS
w.title.xmlns = ATOM10_PREFIX
s.workspaces.append(w)

# XML string of the service document
atomixlib.s_service(s)
# XML string of the workspace elementonly
atomixlib.s_workspace(w)