Zum Inhalt

common.metadata

Overview

common.metadata contains helper functions for reading article and journal metadata, extracting bibliographic fields, generating preservation-related metadata, constructing identifiers and citations, and assembling the metadata dictionary used by downstream WDM modules.

The module primarily works with JSON metadata files produced earlier in the harvesting workflow and exposes small field-specific helpers as well as the higher-level get_article_metadata() function.

Its responsibilities include:

  • extracting author, title, rights, publication, issue, volume, page, DOI, ISSN, and publisher information,
  • resolving fixed organizational metadata for ULB Darmstadt,
  • reading Toolbox identifiers,
  • creating article identifier lists,
  • reporting the BagIt software agent and bagging date,
  • constructing OpenURL-style bibliographic citation metadata,
  • creating a dcterms:isPartOf ISSN identifier,
  • assembling the complete metadata structure required by downstream processing.

Dependencies

Standard library:

  • datetime
  • json
  • pathlib
  • sys
  • urllib.parse
  • importlib.metadata.version

External/runtime dependencies:

  • prefect.get_run_logger

Project dependencies:

  • constants
  • exceptions
  • utils

Constants

MIN_BAGIT_PYTHON_VERSION

MIN_BAGIT_PYTHON_VERSION = (3, 8)

Minimum Python major/minor version for which the installed BagIt package version is queried with importlib.metadata.version().


API Reference

common.metadata.get_article_author(article_metadata_path)

Reads the article metadata JSON file and returns a list of author names.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

list[str]

List of author names.

For each author:

  • if a name field exists, that value is used directly;
  • otherwise given and family are combined;
  • authors without either name component are skipped.

Raises

exceptions.AuthorNameNotFound
Raised when the article metadata does not contain the configured Crossref author field.

json.JSONDecodeError
May be raised if the metadata file does not contain valid JSON.

Filesystem exceptions are not caught.


common.metadata.get_article_title(article_metadata_path)

Returns the article title from the metadata file.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Returns:

  • the first item when the configured title value is a list,
  • the string directly when it is already a string.

If the title key exists but contains another type, the function implicitly returns None.

Raises

exceptions.ArticleTitleNotFound
Raised when the configured article-title field is missing.

Other file and JSON exceptions propagate.


common.metadata.get_article_rights(article_metadata_path)

Extracts article rights/license URLs from the metadata file.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

list[str]

List containing the URL value from every rights entry.

Raises

exceptions.LicenseInfoNotFound
Raised when the configured rights field is missing or when a rights entry does not contain URL.


common.metadata.get_article_publication_year(article_metadata_path)

Returns the publication year from the Crossref publication-date structure.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

The first year value from:

metadata[<publication-field>]["date-parts"][0][0]

Raises

exceptions.ArticlePublicationYearNotFound
Raised when a required dictionary key is missing.

Index-related errors are not converted to the custom exception.


common.metadata.get_article_issued_year(article_metadata_path)

Returns the issued year from the Crossref issued-date structure.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

The first year value from the configured issued date.

Raises

exceptions.ArticleIssuedYearNotFound
Raised when a required key is missing.


common.metadata.get_short_container_title(article_metadata_path)

Returns the short container title associated with the article.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Returns:

  • the first item when the configured value is a list,
  • the value directly when it is a string.

Raises

exceptions.ArticleShortContainerTitleNotFound
Raised when the configured short-container-title field is missing.


common.metadata.get_article_publisher(article_metadata_path)

Returns the publisher recorded in the article metadata.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Publisher value stored under the configured Crossref publisher key.

Raises

exceptions.ArticlePublisherNotFound
Raised when the publisher key is missing.


common.metadata.get_journal_issue(article_metadata_path)

Returns the journal issue associated with the article.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Configured issue value.

Raises

exceptions.JournalIssueNotFound
Raised when the issue key is missing.


common.metadata.get_journal_volume(article_metadata_path)

Returns the journal volume associated with the article.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Configured volume value.

Raises

exceptions.JournalVolumeNotFound
Raised when the volume key is missing.


common.metadata.get_journal_title(journal_metadata_path)

Returns the journal title from journal-level metadata.

Parameters

journal_metadata_path
Path to the journal metadata JSON file.

Returns

Configured journal title value.

Raises

exceptions.JournalTitleNotFound
Raised when the journal title key is missing.


common.metadata.get_article_start_page(article_metadata_path)

Returns the first page of the article's configured page range.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

If the page value contains a hyphen, returns the segment before the first hyphen.

Otherwise returns the complete page value.

Raises

exceptions.ArticlePageInJournalNotFound
Raised when the configured page field is missing.


common.metadata.get_article_doi(article_metadata_path)

Returns the DOI recorded in article metadata.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

