generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
85 lines (65 loc) · 2.3 KB
/
cleanup.py
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
from typing import Any
from copy import deepcopy
from aws_lambda_powertools import Logger
logger = Logger()
UNSUPPORTED_SOBJS = [
"Scorecard",
"ScorecardAssociation",
"ScorecardMetric",
"UserExternalCredential",
"WebCartDocument",
]
def cleanup_records(records: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Cleans up records by removing unneeded fields and replacing newlines
Args:
records (list[dict[str, Any]]): List of records
Returns:
list[dict[str, Any]]: List of records with unneeded fields removed and newlines replaced
"""
records = remove_unneeded_fields(records, ["attributes"])
records = replace_newlines_in_dict_values(records)
return records
def replace_newlines_in_dict_values(
dicts: list[dict[str, Any]],
) -> list[dict[str, Any]]:
"""Replaces newlines in the values of a dictionary
Args:
dicts (list[dict[str, Any]]): List of dictionaries
Returns:
list[dict[str, Any]]: List of dictionaries with newlines replaced
"""
new_dicts = deepcopy(dicts)
for record in new_dicts:
for key, value in record.items():
if isinstance(value, str):
record[key] = value.encode("unicode_escape").decode("utf-8")
logger.debug(f"Replaced newlines in dict values")
return new_dicts
def remove_unneeded_fields(
records: list[dict[str, Any]], fields: list[str]
) -> list[dict[str, Any]]:
"""Removes unneeded fields from a list of records
Args:
records (list[dict[str,Any]]): List of records
fields (list[str]): List of fields to remove
Returns:
list[dict[str,Any]]: List of records with the specified fields removed
"""
new_records = deepcopy(records)
for record in new_records:
for field in fields:
del record[field]
return new_records
def filter_out_sobjects(sobjs: list[str]) -> list[str]:
"""Filters out unsupported sobjects
Args:
sobjs (list[str]): List of sobjects
Returns:
list[str]: List of sobjects with unsupported sobjects removed
"""
new_sobjs = deepcopy(sobjs)
for u_sobj in UNSUPPORTED_SOBJS:
if u_sobj in new_sobjs:
new_sobjs.remove(u_sobj)
logger.debug(f"Filtered out unsupported sobjects: {new_sobjs}")
return new_sobjs