From 29049ab7b25f96d4f59e4cb7b6773a52bad5576b Mon Sep 17 00:00:00 2001 From: GammaC0de Date: Sun, 27 Dec 2015 03:09:56 +0200 Subject: [utils] new function: safe_format() --- module/plugins/internal/utils.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'module/plugins/internal/utils.py') diff --git a/module/plugins/internal/utils.py b/module/plugins/internal/utils.py index 1fdbaf279..02077cffd 100644 --- a/module/plugins/internal/utils.py +++ b/module/plugins/internal/utils.py @@ -30,7 +30,7 @@ except ImportError: class utils(object): __name__ = "utils" __type__ = "plugin" - __version__ = "0.08" + __version__ = "0.09" __status__ = "stable" __pattern__ = r'^unmatchable$' @@ -71,6 +71,36 @@ def format_size(value): return "%.2f %s" % (size, sizes[steps]) +def safe_format(value, unsafe): + """ + Returns the content of value omitting sensitive information + + Args: + value: value to format + unsafe: string or list: sensitive word(s) to remove + """ + if isinstance(value, basestring): + if isinstance(unsafe, basestring): + return "'%s'" % ("**********" if value == unsafe else value) + + elif isinstance(unsafe, list): + return "'%s'" % ("**********" if value in unsafe else value) + + elif isinstance(value, dict): + return "{%s}" % ", ".join("'%s': %s" % (k, safe_format(v, unsafe)) for k, v in value.iteritems()) + + elif isinstance(value, list): + return "[%s]" % ", ".join("%s" % safe_format(v, unsafe) for v in value) + + elif isinstance(value, tuple): + return "(%s)" % ", ".join("%s" % safe_format(v, unsafe) for v in value) + + elif isinstance(value, set): + return "set([%s])" % ", ".join("%s" % safe_format(v, unsafe) for v in value) + + return repr(value) + + def compare_time(start, end): start = map(int, start) end = map(int, end) -- cgit v1.2.3