From 0cc4883fa174e9ba1e3eb22c272c513fe27fecbb Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 3 Jul 2025 14:01:40 +0900 Subject: [PATCH] Move the mask from string to dict helpers, add end comment --- src/CoreLibs/csv_handling/csv_writer.py | 2 + src/CoreLibs/debug_handling/timer.py | 1 - src/CoreLibs/file_handling/file_crc.py | 2 + .../list_dict_handling/data_search.py | 2 + .../list_dict_handling/dict_helpers.py | 37 +++++++++++++++++++ .../list_dict_handling/fingerprint.py | 2 + .../list_dict_handling/manage_dict.py | 2 + .../script_handling/script_helpers.py | 2 + .../string_handling/datetime_helpers.py | 1 - src/CoreLibs/string_handling/hash_helpers.py | 2 + .../string_handling/string_helpers.py | 32 ---------------- 11 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 src/CoreLibs/list_dict_handling/dict_helpers.py diff --git a/src/CoreLibs/csv_handling/csv_writer.py b/src/CoreLibs/csv_handling/csv_writer.py index ff4a529..3ca9bdd 100644 --- a/src/CoreLibs/csv_handling/csv_writer.py +++ b/src/CoreLibs/csv_handling/csv_writer.py @@ -89,3 +89,5 @@ class CsvWriter: csv_row[value] = line[key] self.csv_file_writer.writerow(csv_row) return True + +# __END__ diff --git a/src/CoreLibs/debug_handling/timer.py b/src/CoreLibs/debug_handling/timer.py index a2c5d26..a5dcc3c 100644 --- a/src/CoreLibs/debug_handling/timer.py +++ b/src/CoreLibs/debug_handling/timer.py @@ -110,5 +110,4 @@ class Timer: """ return self._run_time - # __END__ diff --git a/src/CoreLibs/file_handling/file_crc.py b/src/CoreLibs/file_handling/file_crc.py index 3bc2fa6..9dba444 100644 --- a/src/CoreLibs/file_handling/file_crc.py +++ b/src/CoreLibs/file_handling/file_crc.py @@ -42,3 +42,5 @@ def file_name_crc(file_path: Path, add_parent_folder: bool = False) -> str: return str(Path(file_path.parent.name).joinpath(file_path.name)) else: return file_path.name + +# __END__ diff --git a/src/CoreLibs/list_dict_handling/data_search.py b/src/CoreLibs/list_dict_handling/data_search.py index d3b96b7..f08667e 100644 --- a/src/CoreLibs/list_dict_handling/data_search.py +++ b/src/CoreLibs/list_dict_handling/data_search.py @@ -126,3 +126,5 @@ def value_lookup(haystack: dict[str, str], value: str, raise_on_many: bool = Fal if raise_on_many is True and len(keys) > 1: raise ValueError("More than one element found with the same name") return keys[0] + +# __END__ diff --git a/src/CoreLibs/list_dict_handling/dict_helpers.py b/src/CoreLibs/list_dict_handling/dict_helpers.py new file mode 100644 index 0000000..89529f1 --- /dev/null +++ b/src/CoreLibs/list_dict_handling/dict_helpers.py @@ -0,0 +1,37 @@ +""" +Dict helpers +""" + + +def mask( + data_set: dict[str, str], + mask_keys: list[str] | None = None, + mask_str: str = "***", + skip: bool = False +) -> dict[str, str]: + """ + mask data for output + Checks if mask_keys list exist in any key in the data set either from the start or at the end + + Arguments: + data_set {dict[str, str]} -- _description_ + + Keyword Arguments: + mask_keys {list[str] | None} -- _description_ (default: {None}) + mask_str {str} -- _description_ (default: {"***"}) + skip {bool} -- _description_ (default: {False}) + + Returns: + dict[str, str] -- _description_ + """ + if skip is True: + return data_set + if mask_keys is None: + mask_keys = ["password", "secret"] + return { + key: mask_str + if any(key.startswith(mask_key) or key.endswith(mask_key) for mask_key in mask_keys) else value + for key, value in data_set.items() + } + +# __END__ diff --git a/src/CoreLibs/list_dict_handling/fingerprint.py b/src/CoreLibs/list_dict_handling/fingerprint.py index 3bcb092..63f2dbc 100644 --- a/src/CoreLibs/list_dict_handling/fingerprint.py +++ b/src/CoreLibs/list_dict_handling/fingerprint.py @@ -35,3 +35,5 @@ def dict_hash_crc(data: dict[Any, Any] | list[Any]) -> str: return hashlib.sha256( json.dumps(data, sort_keys=True, ensure_ascii=True).encode('utf-8') ).hexdigest() + +# __END__ diff --git a/src/CoreLibs/list_dict_handling/manage_dict.py b/src/CoreLibs/list_dict_handling/manage_dict.py index 31b4056..9887b39 100644 --- a/src/CoreLibs/list_dict_handling/manage_dict.py +++ b/src/CoreLibs/list_dict_handling/manage_dict.py @@ -59,3 +59,5 @@ def build_dict( dict[str, Any | list[Any] | dict[Any, Any]], delete_keys_from_set(any_dict, ignore_entries) ) + +# __END__ diff --git a/src/CoreLibs/script_handling/script_helpers.py b/src/CoreLibs/script_handling/script_helpers.py index 6ef760c..4c37022 100644 --- a/src/CoreLibs/script_handling/script_helpers.py +++ b/src/CoreLibs/script_handling/script_helpers.py @@ -93,3 +93,5 @@ def unlock_run(lock_file: Path) -> None: lock_file.unlink() except IOError as e: raise IOError(f"Cannot remove lock_file: {lock_file}: {e}") from e + +# __END__ diff --git a/src/CoreLibs/string_handling/datetime_helpers.py b/src/CoreLibs/string_handling/datetime_helpers.py index d4e699c..8119c32 100644 --- a/src/CoreLibs/string_handling/datetime_helpers.py +++ b/src/CoreLibs/string_handling/datetime_helpers.py @@ -60,5 +60,4 @@ def create_time(timestamp: float, timestamp_format: str = "%Y-%m-%d %H:%M:%S") - """ return time.strftime(timestamp_format, time.localtime(timestamp)) - # __END__ diff --git a/src/CoreLibs/string_handling/hash_helpers.py b/src/CoreLibs/string_handling/hash_helpers.py index 24fb55f..7f6be38 100644 --- a/src/CoreLibs/string_handling/hash_helpers.py +++ b/src/CoreLibs/string_handling/hash_helpers.py @@ -36,3 +36,5 @@ def sha1_short(string: str) -> str: str -- _description_ """ return hashlib.sha1(string.encode('utf-8')).hexdigest()[:9] + +# __END__ diff --git a/src/CoreLibs/string_handling/string_helpers.py b/src/CoreLibs/string_handling/string_helpers.py index 867378f..b7c9eae 100644 --- a/src/CoreLibs/string_handling/string_helpers.py +++ b/src/CoreLibs/string_handling/string_helpers.py @@ -83,36 +83,4 @@ def format_number(number: float, precision: int = 0) -> str: "f}" ).format(number) - -def mask( - data_set: dict[str, str], - mask_keys: list[str] | None = None, - mask_str: str = "***", - skip: bool = False -) -> dict[str, str]: - """ - mask data for output - Checks if mask_keys list exist in any key in the data set either from the start or at the end - - Arguments: - data_set {dict[str, str]} -- _description_ - - Keyword Arguments: - mask_keys {list[str] | None} -- _description_ (default: {None}) - mask_str {str} -- _description_ (default: {"***"}) - skip {bool} -- _description_ (default: {False}) - - Returns: - dict[str, str] -- _description_ - """ - if skip is True: - return data_set - if mask_keys is None: - mask_keys = ["password", "secret"] - return { - key: mask_str - if any(key.startswith(mask_key) or key.endswith(mask_key) for mask_key in mask_keys) else value - for key, value in data_set.items() - } - # __END__