Configured DOI value.

Raises

exceptions.ArticleDoiNotFound
Raised when the DOI field is missing.


common.metadata.get_journal_issn(journal_metadata_path)

Returns a journal ISSN from journal-level metadata.

Parameters

journal_metadata_path
Path to the journal metadata JSON file.

Returns

If the configured ISSN value is:

  • a list, the first item is returned;
  • a string, the string is returned.

If the key exists with another type, the function implicitly returns None.

Raises

exceptions.JournalIssnNotFound
Raised when the configured ISSN key is missing.


common.metadata.get_source_organization()

Returns the fixed source-organization name used by WDM metadata.

Parameters

None.

Returns

Universitäts- und Landesbibliothek Darmstadt

common.metadata.get_organization_address()

Returns the fixed organization address.

Parameters

None.

Returns

Magdalenenstraße 8, 64289 Darmstadt, Deutschland

common.metadata.get_contact_email()

Returns the fixed WDM contact email address.

Parameters

None.

Returns

wdm@ulb.tu-darmstadt.de

common.metadata.get_external_description(article_metadata_path)

Builds a short external description from the article's short container title.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

A string in the form:

Article from '<short container title>'

Raises

exceptions.ArticleShortContainerTitleNotFound
Propagated from get_short_container_title().


common.metadata.get_external_identifier(module_name: str, latest_previous_module: pathlib.Path)

Reads the Toolbox text identifier from the current module's temporary working directory.

Parameters

module_name: str
Name of the current module.

latest_previous_module: pathlib.Path
Path to the latest output folder of the previous module.

Returns

The first line of the file configured by:

constants.TOOLBOX_TEXT_ID_FILE

Raises

Filesystem exceptions propagate when the identifier file cannot be read.

Processing

  1. Resolve the current temporary module directory with utils.get_module_temp_directory().
  2. Append the Toolbox identifier filename.
  3. Open the file.
  4. Return the first line.

common.metadata.get_article_identifiers(module_name, article_metadata_path, latest_previous_module)

Builds the identifier list associated with an article.

Parameters

module_name
Current module name.

article_metadata_path
Path to the article metadata JSON file.

latest_previous_module
Path to the latest previous-module directory.

Returns

list[str]

Identifier list containing:

  • a DOI URL when the DOI exists and does not already start with constants.DOI_ORG_URL,
  • the Toolbox identifier read by get_external_identifier().

Raises

Filesystem exceptions from reading the Toolbox identifier propagate.

Logging

If the DOI is missing, the function logs a warning and continues.

Notes

If the DOI already begins with constants.DOI_ORG_URL, the current implementation does not append that DOI value to the result list.

The Toolbox identifier is always appended unless reading it fails.


common.metadata.get_bag_software_agent()

Builds the BagIt software-agent description.

Parameters

None.

Returns

String in the form:

bagit.py v<version> <https://github.com/LibraryOfCongress/bagit-python>

Raises

importlib.metadata.PackageNotFoundError may be raised when running on Python 3.8 or newer if the bagit package is not installed.

Notes

For Python versions below MIN_BAGIT_PYTHON_VERSION, the version portion remains empty.


common.metadata.get_bagging_date()

Returns the current local calendar date.

Parameters

None.

Returns

str

Date produced by:

str(datetime.date.today())

typically in YYYY-MM-DD format.


common.metadata.get_bibliographic_citation(article_metadata_path)

Builds an OpenURL-style journal bibliographic citation string from article metadata.

Parameters

article_metadata_path
Path to the article metadata JSON file.

Returns

str

Citation string beginning with:

&ctx_ver=Z39.88-2004&rft_val_fmt=info:ofi/fmt:kev:mtx:journal

Optional values are appended when available:

  • rft.jtitle
  • rft.issue
  • rft.volume
  • rft.spage

Processing

The function attempts to resolve:

  1. short container title,
  2. issue,
  3. volume,
  4. start page.

Each value is URL-encoded using:

urllib.parse.quote(value, safe="")

If the corresponding custom "not found" exception is raised, that field is simply omitted.

Raises

Other errors not represented by the handled custom exceptions propagate.


common.metadata.get_dcterms_ispartof(article_metadata_path)

Constructs a dcterms:isPartOf-style ISSN URN for the article's journal.

Parameters

article_metadata_path
Path to article metadata.

The implementation expects a pathlib.Path-compatible object with .parent.

Returns

When the journal ISSN is available:

urn:ISSN:<ISSN>

When the journal ISSN cannot be found:

(an empty string)

Processing

The journal directory is derived using:

article_metadata_path.parent.parent.parent

The journal metadata filename is then appended using constants.JOURNAL_METADATA_FILE_NAME.


