-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconverter.py
More file actions
64 lines (50 loc) · 2.21 KB
/
converter.py
File metadata and controls
64 lines (50 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Main validation engine
from typing import Any
import exception_messages
from conversion_layout import ConversionField, ConversionLayout
from extractor import Extractor
from mappings import ActionFlag
ConversionErrorRecord = dict[str, Any]
ConvertedRecord = dict[str, Any]
class Converter:
def __init__(self, fhir_data: str | dict[str, Any], action_flag: str = ActionFlag.UPDATE) -> None:
self.converted: ConvertedRecord = {}
self.error_records: list[ConversionErrorRecord] = []
self.action_flag = action_flag
if not fhir_data:
raise ValueError("FHIR data is required for initialization.")
self.extractor = Extractor(fhir_data)
self.conversion_layout = ConversionLayout(self.extractor)
def run_conversion(self) -> ConvertedRecord:
for conversion in self.conversion_layout.get_conversion_layout():
self._convert_data(conversion)
self.error_records.extend(self.extractor.get_error_records())
# Add CONVERSION_ERRORS as the 35th field
self.converted["CONVERSION_ERRORS"] = self.error_records
return self.converted
def _convert_data(self, conversion: ConversionField) -> None:
flat_field = conversion.field_name_flat
try:
if flat_field == "ACTION_FLAG":
self.converted[flat_field] = self.action_flag
return
if (converted := conversion.expression_rule()) is not None:
self.converted[flat_field] = converted
except Exception as error:
self._log_error(
flat_field,
f"Conversion error [{error.__class__.__name__}]: {error}",
code=exception_messages.PARSING_ERROR,
)
self.converted[flat_field] = ""
def _log_error(self, field_name: str, e: Exception | str, code: str) -> None:
self.error_records.append(
{
"code": code,
"field": field_name,
"value": None,
"message": str(e),
}
)
def get_error_records(self) -> list[ConversionErrorRecord]:
return self.error_records