rara-tools 0.0.12__tar.gz → 0.0.13__tar.gz

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.

Files changed (32) hide show
  1. {rara_tools-0.0.12/rara_tools.egg-info → rara_tools-0.0.13}/PKG-INFO +1 -1
  2. rara_tools-0.0.13/VERSION +1 -0
  3. rara_tools-0.0.13/rara_tools/converters.py +81 -0
  4. {rara_tools-0.0.12 → rara_tools-0.0.13/rara_tools.egg-info}/PKG-INFO +1 -1
  5. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_converters.py +33 -11
  6. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_elastic_vector_and_search_operations.py +1 -1
  7. rara_tools-0.0.12/VERSION +0 -1
  8. rara_tools-0.0.12/rara_tools/converters.py +0 -41
  9. {rara_tools-0.0.12 → rara_tools-0.0.13}/LICENSE.md +0 -0
  10. {rara_tools-0.0.12 → rara_tools-0.0.13}/README.md +0 -0
  11. {rara_tools-0.0.12 → rara_tools-0.0.13}/pyproject.toml +0 -0
  12. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/constants/__init__.py +0 -0
  13. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/constants/digitizer.py +0 -0
  14. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/constants/general.py +0 -0
  15. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/decorators.py +0 -0
  16. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/digar_schema_converter.py +0 -0
  17. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/elastic.py +0 -0
  18. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/exceptions.py +0 -0
  19. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/s3.py +0 -0
  20. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/task_reporter.py +0 -0
  21. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools/utils.py +0 -0
  22. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools.egg-info/SOURCES.txt +0 -0
  23. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools.egg-info/dependency_links.txt +0 -0
  24. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools.egg-info/requires.txt +0 -0
  25. {rara_tools-0.0.12 → rara_tools-0.0.13}/rara_tools.egg-info/top_level.txt +0 -0
  26. {rara_tools-0.0.12 → rara_tools-0.0.13}/requirements.txt +0 -0
  27. {rara_tools-0.0.12 → rara_tools-0.0.13}/setup.cfg +0 -0
  28. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_digar_schema_converter.py +0 -0
  29. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_elastic.py +0 -0
  30. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_s3_exceptions.py +0 -0
  31. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_s3_file_operations.py +0 -0
  32. {rara_tools-0.0.12 → rara_tools-0.0.13}/tests/test_task_reporter.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rara-tools
3
- Version: 0.0.12
3
+ Version: 0.0.13
4
4
  Summary: Tools to support Kata's work.
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.10
@@ -0,0 +1 @@
1
+ 0.0.13
@@ -0,0 +1,81 @@
1
+ from .exceptions import SierraResponseConverterException
2
+
3
+
4
+ class SierraResponseConverter:
5
+ """Converts a JSON response from the Sierra API to MARC-in-JSON format."""
6
+
7
+ def __init__(self, response: dict):
8
+ if not isinstance(response, dict):
9
+ raise SierraResponseConverterException("Please provide a valid JSON response.")
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)
49
+
50
+ tag = field.get("tag")
51
+
52
+ if not tag:
53
+ raise SierraResponseConverterException("Field is missing MARC21 tag.")
54
+
55
+ if tag < "010":
56
+ return self._map_control_fields(field)
57
+ else:
58
+ return self._map_data_fields(field)
59
+
60
+ def _convert_response(self) -> list:
61
+ entries = self.response.get("entries")
62
+ if not entries:
63
+ raise SierraResponseConverterException("No entries found in the response.")
64
+
65
+ try:
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
+
73
+ except KeyError as e:
74
+ raise SierraResponseConverterException(f"Malformed response: missing key {e}")
75
+
76
+
77
+ def convert(self) -> list:
78
+ try:
79
+ return self._convert_response()
80
+ except Exception as e:
81
+ raise SierraResponseConverterException(f"An unexpected error occurred: {e}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rara-tools
3
- Version: 0.0.12
3
+ Version: 0.0.13
4
4
  Summary: Tools to support Kata's work.
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.10
@@ -5,12 +5,22 @@ import pytest
5
5
  from rara_tools.converters import SierraResponseConverter
6
6
  from rara_tools.exceptions import SierraResponseConverterException
7
7
 
8
+ import json
9
+
8
10
  root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
9
11
 
10
12
  SIERRA_TEST_DATA_DIR = os.path.join(root, "tests", "test_data", "sierra")
11
13
  INPUT_DIR = os.path.join(SIERRA_TEST_DATA_DIR, "input")
12
14
  OUTPUT_DIR = os.path.join(SIERRA_TEST_DATA_DIR, "output")
13
15
 
16
+ def compare_results(expected, converted):
17
+ return json.dumps(expected) == json.dumps(converted)
18
+
19
+ def read_json_file(file_path):
20
+ with open(file_path, "r") as f:
21
+ data = f.read()
22
+ return json.loads(data)
23
+
14
24
  example_res = {
15
25
  "total": 100,
16
26
  "start": 50000,
@@ -27,6 +37,8 @@ example_res = {
27
37
  {
28
38
  # "tag": "100",
29
39
  "data": {
40
+ "ind1": "1",
41
+ "ind2": " ",
30
42
  "subfields": [
31
43
  {
32
44
  "code": "a",
@@ -36,18 +48,13 @@ example_res = {
36
48
  "code": "d",
37
49
  "data": "1975-"
38
50
  }
39
- ],
40
- "ind1": "1",
41
- "ind2": " "
51
+ ]
42
52
  }
43
53
  },
44
54
  ]}}]}
