-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathredact_live_pointers.py
More file actions
99 lines (74 loc) · 3.36 KB
/
redact_live_pointers.py
File metadata and controls
99 lines (74 loc) · 3.36 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import json
import os
from datetime import datetime, timedelta, timezone
from typing import Any
import boto3
import fire
from nrlf.consumer.fhir.r4.model import DocumentReference
from nrlf.core.constants import PointerTypes
from nrlf.core.logger import logger
from nrlf.core.utils import create_fhir_instant
from nrlf.core.validators import DocumentReferenceValidator
dynamodb = boto3.client("dynamodb")
paginator = dynamodb.get_paginator("scan")
logger.setLevel("ERROR")
type_to_name = {pointer_type.value: pointer_type.name for pointer_type in PointerTypes}
def _redact_timestamps(docref: DocumentReference) -> None:
mock_timestamp = create_fhir_instant()
docref.meta.lastUpdated = mock_timestamp
docref.date = mock_timestamp
def _redact_ids(docref: DocumentReference) -> None:
ods_code = docref.custodian.identifier.value
mock_id = "c2a99222-eb50-4451-ad6e-1e951627800e"
docref.subject.identifier.value = "9999999999"
docref.id = f"{ods_code}-{mock_id}"
if docref.masterIdentifier:
docref.masterIdentifier.value = f"mid_{mock_id}"
if docref.relatesTo:
for relates_to in docref.relatesTo:
relates_to.target.identifier.value = f"rel_{mock_id}"
def _redact_content(docref: DocumentReference) -> None:
mock_timestamp = create_fhir_instant()
for content in docref.content:
if content.attachment.url.startswith("ssp://"):
content.attachment.url = "ssp://content.test.local/content"
else:
content.attachment.url = "https://content.test.local/content"
content.attachment.creation = mock_timestamp
def _redact_context(docref: DocumentReference) -> None:
if docref.context.related:
for related in docref.context.related:
related.identifier.value = "012345678910"
mock_timestamp = create_fhir_instant()
if docref.context.period:
if docref.context.period.start:
docref.context.period.start = mock_timestamp
if docref.context.period.end:
docref.context.period.end = mock_timestamp
def _redact_pointers(src_path: str, dest_path: str) -> None:
"""
Redact pointers in .json files in from the source path and write the redacted pointer to the destination path.
Parameters:
- src_path: The path to the source directory containing the pointers.
- dest_path: The path to the destination directory to write the redacted pointers.
"""
src_pointer_files = [f for f in os.listdir(src_path) if f.endswith(".json")]
for src_pointer_file in src_pointer_files:
print("Reading", src_pointer_file)
with open(f"{src_path}/{src_pointer_file}", "r") as f:
pointer_data = f.read()
docref = DocumentReference.model_validate_json(pointer_data)
ods_code = docref.custodian.identifier.value
type_coding = docref.type.coding[0]
pointer_type = type_to_name[f"{type_coding.system}|{type_coding.code}"]
_redact_ids(docref)
_redact_timestamps(docref)
_redact_content(docref)
_redact_context(docref)
month_year = datetime.now().strftime("%b%y")
filename = f"{dest_path}/{ods_code}_{pointer_type}_{month_year}.json"
print("Writing", filename)
with open(filename, "w") as f:
f.write(docref.model_dump_json(indent=2, exclude_unset=True))
if __name__ == "__main__":
fire.Fire(_redact_pointers)