def sanitize_object(obj, **kwargs): filters = kwargs.get("filters", []) if isinstance(obj, dict): clean_dict = {} for k, v in six.iteritems(obj): # Remove values for keys matching filters if any(f in k for f in filters): clean_dict[k] = "[FILTERED]" else: clean_obj = sanitize_object(v, **kwargs) if clean_obj: clean_dict[k] = clean_obj return clean_dict elif any(isinstance(obj, t) for t in (list, set, tuple)): return [sanitize_object(x, **kwargs) for x in obj] else: try: if isinstance(obj, six.string_types): string = obj else: if six.PY2: string = unicode(str(obj), errors='replace') else: string = str(obj) except Exception: exc = traceback.format_exc() bugsnag.warn("Could not add object to metadata: %s" % exc) string = "[BADENCODING]" return string
def sanitize_object(obj, **kwargs): filters = kwargs.get("filters", []) if isinstance(obj, dict): clean_dict = {} for k, v in six.iteritems(obj): # Remove values for keys matching filters if any(f.lower() in k.lower() for f in filters): clean_dict[k] = "[FILTERED]" else: clean_obj = sanitize_object(v, **kwargs) if clean_obj is not None: clean_dict[k] = clean_obj return clean_dict elif any(isinstance(obj, t) for t in (list, set, tuple)): return [sanitize_object(x, **kwargs) for x in obj] elif any(isinstance(obj, t) for t in (bool, float, int)): return obj else: try: if isinstance(obj, six.string_types): string = obj else: if six.PY2: string = unicode(str(obj), errors='replace') else: string = str(obj) except Exception: exc = traceback.format_exc() bugsnag.warn("Could not add object to metadata: %s" % exc) string = "[BADENCODING]" return string
def shrink_object(obj): if isinstance(obj, six.string_types): return obj[:MAX_STRING_LENGTH] elif isinstance(obj, dict): for k, v in six.iteritems(obj): obj[k] = shrink_object(v) elif any(isinstance(obj, t) for t in (list, set, tuple)): return [shrink_object(x) for x in obj] return obj