Metadata Cookbook

To illustrate metadata introspection, let’s open an existing document:

>>> from lpod.document import odf_get_document
>>> document = odf_get_document('http://example.com/odf/cookbook')

Metadata are accessible through the meta part:

>>> meta = document.get_meta()

You then get access to various getters and setters.

The getters return Python types and the respective setters take the same Python type as a parameter.

Accessing properties

The API has the form get_xxx and set_xxx(value).

Example of getting the title:

>>> meta.get_title()
Example Document for the Cookbook
>>> type(meta.get_title())
<type 'unicode'>

Example of setting the title:

>>> meta.set_title(u"First Example of a Long Series")

Notice that LpOD doesn’t increment editing cycles nor statistics when saving the document.

Properties helpers

For the metadata using dates or durations, lpOD provides datatypes that decode from and serialize back to strings.

Example for dates:

>>> from lpod.datatype import DateTime
>>> modification_date = DateTime.decode('2009-11-17T12:02:49')
>>> type(modification_date)
<type 'datetime.datetime'>
>>> metadata.set_modification_date(modification_date)

Example for durations:

>>> from lpod.datatype import Duration
>>> duration = Duration.decode('PT00H03M02S')
>>> type(duration)
<type 'datetime.timedelta'>
>>> meta.set_editing_duration(duration)

Of course you can use the datetime and timedelta constructors instead.

User-defined metadata

The ODF specification reserved place for free-form metadata for the user to fill in.

They are loaded as a dict:

>>> meta.get_user_defined_metadata()::
{}

You are allowed to store the following Python types: str, unicode, bool, int, float, Decimal, date, datetime, timedelta:

>>> meta.set_user_defined_metadata(u"lpOD Version", 'v0.7-67-g24c08f4')
>>> meta.get_user_defined_metadata()
{u'lpOD Version': u'v0.7-67-g24c08f4'}

Strings are always decoded as unicode, numeric values are always decoded as Decimal (as they offer the best precision).

For the whole list of metadata, consult the lpod.meta module.

Saving Metadata

We can’t write the document over HTTP:

>>> document.save('/tmp/metadata.odt')