blob: 656b084e8bdc104b096b064062e882d0682a8338 (
plain)
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
|
import csv
import io
from enum import Enum
from typing import List, Dict
import json
class Utilities:
def process_json_input(json_input: str):
data = json.loads(json_input)
if not data.get("file_path") or not data.get("pii_fields"):
raise ValueError(
"Missing required file_path & pii_fields entries in JSON input"
)
return data["file_path"], data["pii_fields"]
@staticmethod
def get_s3_path(uri):
parts = uri.replace("s3://", "").split("/")
bucket = parts.pop(0)
key = "/".join(parts)
return bucket, key
@staticmethod
def create_byte_stream(data: List[Dict[str, str]]) -> bytes:
if not data:
return b""
output = io.StringIO()
headers = list(data[0].keys())
writer = csv.DictWriter(output, fieldnames=headers)
writer.writeheader()
writer.writerows(data)
csv_string = output.getvalue()
return csv_string.encode("utf-8")
|