B
    6dD                 @   s   d Z ddlZddlZddlZddlZddlZddlZdddZdd ZG dd	 d	e	Z
y
ejZW n ek
rx   d
ZY nX dd Zdd ZdS )zio-related utilities    Nstdoutc             C   sD   | dkst tt| } y
| j}W n tk
r4   | S X td|S )u   Get a wrapper to write unicode to stdout/stderr as UTF-8.

    This ignores environment variables and default encodings, to reliably write
    unicode to stdout or stderr.

    ::

        unicode_std_stream().write(u'ł@e¶ŧ←')
    )r   stderrzutf-8)AssertionErrorgetattrsysbufferAttributeErrorcodecs	getwriter)streamstream_b r   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/nbconvert/utils/io.pyunicode_std_stream   s    


r   c              C   s4   t j} y
| j}W n tk
r$   | S X td|S )zGet a wrapper to read unicode from stdin as UTF-8.

    This ignores environment variables and default encodings, to reliably read unicode from stdin.

    ::

        totreat = unicode_stdin_stream().read()
    zutf-8)r   stdinr   r   r	   	getreader)r   r   r   r   r   unicode_stdin_stream$   s    	
r   c               @   s   e Zd ZdZdd ZdS )FormatSafeDictzFormat a dictionary safely.c             C   s   d| d S )zHandle missing value.{}r   )selfkeyr   r   r   __missing__9   s    zFormatSafeDict.__missing__N)__name__
__module____qualname____doc__r   r   r   r   r   r   6   s   r   i  c          
   C   sP   t tdstS d}yt| | W n( tk
rJ } z
|j}W dd}~X Y nX |S )zHard links ``src`` to ``dst``, returning 0 or errno.

    Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
    supported by the operating system.
    linkr   N)hasattrosENOLINKr   OSErrorerrno)srcdst
link_errnoer   r   r   r   D   s    
r   c             C   s   t j|r"t j|t j| }t| |}|tjkrt | j	t |j	krRdS |dt
ddd }yt| | W n: tk
r   yt | W n tk
r   Y nX  Y nX t || n|dkrt| | dS )a+  Attempts to hardlink ``src`` to ``dst``, copying if the link fails.

    Attempts to maintain the semantics of ``shutil.copy``.

    Because ``os.link`` does not overwrite files, a unique temporary file
    will be used if the target already exists, then that file will be moved
    into place.
    Nz-temp-   i   Z04Xr   )r   pathisdirjoinbasenamer   r"   EEXISTstatst_inorandomrandintlink_or_copyBaseExceptionremover!   renameshutilcopy)r#   r$   r%   Znew_dstr   r   r   r1   U   s$    


r1   )r   )r   r	   r"   r   r/   r5   r   r   r   dictr   r    r   r   r1   r   r   r   r   <module>   s   


