CodeMeta and CFF
bibliofabric.codemeta parses the two software citation metadata files a
repository carries: codemeta.json (JSON-LD, the
CodeMeta vocabulary) and CITATION.cff (YAML,
the Citation File Format). The
parsers are pure functions with no HTTP client behind them: you give them a
path, text, or a parsed mapping, and they return typed Pydantic models or
raise bibliofabric.ValidationError. There is no CodemetaClient, because
no base URL exists to point one at.
Both formats appear together in research software repositories, and Zenodo and Software Heritage also serve them as metadata; a parser covers them wherever you obtained the file.
Quick start
from bibliofabric.codemeta import parse_codemeta_file, parse_cff_file
record = parse_codemeta_file("codemeta.json")
print(record.entity_type, "-", record.name)
print("license:", record.license[0])
for author in record.author or []:
print(
"author:",
author.entity_type,
author.name or f"{author.given_name} {author.family_name}",
)
cff = parse_cff_file("CITATION.cff")
print(cff.title, cff.version)
for author in cff.authors:
print("author:", author.given_names, author.family_names)
preferred = cff.preferred_citation
if preferred is not None:
print(
"preferred citation:",
preferred.type,
preferred.publisher,
f"pp. {preferred.start}-{preferred.end}",
)
SoftwareSourceCode - PyMap3d
license: https://spdx.org/licenses/BSD-2-Clause
author: Organization SciVision
kani None
author: Andrew Zhu
author: Liam Dugan
author: Alyssa Hwang
author: Chris Callison-Burch
preferred citation: generic Association for Computational Linguistics pp. 65-77
The text variants parse_codemeta and parse_cff take a string or bytes
body (or an already parsed mapping), which fits metadata pulled from an API
or a git blob instead of a file on disk.
Validation
Validation is the point of the parser, not a formality. A document with a
term the format does not define raises bibliofabric.ValidationError, so a
harvest cannot pull a drifted file into your data unnoticed:
codemeta.jsonmust carry a@contextURL that names CodeMeta and an@typeofSoftwareSourceCode,SoftwareApplication,WebApplication, orWebAPI. Every other key must be a CodeMeta term; the field list covers the union of the published CodeMeta 2.0, 3.0, and 4.0 contexts, and the parser accepts the term spellings of all three versions.CITATION.cffmust carrycff-version,title,message, andauthors, and every key must be in the published 1.2.0 schema. Files that declare an oldercff-versionstill parse when their keys fit.- Malformed JSON or YAML raises too; the exception carries the raw payload
in
raw.
See Upstream quirks for the drift real files show.
JSON-LD forms
The parser reads the flat single-node form, with or without the @ on the
JSON-LD keywords: "@type" and "type", "@id" and "id" all map to the
same fields, because real files use both spellings. An @context array is
accepted when one of its string entries names CodeMeta (codemetar writes
["http://purl.org/codemeta/2.0", "http://schema.org"]); the parser keeps
that entry. The @graph array form is rejected; parse the node you want
instead. Structured @context objects are rejected.
Errors
Everything raises bibliofabric.ValidationError, including unknown terms,
missing required keys, and undecodable files; see
Errors for the exception hierarchy.
Examples
A runnable script lives in packages/bibliofabric/examples/codemeta:
quickstart.py: parse the two fixture files and print a summary, or parse thecodemeta.jsonandCITATION.cffof a directory you pass.
Next
- Upstream quirks: what real files do that the specs do not promise.
- Reference: the typed API of the parsers and models.