Package sword2 :: Module atom_objects :: Class Entry
[hide private]
[frames] | no frames]

Class Entry

source code


Used to create `Entry`s - for multipart/metadata submission. Has a simple and extendable way to add in
namespace-aware key-value pairs.

Example of use:

>>> from sword2 import Entry
>>> e = Entry()   # it can be opened blank, but more usefully...
>>> e = Entry(id="atom id",
              title="atom title",
              dcterms_identifier="some other id")

# Getting the bytestring document
>>> print str(e)
<?xml version="1.0"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:dcterms="http://purl.org/dc/terms/">
    <generator uri="http://bitbucket.org/beno/python-sword2" version="0.1"/>
<updated>2011-06-05T16:20:34.914474</updated><dcterms:identifier>some other id</dcterms:identifier><id>atom id</id><title>atom title</title></entry>


# Adding fields to the metadata entry
# dcterms (and other, non-atom fields) can be used by passing in a parameter with an underscore between the 
# prefix and element name, eg:
>>> e.add_fields(dcterms_title= "dcterms title", dcterms_some_other_field = "other")

# atom:author field is treated slightly differently than all the other fields:
# dictionary is required
>>> e.add_fields(author={"name":"Ben", "email":"foo@example.org"})
>>> print str(e)
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:dcterms="http://purl.org/dc/terms/">
    <generator uri="http://bitbucket.org/beno/python-sword2" version="0.1"/>
    <updated>2011-06-05T16:20:34.914474</updated>
    <dcterms:identifier>some other id</dcterms:identifier>
    <id>atom id</id><title>atom title</title>
    <author>
        <name>Ben</name>
        <email>foo@example.org</email>
    </author>
    <dcterms:some_other_field>other</dcterms:some_other_field>
    <dcterms:title>dcterms title</dcterms:title>
</entry>
>>> 

# Other namespaces - use `Entry.register_namespace` to add them to the list of those considered  (prefix, URL):
>>> e.register_namespace("myschema", "http://example.org")
>>> e.add_fields(myschema_foo = "bar")
>>> print str(e)
<?xml version="1.0"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:dcterms="http://purl.org/dc/terms/">
    <generator uri="http://bitbucket.org/beno/python-sword2" version="0.1"/>
    <updated>2011-06-05T16:20:34.914474</updated>
    <dcterms:identifier>some other id</dcterms:identifier>
    <id>atom id</id><title>atom title</title>
    <author>
        <name>Ben</name>
        <email>foo@example.org</email>
    </author>
    <dcterms:some_other_field>other</dcterms:some_other_field>
    <dcterms:title>dcterms title</dcterms:title>
    <myschema:foo xmlns:myschema="http://example.org">bar</myschema:foo>
</entry>

This class doesn't provide editing/updating functions as the full etree API is exposed through the
attribute 'entry'. For example:

>>> len(e.entry.getchildren())
14

Instance Methods [hide private]
 
__init__(self, **kw)
Create a basic `Entry` document, setting the generator and a timestamp for the updated element value.
source code
 
register_namespace(self, prefix, uri)
Registers a namespace,, making it available for use when adding subsequent fields to the entry.
source code
 
add_field(self, k, v)
Append a single key-value pair to the `Entry` document.
source code
 
add_fields(self, **kw)
Add in multiple elements in one method call.
source code
 
add_author(self, name, uri=None, email=None)
Convenience function to add in the atom:author elements in the fashion required for Atom
source code
 
__str__(self)
Export the XML to a bytestring, ready for use
source code
 
pretty_print(self)
A version of the XML document which should be slightly more readable on the command line.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
  atom_fields = ['title', 'id', 'updated', 'summary']
  add_ns = ['dcterms', 'atom', 'app']
  bootstrap = '<?xml version="1.0"?>\n<entry xmlns="http://www.w...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, **kw)
(Constructor)

source code 

Create a basic `Entry` document, setting the generator and a timestamp for the updated element value.

Any keyword parameters passed in will be passed to the add_fields method and added to the entry bootstrap document. It's currently not possible to add a namespace and use it within the init call.

Overrides: object.__init__

register_namespace(self, prefix, uri)

source code 

Registers a namespace,, making it available for use when adding subsequent fields to the entry.

Registration will also affect the XML export, adding in the xmlns:prefix="url" attribute when required.

add_field(self, k, v)

source code 

Append a single key-value pair to the `Entry` document.

eg

>>> e.add_field("myprefix_fooo", "value")

It is advisable to use the `Entry.add_fields` method instead as this is neater and simplifies element entry.

Note that the atom:author field is handled differently, as it requires certain fields from the author:

>>> e.add_field("author", {'name':".....",
                           'email':"....",
                           'uri':"...."} )

Note that this means of entry is not supported for other elements.

add_fields(self, **kw)

source code 

Add in multiple elements in one method call.

Eg:

>>> e.add_fields(dcterms_title="Origin of the Species",
                dcterms_contributor="Darwin, Charles")

__str__(self)
(Informal representation operator)

source code 

Export the XML to a bytestring, ready for use

Overrides: object.__str__

Class Variable Details [hide private]

bootstrap

Value:
'''<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
        xmlns:dcterms="http://purl.org/dc/terms/">
    <generator uri="http://bitbucket.org/beno/python-sword2" version="\
0.1"/>
</entry>'''