swh.deposit.utils module

swh.deposit.utils.parse_xml(stream, encoding='utf-8')[source]
swh.deposit.utils.merge(*dicts)[source]

Given an iterator of dicts, merge them losing no information.

Parameters

*dicts – arguments are all supposed to be dict to merge into one

Returns

dict merged without losing information

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.identifiers.normalize_timestamp.

Returns

The swh date object

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

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

swh.deposit.utils.parse_swh_reference(metadata: Dict) Optional[Union[swh.model.swhids.QualifiedSWHID, str]][source]

Parse swh reference within the metadata dict (or origin) reference if found, None otherwise.

<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 with parse_xml()

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: swh.model.swhids.QualifiedSWHID) swh.model.swhids.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"'