Writing METS Files

Building METS Documents

To add data to a metsrw.METSDocument, create and append metsrw.FSEntry objects.

>>> mets = metsrw.METSDocument()
>>> directory_1 = metsrw.FSEntry(label="test", path="test", type="Directory")
>>> file_1 = metsrw.FSEntry(
...     label="hello.pdf", path="test/hello.pdf", type="Item",
...     file_uuid=str(uuid.uuid4()))
>>> directory_1.children.append(file_1)
>>> file_2 = metsrw.FSEntry(
...    label="demo.jpg", path="test/demo.jpg", type="Item",
...    file_uuid=str(uuid.uuid4()))
>>> directory_1.children.append(file_2)
>>> mets.append_file(directory_1)
>>> mets.all_files()
{FSEntry(...), FSEntry(...)}

Adding metadata is done via the metsrw.FSEntry.

file_1 = metsrw.FSEntry(
    label="hello.pdf", path="test/hello.pdf", type="Item",
    file_uuid=str(uuid.uuid4()))

file_1.add_premis_object("<premis>object</premis>")
file_1.add_premis_event("<premis>event</premis>")
file_1.add_premis_agent("<premis>agent</premis>")
rights = file_1.add_premis_rights("<premis>rights</premis>")
dc = file_1.add_dublin_core("<dublincore>metadata</dublincore>")

# Replaces added metadata
rights.replace_with(file_1.add_premis_rights("<premis>newer rights</premis>"))

Serialization

metsrw supports serialization to file, bytes or lxml Element object.

>>> mets = metsrw.METSDocument()
>>> file1 = metsrw.FSEntry("hello.pdf", file_uuid=str(uuid.uuid4()))
>>> mets.append_file(file1)

# To file on disk
>>> mets.write(output_path)

# To _Element object
>>> mets.serialize()
<Element {http://www.loc.gov/METS/}mets ...>

# To bytes
>>> mets.tostring()
b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<mets:mets ...'