-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
31 lines (23 loc) · 823 Bytes
/
helpers.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
from collections.abc import Mapping
from typing import Any
from .const import KEYS_TO_REDACT
REDACTED = "**REDACTED**"
def redact_dict(data):
"""Redact sensitive data in a dict."""
if not isinstance(data, (Mapping, list)):
return data
if isinstance(data, list):
return [redact_dict(val) for val in data]
redacted: Mapping[str, Any] = {**data}
for key, value in redacted.items():
if value is None:
continue
if isinstance(value, str) and not value:
continue
if key in KEYS_TO_REDACT:
redacted[key] = REDACTED
elif isinstance(value, Mapping):
redacted[key] = redact_dict(value)
elif isinstance(value, list):
redacted[key] = [redact_dict(item) for item in value]
return redacted