Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | def _url(url: str) -> str: | ||||
""" | """ | ||||
if not url.endswith("/1"): | if not url.endswith("/1"): | ||||
url = "%s/1" % url | url = "%s/1" % url | ||||
return url | return url | ||||
def generate_metadata( | def generate_metadata( | ||||
deposit_client: str, name: str, external_id: str, authors: List[str] | deposit_client: str, name: str, external_id: Optional[str], authors: List[str] | ||||
) -> str: | ) -> str: | ||||
"""Generate sword compliant xml metadata with the minimum required metadata. | """Generate sword compliant xml metadata with the minimum required metadata. | ||||
The Atom spec, https://tools.ietf.org/html/rfc4287, says that: | The Atom spec, https://tools.ietf.org/html/rfc4287, says that: | ||||
- atom:entry elements MUST contain one or more atom:author elements | - atom:entry elements MUST contain one or more atom:author elements | ||||
- atom:entry elements MUST contain exactly one atom:title element. | - atom:entry elements MUST contain exactly one atom:title element. | ||||
- atom:entry elements MUST contain exactly one atom:updated element. | - atom:entry elements MUST contain exactly one atom:updated element. | ||||
Show All 17 Lines | ) -> str: | ||||
Returns: | Returns: | ||||
metadata xml string | metadata xml string | ||||
""" | """ | ||||
import xmltodict | import xmltodict | ||||
# generate a metadata file with the minimum required metadata | # generate a metadata file with the minimum required metadata | ||||
codemetadata = { | document = { | ||||
"atom:entry": { | "atom:entry": { | ||||
"@xmlns:atom": "http://www.w3.org/2005/Atom", | "@xmlns:atom": "http://www.w3.org/2005/Atom", | ||||
"@xmlns:codemeta": "https://doi.org/10.5063/SCHEMA/CODEMETA-2.0", | "@xmlns:codemeta": "https://doi.org/10.5063/SCHEMA/CODEMETA-2.0", | ||||
"codemeta:identifier": external_id, | |||||
"atom:updated": datetime.now(tz=timezone.utc), # mandatory, cf. docstring | "atom:updated": datetime.now(tz=timezone.utc), # mandatory, cf. docstring | ||||
"atom:author": deposit_client, # mandatory, cf. docstring | "atom:author": deposit_client, # mandatory, cf. docstring | ||||
"atom:title": name, # mandatory, cf. docstring | "atom:title": name, # mandatory, cf. docstring | ||||
"codemeta:name": name, # mandatory, cf. docstring | "codemeta:name": name, # mandatory, cf. docstring | ||||
"codemeta:author": [ # mandatory, cf. docstring | "codemeta:author": [ # mandatory, cf. docstring | ||||
{"codemeta:name": author_name} for author_name in authors | {"codemeta:name": author_name} for author_name in authors | ||||
], | ], | ||||
}, | }, | ||||
} | } | ||||
logging.debug("Metadata dict to generate as xml: %s", codemetadata) | if external_id: | ||||
return xmltodict.unparse(codemetadata, pretty=True) | document["atom:entry"]["codemeta:identifier"] = external_id | ||||
logging.debug("Atom entry dict to generate as xml: %s", document) | |||||
return xmltodict.unparse(document, pretty=True) | |||||
def _collection(client: PublicApiDepositClient) -> str: | def _collection(client: PublicApiDepositClient) -> str: | ||||
"""Retrieve the client's collection | """Retrieve the client's collection | ||||
""" | """ | ||||
# retrieve user's collection | # retrieve user's collection | ||||
sd_content = client.service_document() | sd_content = client.service_document() | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | Returns: | ||||
"collection": the user's collection under which to put the deposit | "collection": the user's collection under which to put the deposit | ||||
"slug": the slug or external id identifying the deposit to make | "slug": the slug or external id identifying the deposit to make | ||||
"in_progress": if the deposit is partial or not | "in_progress": if the deposit is partial or not | ||||
"url": deposit's server main entry point | "url": deposit's server main entry point | ||||
"deposit_id": optional deposit identifier | "deposit_id": optional deposit identifier | ||||
"swhid": optional deposit swhid | "swhid": optional deposit swhid | ||||
"replace": whether the given deposit is to be replaced or not | "replace": whether the given deposit is to be replaced or not | ||||
""" | """ | ||||
if not slug: # generate one as this is mandatory | |||||
slug = generate_slug() | |||||
if not metadata: | if not metadata: | ||||
if name and authors: | if name and authors: | ||||
metadata_path = os.path.join(temp_dir, "metadata.xml") | metadata_path = os.path.join(temp_dir, "metadata.xml") | ||||
logging.debug("Temporary file: %s", metadata_path) | logging.debug("Temporary file: %s", metadata_path) | ||||
metadata_xml = generate_metadata(username, name, slug, authors) | metadata_xml = generate_metadata(username, name, slug, authors) | ||||
logging.debug("Metadata xml generated: %s", metadata_xml) | logging.debug("Metadata xml generated: %s", metadata_xml) | ||||
with open(metadata_path, "w") as f: | with open(metadata_path, "w") as f: | ||||
f.write(metadata_xml) | f.write(metadata_xml) | ||||
▲ Show 20 Lines • Show All 287 Lines • Show Last 20 Lines |