Compare commits

...

2 Commits

Author SHA1 Message Date
Clemens Schwaighofer
0cc4883fa1 Move the mask from string to dict helpers, add end comment 2025-07-03 14:01:40 +09:00
Clemens Schwaighofer
1eb464dd2c Add string handling helper maks to mask strings 2025-07-03 13:57:39 +09:00
11 changed files with 52 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
# MARK: Project info # MARK: Project info
[project] [project]
name = "corelibs" name = "corelibs"
version = "0.4.0" version = "0.5.0"
description = "Collection of utils for Python scripts" description = "Collection of utils for Python scripts"
readme = "ReadMe.md" readme = "ReadMe.md"
requires-python = ">=3.13" requires-python = ">=3.13"

View File

@@ -89,3 +89,5 @@ class CsvWriter:
csv_row[value] = line[key] csv_row[value] = line[key]
self.csv_file_writer.writerow(csv_row) self.csv_file_writer.writerow(csv_row)
return True return True
# __END__

View File

@@ -110,5 +110,4 @@ class Timer:
""" """
return self._run_time return self._run_time
# __END__ # __END__

View File

@@ -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)) return str(Path(file_path.parent.name).joinpath(file_path.name))
else: else:
return file_path.name return file_path.name
# __END__

View File

@@ -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: if raise_on_many is True and len(keys) > 1:
raise ValueError("More than one element found with the same name") raise ValueError("More than one element found with the same name")
return keys[0] return keys[0]
# __END__

View File

@@ -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__

View File

@@ -35,3 +35,5 @@ def dict_hash_crc(data: dict[Any, Any] | list[Any]) -> str:
return hashlib.sha256( return hashlib.sha256(
json.dumps(data, sort_keys=True, ensure_ascii=True).encode('utf-8') json.dumps(data, sort_keys=True, ensure_ascii=True).encode('utf-8')
).hexdigest() ).hexdigest()
# __END__

View File

@@ -59,3 +59,5 @@ def build_dict(
dict[str, Any | list[Any] | dict[Any, Any]], dict[str, Any | list[Any] | dict[Any, Any]],
delete_keys_from_set(any_dict, ignore_entries) delete_keys_from_set(any_dict, ignore_entries)
) )
# __END__

View File

@@ -93,3 +93,5 @@ def unlock_run(lock_file: Path) -> None:
lock_file.unlink() lock_file.unlink()
except IOError as e: except IOError as e:
raise IOError(f"Cannot remove lock_file: {lock_file}: {e}") from e raise IOError(f"Cannot remove lock_file: {lock_file}: {e}") from e
# __END__

View File

@@ -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)) return time.strftime(timestamp_format, time.localtime(timestamp))
# __END__ # __END__

View File

@@ -36,3 +36,5 @@ def sha1_short(string: str) -> str:
str -- _description_ str -- _description_
""" """
return hashlib.sha1(string.encode('utf-8')).hexdigest()[:9] return hashlib.sha1(string.encode('utf-8')).hexdigest()[:9]
# __END__