def anonymize(cls, user, ldap_attrs, **kwargs): # type: (User, Dict[AnyStr, Any], **Any) -> Dict[AnyStr, AnyStr] """ Change values of function arguments to anonymize/pseudonymize user if UCRV asm/attributes//anonymize is true. Will return unchanged function arguments otherwise. :param User user: user object :param dict ldap_attrs: dictionary with the users LDAP attributes :return: dictionary with [modified] function arguments :rtype: dict :raises NotImplementedError: if cls.ucr_anonymize_key_base is unset """ ucr = get_ucr() if ucr.is_true(cls.ucr_anonymize_key_base): for k, v in cls.anonymize_mapping().items(): if v and v.startswith('%'): attr = v[1:].strip() try: v = ldap_attrs[attr][0] except KeyError: raise ValueError('Attribute {!r} not found in LDAP object of {}.'.format(attr, user)) except IndexError: raise ValueError('Attribute {!r} empty in LDAP object of {}.'.format(attr, user)) kwargs[k] = v return kwargs