Zum Inhalt

common.ddc_utils

Overview

common.ddc_utils contains helper functions for retrieving DDC-related subject information from the German National Library's ZDB SRU service and for maintaining local files used by DDC processing.

The module provides functionality for:

  • querying subject data for an ISSN,
  • constructing the path to DDC.txt,
  • locating the persistent or temporary missing_ddc.txt,
  • adding ISSNs to the missing-DDC list,
  • removing ISSNs from the missing-DDC list.

Dependencies

  • pathlib
  • http.HTTPStatus
  • xml.etree.ElementTree
  • requests
  • constants
  • utils

Constants

DDC_FILE

DDC_FILE = "DDC.txt"

Filename used for the DDC file.

MISSING_DDC_FILE

MISSING_DDC_FILE = "missing_ddc.txt"

Filename used for the list of ISSNs lacking DDC information.


External Service

fetch_ddc() sends an HTTP GET request to the ZDB SRU service.

The request is constructed with:

version=1.1
operation=searchRetrieve
query=iss=<ISSN>
recordSchema=RDFxml

The returned XML is searched for dc:subject elements in the namespace:

http://purl.org/dc/elements/1.1/

API Reference

common.ddc_utils.fetch_ddc(issn)

Fetches subject information for an ISSN from the ZDB SRU service.

Parameters

issn
ISSN inserted into the SRU query.

The function does not perform explicit ISSN-format validation.

Returns

tuple[bool, list | None]

For an HTTP 200 OK response:

(True, subjects)

where subjects contains the text of all matching dc:subject elements.

For any other HTTP status:

(False, None)

Raises

The function does not catch exceptions from requests.get().

xml.etree.ElementTree.ParseError may propagate when the response body cannot be parsed as XML.

Other exceptions raised during response processing also propagate.

Processing

  1. Construct the ZDB SRU request URL.
  2. Send the request using requests.get().
  3. Check whether the response status is HTTPStatus.OK.
  4. Return (False, None) for another status.
  5. Parse the response body as XML.
  6. Find all dc:subject elements.
  7. Collect each element's .text value.
  8. Return (True, subjects).

Notes

The function does not filter out None if a matching dc:subject element contains no text.


common.ddc_utils.get_ddc_file_path(root: pathlib.Path)

Constructs the path to DDC.txt below a supplied root directory.

Parameters

root: pathlib.Path
Root directory under which the DDC file should be located.

Returns

pathlib.Path

Equivalent to:

root / "DDC.txt"

Raises

No exceptions are explicitly raised by the function.

Side Effects

None.


common.ddc_utils.get_missing_ddc_file(is_temp=False) -> pathlib.Path

Returns the path to the persistent or temporary missing-DDC file.

Parameters

is_temp: bool = False
Selects the returned filename.

  • False: <data_root>/missing_ddc.txt
  • True: the filename is prefixed with constants.TEMP_DIRECTORY_IDENTIFIER

Returns

pathlib.Path

Path to the requested missing-DDC file.

Raises

Exceptions from utils.get_data_root() or path construction are not caught.

Processing

The WDM data root is obtained using:

utils.get_data_root()

For the persistent file:

root.joinpath(MISSING_DDC_FILE)

For the temporary file:

root.joinpath(
    constants.TEMP_DIRECTORY_IDENTIFIER + MISSING_DDC_FILE
)

Side Effects

None. The function does not create the file.


common.ddc_utils.add_entry_to_missing_ddc(issn: str)

Adds an ISSN to missing_ddc.txt if a matching line is not already present.

If the file does not exist, it is created first.

Parameters

issn: str
ISSN value to add to the missing-DDC file.

Returns

None

The function modifies a file and does not explicitly return a value.

Raises

Filesystem exceptions raised while creating, opening, reading, or writing the file are not caught.

Processing

  1. Resolve the path with get_missing_ddc_file().
  2. Create an empty file when it does not already exist.
  3. Open the file in r+ mode.
  4. Iterate through existing lines.
  5. Check whether issn occurs in a line.
  6. Append issn + "\n" if no match is found.

Duplicate Detection

Duplicate detection uses substring matching:

if issn in line:

The comparison is not an exact stripped-line comparison.

Side Effects

The function may create missing_ddc.txt and append a new line.


common.ddc_utils.remove_entry_from_missing_ddc(issn)

Removes lines containing the supplied ISSN from missing_ddc.txt.

The update is written to a temporary file, which then replaces the original.

Parameters

issn
ISSN string used to identify lines that should be removed.

Returns

None

If missing_ddc.txt does not exist, the function returns immediately.

Raises

Filesystem exceptions raised while reading, writing, replacing, or opening files are not caught.

Processing

  1. Resolve the persistent missing-DDC path.
  2. Return if the file does not exist.
  3. Resolve the temporary missing-DDC path.
  4. Open the original file for reading.
  5. Open the temporary file for writing.
  6. Copy all lines that do not contain the ISSN.
  7. Replace the original file with the temporary file.

Matching Behavior

Filtering uses substring matching:

if issn not in line:

A line is removed whenever the ISSN string appears anywhere in that line.

Temporary File Strategy

After writing the filtered content, the function calls:

temp_missing_ddc_path.replace(missing_ddc_path)

Side Effects

The function may create or overwrite the temporary missing-DDC file and replace the persistent missing_ddc.txt.


File Lifecycle

missing_ddc.txt
      |
      | add_entry_to_missing_ddc()
      v
ISSN appended if no matching line exists

missing_ddc.txt
      |
      | remove_entry_from_missing_ddc()
      v
temporary file created
      |
      v
non-matching lines copied
      |
      v
temporary file replaces missing_ddc.txt

Operational Considerations

HTTP Error Handling

fetch_ddc() returns (False, None) for HTTP statuses other than 200 OK.

It does not implement retries or use a shared API retry helper.

Request Timeout

No explicit timeout is passed to requests.get().

XML Parsing

The response is parsed directly with ElementTree.fromstring().

Malformed XML therefore raises a parsing exception instead of returning (False, None).

Missing-DDC Matching

Both addition and removal use substring matching rather than exact line equality.

File Encoding

remove_entry_from_missing_ddc() explicitly uses UTF-8.

add_entry_to_missing_ddc() opens its files without specifying an encoding.