45
55
 
46
56
 
47
- def read_json_file(file_path):
48
- with open(file_path, "r") as f:
49
- data = f.read()
50
- return json.loads(data)
57
+
51
58
 
52
59
  def test_convert_bibs_response():
53
60
  response = read_json_file(os.path.join(INPUT_DIR, "bibs.json"))
@@ -55,8 +62,9 @@ def test_convert_bibs_response():
55
62
  converter = SierraResponseConverter(response)
56
63
  data = converter.convert()
57
64
 
58
- expected = read_json_file(os.path.join(OUTPUT_DIR, "bibs.json"))
59
- assert data == expected
65
+ expected = read_json_file(os.path.join(OUTPUT_DIR, "bibs.json"))
66
+
67
+ assert compare_results(expected, data)
60
68
 
61
69
 
62
70
  def test_convert_keywords_response():
@@ -67,9 +75,10 @@ def test_convert_keywords_response():
67
75
  converter = SierraResponseConverter(response)
68
76
  data = converter.convert()
69
77
 
78
+
70
79
  expected = read_json_file(os.path.join(OUTPUT_DIR, "keywords.json"))
71
80
 
72
- assert data == expected
81
+ assert compare_results(expected, data)
73
82
 
74
83
 
75
84
  def test_convert_authorities_response():
@@ -82,7 +91,20 @@ def test_convert_authorities_response():
82
91
 
83
92
  expected = read_json_file(os.path.join(OUTPUT_DIR, "authorities.json"))
84
93
 
85
- assert data == expected
94
+ assert compare_results(expected, data)
95
+
96
+ def test_converter_handles_marc_in_json_response():
97
+ """ Gracefully handle entries already in MARC-in-JSON format """
98
+ with open(os.path.join(INPUT_DIR, "bibsmarc.json"), "r") as f:
99
+ response = f.read()
100
+ response = json.loads(response)
101
+
102
+ converter = SierraResponseConverter(response)
103
+ data = converter.convert()
104
+
105
+ expected = read_json_file(os.path.join(OUTPUT_DIR, "bibsmarc.json"))
106
+
107
+ assert compare_results(expected, data)
86
108
 
87
109
  def test_convert_with_wrong_format():
88
110
  with pytest.raises(SierraResponseConverterException):
@@ -15,7 +15,7 @@ TEST_DOCUMENTS = load_json("./tests/test_data/elastic_vectorized_docs.json")
15
15
  TEST_VECTOR_DATA = load_json("./tests/test_data/test_vector_data.json")
16
16
  TEST_VECTOR = TEST_VECTOR_DATA.get("vector")
17
17
 
18
- es_url = os.getenv("ELASTIC_TEST_URL", "http://rara-elastic.texta.ee:9200")#http://localhost:9200")
18
+ es_url = os.getenv("ELASTIC_TEST_URL", "http://localhost:9200")
19
19
  ELASTIC = KataElastic(es_url)
20
20
 
21
21
  TEST_KNN_INDEX_NAME = "tools_knn_testing_index"
rara_tools-0.0.12/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.12
@@ -1,41 +0,0 @@
1
- from .exceptions import SierraResponseConverterException
2
-
3
-
4
- class SierraResponseConverter:
5
- """ Takes a JSON response from the Sierra API (https://tester.ester.ee/iii/sierra-api/swagger/index.html)
6
- and converts it to MARC-in-JSON format.
7
-
8
- """
9
-
10
- def __init__(self, response: dict):
11
- if not isinstance(response, dict):
12
- raise SierraResponseConverterException("Please provide a valid JSON response.")
13
- self.response = response
14
-
15
- def _map_field_data(self, field):
16
- tag = field.get("tag")
17
- if not tag:
18
- raise SierraResponseConverterException("Field is missing a valid 'tag'.")
19
- data = field.get("data", {})
20
- return {tag: data}
21
-
22
- def _convert_response(self):
23
- response = self.response
24
-
25
- entries = response.get("entries")
26
- if not entries:
27
- raise SierraResponseConverterException("No entries found in the response.")
28
-
29
- try:
30
- fields = [self._map_field_data(f) for e in entries for f in e["marc"]["fields"]]
31
- except KeyError as e:
32
- raise SierraResponseConverterException(f"Missing expected MARC fields in the response: {e}")
33
-
34
- return {"fields": fields}
35
-
36
- def convert(self):
37
- """Runner method, converts the response to MARC-in-JSON format with error handling."""
38
- try:
39
- return self._convert_response()
40
- except Exception as e:
41
- raise SierraResponseConverterException(f"An unexpected error occurred during conversion: {e}")
File without changes
File without changes
File without changes
File without changes