swh.deposit.utils module#

swh.deposit.utils.normalize_date(date)[source]#

Normalize date fields as expected by swh workers.

If date is a list, elect arbitrarily the first element of that list

If date is (then) a string, parse it through dateutil.parser.parse to extract a datetime.

Then normalize it through swh.model.model.TimestampWithTimezone

Returns

The swh date object

swh.deposit.utils.compute_metadata_context(swhid_reference: QualifiedSWHID) Dict[str, Any][source]#

Given a SWHID object, determine the context as a dict.

swh.deposit.utils.parse_swh_metadata_provenance(metadata: Element) str | None[source]#

Parse swh metadata-provenance within the metadata dict reference if found, None otherwise.

<swh:deposit>
  <swh:metadata-provenance>
    <schema:url>https://example.org/metadata/url</schema:url>
  </swh:metadata-provenance>
</swh:deposit>
Parameters:

metadata – result of parsing an Atom document with parse_xml()

Raises:

ValidationError in case of invalid xml

Returns:

Either the metadata provenance url if any or None otherwise

swh.deposit.utils.parse_swh_deposit_origin(metadata: Element) Tuple[str | None, str | None][source]#

Parses <swh:add_to_origin> and <swh:create_origin> from metadata document, if any.

<swh:deposit>
  <swh:create_origin>
    <swh:origin url='https://example.org/repo/software123/'/>
  </swh:reference>
</swh:deposit>
<swh:deposit>
  <swh:add_to_origin>
    <swh:origin url='https://example.org/repo/software123/'/>
  </swh:add_to_origin>
</swh:deposit>
Returns:

tuple of (origin_to_create, origin_to_add). If both are non-None, this should typically be an error raised to the user.

swh.deposit.utils.parse_swh_reference(metadata: Element) QualifiedSWHID | str | None[source]#

Parse <swh:reference> within the metadata document, if any.

<swh:deposit>
  <swh:reference>
    <swh:origin url='https://github.com/user/repo'/>
  </swh:reference>
</swh:deposit>

or:

<swh:deposit>
  <swh:reference>
    <swh:object swhid="swh:1:dir:31b5c8cc985d190b5a7ef4878128ebfdc2358f49;origin=https://hal.archives-ouvertes.fr/hal-01243573;visit=swh:1:snp:4fc1e36fca86b2070204bedd51106014a614f321;anchor=swh:1:rev:9c5de20cfb54682370a398fcc733e829903c8cba;path=/moranegg-AffectationRO-df7f68b/" />
</swh:deposit>
Parameters:

metadata – result of parsing an Atom document

Raises:

ValidationError in case the swhid referenced (if any) is invalid

Returns:

Either swhid or origin reference if any. None otherwise.

swh.deposit.utils.extended_swhid_from_qualified(swhid: QualifiedSWHID) ExtendedSWHID[source]#

Used to get the target of a metadata object from a <swh:reference>, as the latter uses a QualifiedSWHID.

Build a single header link.

>>> link_next = to_header_link("next-url", "next")
>>> link_next
'<next-url>; rel="next"'
>>> ','.join([link_next, to_header_link("prev-url", "prev")])
'<next-url>; rel="next",<prev-url>; rel="prev"'