Skip to content
Richard Jones edited this page Jan 3, 2017 · 2 revisions

Install

The package is in the pypi cheeseshop so can be installed via:

easy_install sword2

Use

The key classes in the module are Connection (the HTTP/SWORD2 client) and Entry (easy means to create metadata (atom) entry documents).

Connect to a sword server using the URL for its service document (SD_URI), pick a collection and submit a zip file to the collection

from sword2 import Connection
c = Connection(SD_URI, user_name = "foo", user_pass="bar")
c.get_service_document()

# pick the first collection within the first workspace:
workspace_1_title, workspace_1_collections = c.workspaces[0]
collection = workspace_1_collections[0]

# upload "package.zip" to this collection as a new (binary) resource:
with open("package.zip", "r") as pkg:
    receipt = c.create(col_iri = collection.href,
                                payload = pkg,
                                mimetype = "application/zip",
                                filename = "package.zip",
                                packaging = 'http://purl.org/net/sword/package/Binary',
                                in_progress = True)    # As the deposit isn't yet finished


# Add a metadata record to this newly created resource (or 'container')
from sword2 import Entry
# Entry can be passed keyword parameters to add metadata to the entry (namespace + '_' + tagname)
e = Entry(id="atomid", 
          title="atom-title",
          dcterms_abstract = "Info about the resource....",
          etc... )
# to add a new namespace:
e.register_namespace('skos', 'http://www.w3.org/2004/02/skos/core#')
e.add_field("skos_Concept", "...")


# Update the metadata entry to the resource:
updated_receipt = c.update(metadata_entry = e,
                           dr = receipt,   # use the receipt to discover the right URI to use
                           in_progress = False)  # finish the deposit