common.metadata.get_article_metadata(module_name: str, latest_previous_module: pathlib.Path)

Builds the metadata dictionary used by downstream WDM processing.

This is the main aggregation function in the module.

Parameters

module_name: str
Name of the current WDM module.

latest_previous_module: pathlib.Path
Path to the latest previous-module output folder.

Returns

dict

The returned dictionary contains the following keys:

{
    "authors": None,
    "title": None,
    "rights": None,
    "identifier": None,
    "issued_year": None,
    "publisher": None,
    "bagging-date": None,
    "external-description": None,
    "external-identifier": None,
    "source-organization": None,
    "organization-address": None,
    "contact-email": None,
    "bag-software-agent": None,
    "is_part_of": None,
    "bibliographic_citation": None,
}

Available values are populated according to the module and available source metadata.

Raises

The function catches several field-specific custom metadata exceptions and logs missing values.

Other filesystem, JSON, package-version, or helper-function exceptions propagate.

Processing

  1. Resolve the current temporary module directory.
  2. Build the path to the article metadata JSON file.
  3. Initialize the metadata dictionary with None values.
  4. Attempt to populate:
  5. authors,
  6. title,
  7. rights,
  8. issued year,
  9. publisher,
  10. external description.
  11. If the issued year is missing, fall back to the publication year.
  12. If module_name == constants.MODULE_VALIDATE_DATA, return immediately.
  13. Otherwise additionally populate:
  14. identifier,
  15. bagging date,
  16. source organization,
  17. organization address,
  18. contact email,
  19. BagIt software agent,
  20. is_part_of,
  21. bibliographic citation.
  22. Return the final dictionary.

Issued-Year Fallback

The function first calls:

get_article_issued_year()

If exceptions.ArticleIssuedYearNotFound is raised, it then tries:

get_article_publication_year()

and logs that the publication year was used as a fallback.

Validate-Metadata Behavior

When:

module_name == constants.MODULE_VALIDATE_DATA

the function returns before adding preservation- and export-oriented metadata such as:

  • identifiers,
  • bagging date,
  • organization information,
  • software agent,
  • is_part_of,
  • bibliographic citation.

Logging

Missing metadata fields are logged using the current Prefect run logger.


common.metadata.get_journal_publisher(journal_metadata_path)

Returns the publisher recorded in journal-level metadata.

Parameters

journal_metadata_path
Path to the journal metadata JSON file.

Returns

Configured journal publisher value.

Raises

exceptions.JournalPublisherNotFound
Raised when the configured publisher key is missing.


Metadata Field Groups

Article-Level Fields

The following helpers read article metadata:

  • get_article_author()
  • get_article_title()
  • get_article_rights()
  • get_article_publication_year()
  • get_article_issued_year()
  • get_short_container_title()
  • get_article_publisher()
  • get_journal_issue()
  • get_journal_volume()
  • get_article_start_page()
  • get_article_doi()

Journal-Level Fields

The following helpers read journal metadata:

  • get_journal_title()
  • get_journal_issn()
  • get_journal_publisher()

Fixed Organizational Metadata

  • get_source_organization()
  • get_organization_address()
  • get_contact_email()

Derived Preservation and Export Metadata

  • get_external_description()
  • get_external_identifier()
  • get_article_identifiers()
  • get_bag_software_agent()
  • get_bagging_date()
  • get_bibliographic_citation()
  • get_dcterms_ispartof()
  • get_article_metadata()

Operational Considerations

Repeated JSON Reads

Most field-specific helpers independently open and parse the metadata JSON file.

A caller requesting many fields through individual helpers therefore causes repeated file reads. get_article_metadata() currently uses those helpers rather than loading the JSON once and passing the parsed data through.

Custom Exceptions

The module translates many missing Crossref fields into project-specific exceptions from exceptions.py.

This allows higher-level code such as get_article_metadata() to distinguish missing optional metadata from unrelated processing failures.

Partial Metadata

get_article_metadata() is intentionally tolerant of several missing fields.

When a handled field-specific exception occurs:

  • a log message is written,
  • the corresponding result remains None,
  • processing continues.

Identifier Handling

The identifier list combines DOI-based and Toolbox-based identifiers.

The current implementation only appends the DOI when it does not already begin with constants.DOI_ORG_URL.

Path Assumptions

Some helpers derive related files by navigating parent directories.

In particular, get_dcterms_ispartof() assumes the article metadata path is nested deeply enough for:

article_metadata_path.parent.parent.parent

to resolve to the journal directory.

BagIt Version Lookup

On supported Python versions, get_bag_software_agent() expects the bagit package to be installed and discoverable through importlib.metadata.

Date Semantics

get_bagging_date() uses datetime.date.today(), which reflects the local system date rather than an explicitly UTC-normalized date.