B
    x\1                 @   s`   d Z ddlZddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZ dddZdd
dZdS )z io on the clipboard     N)PY2PY3StringIO)ABCDataFrame)
get_optionoption_context\s+c             K   s  | dd}|dk	r0| dddkr0tddd	lm} dd
lm} | }try t	j
||dpjtdd}W n tk
r   Y nX |dd ddd dd }dd |D }t|dkrt|dkr|  dkrd} | dkr|ddkrd} t| dkr"|ddkr"d|d< n(t| dkrJ|ddkrJtd |ddkrjtrj|d}|t|fd| i|S )a=  
    Read text from clipboard and pass to read_csv. See read_csv for the
    full argument list

    Parameters
    ----------
    sep : str, default '\s+'
        A string or regex delimiter. The default of '\s+' denotes
        one or more whitespace characters.

    Returns
    -------
    parsed : DataFrame
    encodingzutf-8N- utf8z3reading from clipboard only supports utf-8 encodingr   )clipboard_get)read_csvzdisplay.encoding)r	   i'  

   c             S   s   h | ]}|  d qS )	)lstripcount).0x r   3lib/python3.7/site-packages/pandas/io/clipboards.py	<setcomp>>   s    z!read_clipboard.<locals>.<setcomp>   r   Zdelim_whitespacez\s+ZengineZpythonczHread_clipboard with regex separator does not work properly with c enginesep)poplowerreplaceNotImplementedErrorpandas.io.clipboardr   Zpandas.io.parsersr   r   compatZbytes_to_strgetr   AttributeErrorsplitlenwarningswarnr   encoder   )r   kwargsr	   r   r   textlinesZcountsr   r   r   read_clipboard   s6    "$


r-   Tc       	   	   K   s  | dd}|dk	r0| dddkr0tddd	lm} |dkrHd
}|ryL|dkrZd}t }| j|f|dd| | }t	r|
d}|| dS  tk
r   td Y qX n|dk	rtd t| trtdd | jf |}W dQ R X nt| }|| dS )a  
    Attempt to write text representation of object to the system clipboard
    The clipboard can be then pasted into Excel for example.

    Parameters
    ----------
    obj : the object to write to the clipboard
    excel : boolean, defaults to True
            if True, use the provided separator, writing in a csv
            format for allowing easy pasting into excel.
            if False, write a string representation of the object
            to the clipboard
    sep : optional, defaults to tab
    other keywords are passed to to_csv

    Notes
    -----
    Requirements for your platform
      - Linux: xclip, or xsel (with gtk or PyQt4 modules)
      - Windows:
      - OS X:
    r	   zutf-8Nr
   r   r   z&clipboard only supports utf-8 encodingr   )clipboard_setTr   )r   r	   zAto_clipboard in excel mode requires a single character separator.z6to_clipboard with excel=False ignores the sep argumentzdisplay.max_colwidthi?B )r   r   r   
ValueErrorr!   r.   r   Zto_csvgetvaluer   decode	TypeErrorr'   r(   
isinstancer   r   Z	to_stringstr)	objZexcelr   r*   r	   r.   Zbufr+   Zobjstrr   r   r   to_clipboardX   s4    


r6   )r   )TN)__doc__r'   Zpandas.compatr"   r   r   r   Zpandas.core.dtypes.genericr   Zpandasr   r   r-   r6   r   r   r   r   <module>   s   
L