import six
import base64


def Base64WSEncode(s):
    """
    Return Base64 web safe encoding of s. Suppress padding characters (=).

    Uses URL-safe alphabet: - replaces +, _ replaces /. Will convert s of type
    unicode to string type first.

    @param s: string to encode as Base64
    @type s: string

    @return: Base64 representation of s.
    @rtype: string

    NOTE: Taken from keyczar (Apache 2.0 license)
    """
    if isinstance(s, six.text_type):
        # Make sure input string is always converted to bytes (if not already)
        s = s.encode("utf-8")

    return base64.urlsafe_b64encode(s).decode("utf-8").replace("=", "")