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
pathlibhttp.HTTPStatusxml.etree.ElementTreerequestsconstantsutils
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
- Construct the ZDB SRU request URL.
- Send the request using
requests.get(). - Check whether the response status is
HTTPStatus.OK. - Return
(False, None)for another status. - Parse the response body as XML.
- Find all
dc:subjectelements. - Collect each element's
.textvalue. - 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.txtTrue: the filename is prefixed withconstants.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
- Resolve the path with
get_missing_ddc_file(). - Create an empty file when it does not already exist.
- Open the file in
r+mode. - Iterate through existing lines.
- Check whether
issnoccurs in a line. - 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
- Resolve the persistent missing-DDC path.
- Return if the file does not exist.
- Resolve the temporary missing-DDC path.
- Open the original file for reading.
- Open the temporary file for writing.
- Copy all lines that do not contain the ISSN.
- 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.