def escape_filter_chars(assertion_value, escape_mode=0):
    """
    Replace all special characters found in assertion_value
    by quoted notation.

    escape_mode
        If 0 only special chars mentioned in RFC 4515 are escaped.
        If 1 all NON-ASCII chars are escaped.
        If 2 all chars are escaped.
    """

    if isinstance(assertion_value, six.text_type):
        assertion_value = assertion_value.encode("utf_8")

    s = []
    for c in assertion_value:
        do_escape = False

        if str != bytes:  # Python 3
            pass
        else:  # Python 2
            c = ord(c)

        if escape_mode == 0:
            if c == ord('\\') or c == ord('*') \
                    or c == ord('(') or c == ord(')') \
                    or c == ord('\x00'):
                do_escape = True
        elif escape_mode == 1:
            if c < '0' or c > 'z' or c in "\\*()":
                do_escape = True
        elif escape_mode == 2:
            do_escape = True
        else:
            raise ValueError('escape_mode must be 0, 1 or 2.')

        if do_escape:
            s.append(b"\\%02x" % c)
        else:
            b = None
            if str != bytes:  # Python 3
                b = bytes([c])
            else:  # Python 2
                b = chr(c)
            s.append(b)

    return b''.join(s)
def escape_filter_chars ( assertion_value , escape_mode = 0 ) : if isinstance ( assertion_value , six . text_type ) : assertion_value = assertion_value . encode ( "utf_8" ) s = [ ] for c in assertion_value : do_escape = False if str != bytes : pass else : c = ord ( c ) if escape_mode == 0 : if c == ord ( '\\' ) or c == ord ( '*' ) or c == ord ( '(' ) or c == ord ( ')' ) or c == ord ( '\x00' ) : do_escape = True elif escape_mode == 1 : if c < '0' or c > 'z' or c in "\\*()" : do_escape = True elif escape_mode == 2 : do_escape = True else : raise ValueError ( 'escape_mode must be 0, 1 or 2.' ) if do_escape : s . append ( b"\\%02x" % c ) else : b = None if str != bytes : b = bytes ( [ c ] ) else : b = chr ( c ) s . append ( b ) return b'' . join ( s )
Replace all special characters found in assertion_value
    by quoted notation.

    escape_mode
        If 0 only special chars mentioned in RFC 4515 are escaped.
        If 1 all NON-ASCII chars are escaped.
        If 2 all chars are escaped.