
                         t    S r SSKJr  SSKJr  SSKJr  SSKrSSKrSS jrSS jrSS jr	SS	 jr
SS
 jrS rg)zCA module for dealing with unknown string and environment encodings.    )absolute_import)division)unicode_literalsNc                     U c  g[         R                  (       d  U $ [        U [         R                  5      (       a  U $ U=(       d
    [	        5       nU R                  U5      $ )zEncode the text string to a byte string.

Args:
  string: str, The text string to encode.
  encoding: The suggested encoding if known.

Returns:
  str, The binary string.
N)sixPY2
isinstancebinary_type_GetEncodingencode)stringencodings     (lib/googlecloudsdk/core/util/encoding.pyEncoder      sK     ^	 M((M'(	x	      c                    U c  g[        U [        R                  5      (       d  [        U [        R                  5      (       a  U nO [        R                  " U 5      n[        U[        R                  5      (       a  U$  UR                  S5      $ ! [        [
        4 a    [        U 5      n NPf = f! [
         a     Of = fU(       a"   UR                  U5      $ ! [
         a     Of = f UR                  S5      $ ! [
         a     Of = f UR                  [        R                  " 5       5      $ ! [
         a     Of = f UR                  [        R                  " 5       5      $ ! [
         a     Of = fUR                  S5      $ )a  Returns string with non-ascii characters decoded to UNICODE.

UTF-8, the suggested encoding, and the usual suspects will be attempted in
order.

Args:
  data: A string or object that has str() and unicode() methods that may
    contain an encoding incompatible with the standard output encoding.
  encoding: The suggested encoding if known.

Returns:
  A text string representing the decoded byte string.
Nasciizutf-8z
iso-8859-1)r	   r   	text_typer
   	TypeErrorUnicodeErrorstrdecodesysgetfilesystemencodinggetdefaultencoding)datar   r   s      r   Decoder   4   sg    
\
 cmm$$
4(I(IF}}T"f &&M	==!! |$  4yf 
 		
 ]]8$$ 
	==!!	 		
	==22455	 		
	==//122	 		& 
|	$$sl   B ?B1 B.-B.1
B>=B>	C 
C'&C'+C< <
D	D	#D1 1
D>=D>#E& &
E32E3c                 Z    [        U5      nU R                  U5      nUc  U$ [        U5      $ )zReturns the decoded value of the env var name.

Args:
  env: {str: str}, The env dict.
  name: str, The env var name.
  default: The value to return if name is not in env.

Returns:
  The decoded value of the env var name.
)r   getr   )envnamedefaultvalues       r   GetEncodedValuer$      s0     
$
''$-%
]N 
r   c                 X    [        XS9nUc  U R                  US5        g[        X#S9X'   g)a  Sets the value of name in env to an encoded value.

Args:
  env: {str: str}, The env dict.
  name: str, The env var name.
  value: str or unicode, The value for name. If None then name is removed from
    env.
  encoding: str, The encoding to use or None to try to infer it.
r   N)r   pop)r    r!   r#   r   s       r   SetEncodedValuer(      s0    & 
	($
]GGD$
U.#)r   c                     U=(       d
    [        5       n0 n[        R                  " U 5       H  u  p4[        X#XAS9  M     U$ )a  Encodes all the key value pairs in env in preparation for subprocess.

Args:
  env: {str: str}, The environment you are going to pass to subprocess.
  encoding: str, The encoding to use or None to use the default.

Returns:
  {bytes: bytes}, The environment to pass to subprocess.
r&   )r   r   	iteritemsr(   )r    r   encoded_envkvs        r   	EncodeEnvr.      s:     '(+mmC daKA9 !	r   c                  d    [         R                  " 5       =(       d    [         R                  " 5       $ )z!Gets the default encoding to use.)r   r   r    r   r   r   r      s    		"	"	$	@(>(>(@@r   )N)__doc__
__future__r   r   r   r   r   r   r   r$   r(   r.   r   r0   r   r   <module>r3      s;   " J &  ' 
 
!0X%v(/4$Ar   