Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifies new OCLC MARC record fields 007 and 040 #1395

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions libsys_airflow/plugins/data_exports/oclc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,41 @@ def oclc_records_operation(**kwargs) -> dict:
return {"success": success, "failures": failures, "archive": archive_files}


def __mod_007__(field_007: pymarc.Field):
mod_007 = []
for i, char in enumerate(field_007.value()):
if i == 0 or i == 2:
mod_007.append(char)
continue
if char == " ":
mod_007.append("|")
continue
mod_007.append(char)
field_007.data = "".join(mod_007)


def __mod_040__(field_040: pymarc.Field):
mod_subfields = []
for subfield in field_040.subfields:
if "CSt" in subfield.value:
subfield = pymarc.Subfield(code=subfield.code, value="STF")
mod_subfields.append(subfield)
field_040.subfields = mod_subfields


def __oclc_marc_modifications__(marc_record: pymarc.Record) -> pymarc.Record:
"""
Modifies the 007 and 040 fields for new OCLC records. For 007 field,
replace any spaces with |. For 040 substitute CSt with STF for all
subfields.
"""
for field in marc_record.get_fields("007"):
__mod_007__(field)
for field in marc_record.get_fields("040"):
__mod_040__(field)
return marc_record


class OCLCAPIWrapper(object):
# Helper class for transmitting MARC records to OCLC Worldcat API

Expand Down Expand Up @@ -468,6 +503,7 @@ def __new_oclc__(**kwargs):

export_record = copy.deepcopy(record)
export_record.remove_fields(*oclc_excluded)
export_record = __oclc_marc_modifications__(export_record)

marc21 = export_record.as_marc21()

Expand Down
37 changes: 36 additions & 1 deletion tests/data_exports/test_oclc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def sample_marc_records():
sample = []
marc_record = pymarc.Record()
marc_record.add_field(
pymarc.Field(tag="007", data="cr un "),
pymarc.Field(
tag="040",
indicators=pymarc.Indicators(" ", " "),
subfields=[
pymarc.Subfield("a", "CSt"),
pymarc.Subfield("b", "eng"),
pymarc.Subfield("e", "rda"),
pymarc.Subfield("c", "CSt"),
],
),
pymarc.Field(
tag="100",
indicators=['1', ''],
Expand All @@ -31,11 +42,21 @@ def sample_marc_records():
sample.append(marc_record)
no_srs_record = pymarc.Record()
no_srs_record.add_field(
pymarc.Field(tag="007", data="cr un a "),
pymarc.Field(
tag="040",
indicators=pymarc.Indicators(" ", " "),
subfields=[
pymarc.Subfield("a", "CSt"),
pymarc.Subfield("e", "rda"),
pymarc.Subfield("c", "CSt"),
],
),
pymarc.Field(
tag='245',
indicators=[' ', ' '],
subfields=[pymarc.Subfield(code='a', value='Much Ado about Something')],
)
),
)
sample.append(no_srs_record)
another_record = pymarc.Record()
Expand Down Expand Up @@ -1007,6 +1028,20 @@ def test_oclc_records_operation_no_records(mock_oclc_api, caplog):
assert "No delete records for STF" in caplog.text


def test_oclc_marc_modifications():
sample_records = sample_marc_records()
mod_record = oclc_api.__oclc_marc_modifications__(sample_records[0])

assert mod_record["007"].value() == "cr un|||||||||"
assert mod_record["040"]["a"] == "STF"
assert mod_record["040"]["b"] == "eng"

second_mod = oclc_api.__oclc_marc_modifications__(sample_records[1])

assert second_mod["007"].value() == "cr un|||||a||||"
assert second_mod["040"]["a"] == "STF"


def test_oclc_records_operation(mocker, mock_oclc_api, tmp_path):
connections = {"STF": {"username": "sul-admin", "password": "123245"}}

Expand Down
Loading