7fdd2411bb
git-subtree-dir: utils_v2 git-subtree-split: 62600ef57051e69587da18015b7b6840fdac3194
136 lines
5.2 KiB
Python
136 lines
5.2 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Thursday, 25th Jul., 2024
|
|
|
|
OBJECTIVE:
|
|
|
|
To provide a set of data cleaning functions for inputs like phone numbers, emails, etc.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# My utils:
|
|
from utils_v2.string import regex
|
|
|
|
# For random strings and tokens:
|
|
import string
|
|
import random
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
def file_name(input_string: str):
|
|
|
|
"""
|
|
Cleans up the string to allow it to safely become a filename.
|
|
:param input_string: The string that you want to make safe for using as a filename.
|
|
:return: The string that can safely be used as a filename.
|
|
"""
|
|
|
|
return regex.replace(
|
|
text = input_string.replace("\n", " "),
|
|
pattern = r"[^a-zA-Z0-9 \-_\.]",
|
|
substitute_text = ""
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
def for_mongo(input_document):
|
|
|
|
"""
|
|
Sanitizes and disarms any JSON-like input that could be used for NoSQL-injection attacks.
|
|
:param input_document: The list or dict to be sanitized.
|
|
:return: The sanitized list or dict.
|
|
"""
|
|
|
|
# A special function that disarms any input string by dealing with special characters
|
|
# that Mongo may consider to be instructions:
|
|
def disarm(input_string):
|
|
input_string = regex.replace(
|
|
text = input_string,
|
|
pattern = r"[^a-zA-Z0-9,_\-\.\\\/:;'\(\) ]",
|
|
substitute_text = ""
|
|
)
|
|
return input_string
|
|
|
|
# Initially we assign the value of the input to the output:
|
|
sanitized_document = input_document
|
|
|
|
# Handle the case where the input is an array:
|
|
if isinstance(input_document, list):
|
|
sanitized_document = [for_mongo(document) for document in input_document]
|
|
|
|
# Handle the case when the input is a document:
|
|
elif isinstance(input_document, dict):
|
|
sanitized_document = {}
|
|
for k, v in input_document.items():
|
|
sanitized_document[disarm(k)] = v if type(v) not in [list, dict] else for_mongo(v)
|
|
|
|
# Done here:
|
|
return sanitized_document
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|