rara-tools 0.0.12__py3-none-any.whl → 0.0.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rara-tools might be problematic. Click here for more details.
- rara_tools/converters.py +58 -18
- {rara_tools-0.0.12.dist-info → rara_tools-0.0.13.dist-info}/METADATA +1 -1
- {rara_tools-0.0.12.dist-info → rara_tools-0.0.13.dist-info}/RECORD +6 -6
- {rara_tools-0.0.12.dist-info → rara_tools-0.0.13.dist-info}/WHEEL +1 -1
- {rara_tools-0.0.12.dist-info → rara_tools-0.0.13.dist-info}/LICENSE.md +0 -0
- {rara_tools-0.0.12.dist-info → rara_tools-0.0.13.dist-info}/top_level.txt +0 -0
rara_tools/converters.py
CHANGED
|
@@ -2,40 +2,80 @@ from .exceptions import SierraResponseConverterException
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class SierraResponseConverter:
|
|
5
|
-
"""
|
|
6
|
-
and converts it to MARC-in-JSON format.
|
|
7
|
-
|
|
8
|
-
"""
|
|
5
|
+
"""Converts a JSON response from the Sierra API to MARC-in-JSON format."""
|
|
9
6
|
|
|
10
7
|
def __init__(self, response: dict):
|
|
11
8
|
if not isinstance(response, dict):
|
|
12
9
|
raise SierraResponseConverterException("Please provide a valid JSON response.")
|
|
13
10
|
self.response = response
|
|
11
|
+
|
|
12
|
+
def _map_control_fields(self, field: dict) -> dict:
|
|
13
|
+
# for tags < 010, no subfields, instead one str value in "value"
|
|
14
|
+
return {field["tag"]: field["value"]}
|
|
15
|
+
|
|
16
|
+
def _map_data_fields(self, field: dict) -> dict:
|
|
17
|
+
""" Maps marc fields > 010.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
field (dict): Contains the marc tag and list with indicators and subfields.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
dict: standardised marc-in-json format.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
data = field["data"]
|
|
27
|
+
|
|
28
|
+
# Order matters ind1, in2, subfields
|
|
29
|
+
field_data = {
|
|
30
|
+
"ind1": data.get("ind1", " "),
|
|
31
|
+
"ind2": data.get("ind2", " "),
|
|
32
|
+
"subfields": data.get("subfields", [])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {field["tag"]: field_data}
|
|
36
|
+
|
|
37
|
+
def _is_marc21structured(self, field: dict) -> bool:
|
|
38
|
+
"""Checks if the field is already structured according to MARC21 in JSON"""
|
|
39
|
+
return any(key.isdigit() for key in field.keys())
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _handle_field_type(self, field: dict) -> dict:
|
|
43
|
+
|
|
44
|
+
if self._is_marc21structured(field):
|
|
45
|
+
return field
|
|
46
|
+
|
|
47
|
+
if field.get("data"):
|
|
48
|
+
return self._map_data_fields(field)
|
|
14
49
|
|
|
15
|
-
def _map_field_data(self, field):
|
|
16
50
|
tag = field.get("tag")
|
|
51
|
+
|
|
17
52
|
if not tag:
|
|
18
|
-
raise SierraResponseConverterException("Field is missing
|
|
19
|
-
data = field.get("data", {})
|
|
20
|
-
return {tag: data}
|
|
53
|
+
raise SierraResponseConverterException("Field is missing MARC21 tag.")
|
|
21
54
|
|
|
22
|
-
|
|
23
|
-
|
|
55
|
+
if tag < "010":
|
|
56
|
+
return self._map_control_fields(field)
|
|
57
|
+
else:
|
|
58
|
+
return self._map_data_fields(field)
|
|
24
59
|
|
|
25
|
-
|
|
60
|
+
def _convert_response(self) -> list:
|
|
61
|
+
entries = self.response.get("entries")
|
|
26
62
|
if not entries:
|
|
27
63
|
raise SierraResponseConverterException("No entries found in the response.")
|
|
28
64
|
|
|
29
65
|
try:
|
|
30
|
-
|
|
66
|
+
return {"fields": [
|
|
67
|
+
{e["id"]: [
|
|
68
|
+
self._handle_field_type(f) for f in e["marc"]["fields"]
|
|
69
|
+
]}
|
|
70
|
+
for e in entries
|
|
71
|
+
]}
|
|
72
|
+
|
|
31
73
|
except KeyError as e:
|
|
32
|
-
raise SierraResponseConverterException(f"
|
|
74
|
+
raise SierraResponseConverterException(f"Malformed response: missing key {e}")
|
|
33
75
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def convert(self):
|
|
37
|
-
"""Runner method, converts the response to MARC-in-JSON format with error handling."""
|
|
76
|
+
|
|
77
|
+
def convert(self) -> list:
|
|
38
78
|
try:
|
|
39
79
|
return self._convert_response()
|
|
40
80
|
except Exception as e:
|
|
41
|
-
raise SierraResponseConverterException(f"An unexpected error occurred
|
|
81
|
+
raise SierraResponseConverterException(f"An unexpected error occurred: {e}")
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
rara_tools/converters.py,sha256=
|
|
1
|
+
rara_tools/converters.py,sha256=O769zNjde1VCfEUF2VU_49IAbm8NT-cG-VR0uPxixtE,2687
|
|
2
2
|
rara_tools/decorators.py,sha256=MjOyvZ5nTkwxwx2JLFEGpKKBysvecFw6EN6UDrSvZLU,2187
|
|
3
3
|
rara_tools/digar_schema_converter.py,sha256=k95U2iRlEA3sh772-v6snhHW6fju6qSTMnvWJ6DpzZk,14254
|
|
4
4
|
rara_tools/elastic.py,sha256=MgPHxZ3UbSTIL8_sT9gU5V4PLKJjo3aQ8CGyhXjRz6M,13065
|
|
@@ -9,8 +9,8 @@ rara_tools/utils.py,sha256=9vSbmuWYU5ydr4lXBKlUKa0xzDccFsaJv4T-XwgUfuY,2578
|
|
|
9
9
|
rara_tools/constants/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
10
|
rara_tools/constants/digitizer.py,sha256=gJ3jOMwuZfKcLqgOAxTyB266VYsskLabJiMUiSz3xX4,297
|
|
11
11
|
rara_tools/constants/general.py,sha256=E9Jaw-YxocS_tOZw9QBoxO3e9KK5EMbLoM0R7D4Iflw,171
|
|
12
|
-
rara_tools-0.0.
|
|
13
|
-
rara_tools-0.0.
|
|
14
|
-
rara_tools-0.0.
|
|
15
|
-
rara_tools-0.0.
|
|
16
|
-
rara_tools-0.0.
|
|
12
|
+
rara_tools-0.0.13.dist-info/LICENSE.md,sha256=hkZVnIZll7e_KNEQzeY94Y9tlzVL8iVZBTMBvDykksU,35142
|
|
13
|
+
rara_tools-0.0.13.dist-info/METADATA,sha256=0Aipkuodi_CzCTUMkVqKOI__n5mN2r8hEGJ49-MjpMo,3895
|
|
14
|
+
rara_tools-0.0.13.dist-info/WHEEL,sha256=nn6H5-ilmfVryoAQl3ZQ2l8SH5imPWFpm1A5FgEuFV4,91
|
|
15
|
+
rara_tools-0.0.13.dist-info/top_level.txt,sha256=JwfB5b8BAtW5OFKRln2AQ_WElTRyIBM4nO0FKN1cupY,11
|
|
16
|
+
rara_tools-0.0.13.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|