
    x                     F    d Z ddlZddlZddlZd Zd Zd	dZd Zd Zd Z	y)
z-Helper functions for commonly used utilities.    Nc                 :    | j                  d      }|dk7  r| |d S y)az  Identify and extract PEM keys.

    Determines whether the given key is in the format of PEM key, and extracts
    the relevant part of the key if it is.

    Args:
        raw_key_input: The contents of a private key file (either PEM or
                       PKCS12).

    Returns:
        string, The actual key if the contents are from a PEM file, or
        else None.
    s   -----BEGIN N)find)raw_key_inputoffsets     (lib/third_party/oauth2client/_helpers.py_parse_pem_keyr	      s-     /F|VW%%     c                 0    t        j                  | d      S )N),:)
separators)jsondumps)datas    r   _json_encoder   )   s    ::dz22r
   c                     t        | t        j                        r| j                  |      n| }t        |t        j                        r|S t        dj                  |             )a4  Converts a string value to bytes, if necessary.

    Unfortunately, ``six.b`` is insufficient for this task since in
    Python2 it does not modify ``unicode`` objects.

    Args:
        value: The string/bytes value to be converted.
        encoding: The encoding to use to convert unicode to bytes. Defaults
                  to "ascii", which will not allow any characters from ordinals
                  larger than 127. Other useful values are "latin-1", which
                  which will only allows byte ordinals (up to 255) and "utf-8",
                  which will encode any unicode that needs to be.

    Returns:
        The original value converted to bytes (if unicode) or as passed in
        if it started out as bytes.

    Raises:
        ValueError if the value could not be converted to bytes.
    z%{0!r} could not be converted to bytes)
isinstancesix	text_typeencodebinary_type
ValueErrorformat)valueencodingresults      r   	_to_bytesr   -   sR    , E3==1 ll8$7< &#//*@GGNOOr
   c                     t        | t        j                        r| j                  d      n| }t        |t        j                        r|S t        dj                  |             )aE  Converts bytes to a string value, if necessary.

    Args:
        value: The string/bytes value to be converted.

    Returns:
        The original value converted to unicode (if bytes) or as passed in
        if it started out as unicode.

    Raises:
        ValueError if the value could not be converted to unicode.
    utf-8z'{0!r} could not be converted to unicode)r   r   r   decoder   r   r   )r   r   s     r   _from_bytesr"   J   sX     E3??3 ll7#9> &#--(5<<UCE 	Er
   c                 d    t        | d      } t        j                  |       j                  d      S )Nr    )r      =)r   base64urlsafe_b64encoderstrip)	raw_bytess    r   _urlsafe_b64encoder)   `   s*    )g6I##I.55d;;r
   c                 p    t        |       } | ddt        |       dz  z
  z  z   }t        j                  |      S )Nr$      )r   lenr%   urlsafe_b64decode)	b64stringpaddeds     r   _urlsafe_b64decoder0   e   s:    )$IS^a%7!788F##F++r
   )ascii)
__doc__r%   r   r   r	   r   r   r"   r)   r0    r
   r   <module>r4      s3    4   
&&3P:E,<
,r
   