U
    ,c                     @   sv   d Z ddlmZ ddlmZ ddlmZ ddlZddlZdddZddd	Zdd
dZ	dddZ
dddZdd ZdS )zCA module for dealing with unknown string and environment encodings.    )absolute_import)division)unicode_literalsNc                 C   s:   | dkrdS t js| S t| t jr&| S |p.t }| |S )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encoding r   9/tmp/pip-unpacked-wheel-1xt1w7un/fire/console/encoding.pyEncode   s    

r   c              	   C   s.  | dkrdS t | tjs$t | tjr*| }n0zt| }W n  ttfk
rX   t| }Y nX t |tjrj|S z|dW S  tk
r   Y nX |rz||W S  tk
r   Y nX z|dW S  tk
r   Y nX z|t	 W S  tk
 r   Y nX z|t
 W S  tk
r"   Y nX |dS )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asciiutf8z
iso-8859-1)r   r   	text_typer   	TypeErrorUnicodeErrorstrdecodesysgetfilesystemencodinggetdefaultencoding)datar   r   r   r   r   Decode4   s@    r   c                 C   s&   t |}| |}|dkr|S t|S )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.
  N)r   getr   )envnamedefaultvaluer   r   r   GetEncodedValue   s
    
r!   c                 C   s8   t ||d}|dkr$| |d dS t ||d| |< dS )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   r   r   r   SetEncodedValue   s
    r$   c                    s"    pt    fddt| D S )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.
  c                    s&   i | ]\}}t | d t | d qS )r"   )r   ).0kvr"   r   r   
<dictcomp>   s   
 zEncodeEnv.<locals>.<dictcomp>)r	   r   	iteritems)r   r   r   r"   r   	EncodeEnv   s    


r*   c                   C   s   t  pt  S )z!Gets the default encoding to use.)r   r   r   r   r   r   r   r	      s    r	   )N)N)N)N)N)__doc__
__future__r   r   r   r   r   r   r   r!   r$   r*   r	   r   r   r   r   <module>   s   

[


