B
    f³]=Q  ã               @   s  d dl Z d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ	 d dl
mZ d dlmZ d dlZd dlmZ ejr‚dZn yd dlZe ejd¡ W n eejfk
r¶   Y nX dZejdkrøye ¡ d	 Ze  e¡ W n ek
rô   Y nX nžy2e ej ¡ZedksedkrdZe  e¡ W nj e!e"efk
r”   y2e ¡ d	 ZedksdedkrhdZe  e¡ W n e#efk
rŽ   Y nX Y nX e $¡ ZeZe %d
ej&¡Z'e %dej&¡Z(dd„ Z)G dd„ dƒZ*dd„ Z+e,dkrd dl-m.Z. e.dddd d dl/m0Z0 e0e+ƒ dS )é    N)ÚBOM_UTF8)Ú	askstring)ÚidleConfzutf-8Ú ÚasciiZwin32é   z&^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)z^[ \t\f]*(?:[#\r\n]|$)c             C   sÎ   t | tƒr|  d¡}n| }d|kr8| dd¡dd… }n$d|krV| dd¡dd… }n|g}x2|D ]&}t |¡}|dk	rzP t |¡sbdS qbW dS | d¡}yt 	|¡ W n  t
k
rÈ   t
d| ƒ‚Y nX |S )aV  Return the encoding declaration according to PEP 263.

    When checking encoded data, only the first two lines should be passed
    in to avoid a UnicodeDecodeError if the rest of the data is not unicode.
    The first two lines would contain the encoding specification.

    Raise a LookupError if the encoding is declared but unknown.
    z
iso-8859-1Ú
é   Núr   zUnknown encoding: )Ú
isinstanceÚbytesÚdecodeÚsplitÚ	coding_reÚmatchÚblank_reÚgroupÚcodecsÚlookupÚLookupError)ÚdataÚlinesZlstÚliner   Úname© r   úlib/python3.7/idlelib/iomenu.pyÚcoding_specG   s*    	




r   c               @   sþ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdZdd„ Z	dZ
dZdd„ Zd4dd„ZdZe e¡ZejZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdZdZ d'Z!e"j#d(krÔd)nd*Z$d+d,„ Z%d5d.d/„Z&d0d1„ Z'd2d3„ Z(dS )6Ú	IOBindingc             C   sr   || _ |j| _| j d| j¡| _| j d| j¡| _| j d| j¡| _| j d| j	¡| _
d | _| j d| j¡| _d S )Nz<<open-window-from-file>>z<<save-window>>z<<save-window-as-file>>z<<save-copy-of-window-as-file>>z<<print-window>>)ÚeditwinÚtextÚbindÚopenÚ_IOBinding__id_openÚsaveÚ_IOBinding__id_saveÚsave_asÚ_IOBinding__id_saveasÚsave_a_copyÚ_IOBinding__id_savecopyÚfileencodingÚprint_windowÚ_IOBinding__id_print)Úselfr   r   r   r   Ú__init__u   s    

zIOBinding.__init__c             C   sf   | j  d| j¡ | j  d| j¡ | j  d| j¡ | j  d| j¡ | j  d| j¡ d | _d | _ d | _d S )Nz<<open-window-from-file>>z<<save-window>>z<<save-window-as-file>>z<<save-copy-of-window-as-file>>z<<print-window>>)	r   Zunbindr"   r$   r&   r(   r+   r   Úfilename_change_hook)r,   r   r   r   Úclose   s    zIOBinding.closec             C   s
   | j  ¡ S )N)r   Ú	get_saved)r,   r   r   r   r0      s    zIOBinding.get_savedc             C   s   | j  |¡ d S )N)r   Ú	set_saved)r,   Úflagr   r   r   r1      s    zIOBinding.set_savedc             C   s   | j  ¡  d S )N)r   Ú
reset_undo)r,   r   r   r   r3   “   s    zIOBinding.reset_undoNc             C   s
   || _ d S )N)r.   )r,   Úhookr   r   r   Úset_filename_change_hook˜   s    z"IOBinding.set_filename_change_hookc             C   sF   |rt j |¡rd | _|| _n$|| _d | _|  d¡ | jrB|  ¡  d S )Nr   )ÚosÚpathÚisdirÚfilenameÚdirnamer1   r.   )r,   r9   r   r   r   Úset_filenamež   s    
zIOBinding.set_filenamec             C   sÊ   | j j}|rt|s|  ¡ }n|}|r`| j rTt| j dd ƒsT| jsT|  ¡ rT| || j¡ qp| |¡ n| jrp| j 	¡  dS |  ¡ rš|  
¡ }|dkrš| j 	¡  dS |s¨|  ¡ }n|}|r¼|  |¡ n
| j 	¡  dS )NZinterpÚbreakÚcancel)r   ÚflistÚaskopenfileÚgetattrr9   r0   r!   Úloadfiler   Ú	focus_setÚ	maybesave)r,   ÚeventZeditFiler>   r9   Úreplyr   r   r   r!   ©   s6    




zIOBinding.openz(\r\n)|\n|\rc       	   
   C   sB  y<t |dƒ(}| ¡ | ¡  }| d¡ | ¡ }W d Q R X W n8 tk
rt } ztjdt|ƒ| jd dS d }~X Y nX |  	||¡\}}|d kr¨tjdd| | jd dS | j
 |¡}|rÒ| d¡| _| j
 d|¡}| j d	d
¡ |  d ¡ | j d	|¡ |  ¡  |  |¡ |r|  d¡ | j dd	¡ | j d¡ |  |¡ dS )NÚrbr   z	I/O Error)ÚparentFzDecoding ErrorzFile %s
Failed to Decodez\nz1.0ÚendÚinsertT)r!   ÚreadlineÚseekÚreadÚOSErrorÚtkMessageBoxÚ	showerrorÚstrr   Ú_decodeÚeol_reÚsearchr   Úeol_conventionÚsubÚdeleter;   rI   r3   r1   Zmark_setZyviewÚupdaterecentfileslist)	r,   r9   ÚfÚ	two_linesr   ÚmsgÚcharsZ	convertedZfirsteolr   r   r   rA   Ù   s:    





zIOBinding.loadfilec          
   C   s”  d}|  t¡rHy|dd…  d¡}W n tk
r8   dS X d| _|dfS yt|ƒ}W nN tk
r } ztjdd| | j	d	 d}W dd}~X Y n tk
r¢   dS X |rÖyt
||ƒ}|| _|dfS  tk
rÔ   Y nX yt
|d
ƒ}d| _|dfS  tk
r   Y nX yt
|dƒ}d| _|dfS  tk
r4   Y nX y>| jj	 ¡  tddt| jj	d}|rnt
||ƒ}d| _|dfS  ttfk
rŽ   Y nX dS )zCreate a Unicode string.Né   zutf-8)NFÚBOMFzError loading the filez^The encoding '%s' is not known to this Python installation. The file may not display correctly)ÚtitleÚmessagerG   r   zSpecify file encodingzwThe file's encoding is invalid for Python 3.x.
IDLE will convert it to UTF-8.
What is the current encoding of the file?)ZinitialvaluerG   T)Ú
startswithr   r   ÚUnicodeDecodeErrorr)   r   r   rN   rO   r   rP   r   Úupdater   Úencoding)r,   rY   r   r[   Úencr   r   r   r   rQ   ý   sh    





zIOBinding._decodec             C   sp   |   ¡ rdS d| jpd }tjd|tj| jd}|rPd}|  d ¡ |   ¡ sbd}n|d kr^d}nd}| j ¡  |S )NZyesz&Do you want to save %s before closing?zthis untitled documentzSave On Close)r^   r_   ÚdefaultrG   r=   Zno)r0   r9   rN   ZaskyesnocancelZYESr   r#   rB   )r,   r_   ÚconfirmrE   r   r   r   rC   A  s&    


zIOBinding.maybesavec             C   sZ   | j s|  |¡ n:|  | j ¡rL|  d¡ y| j ¡  W n tk
rJ   Y nX | j ¡  dS )NTr<   )	r9   r%   Ú	writefiler1   r   Ústore_file_breaksÚAttributeErrorr   rB   )r,   rD   r   r   r   r#   W  s    

zIOBinding.savec             C   sf   |   ¡ }|rN|  |¡rN|  |¡ |  d¡ y| j ¡  W n tk
rL   Y nX | j ¡  |  	|¡ dS )Nr   r<   )
Úasksavefilerg   r;   r1   r   rh   ri   r   rB   rW   )r,   rD   r9   r   r   r   r%   d  s    




zIOBinding.save_asc             C   s.   |   ¡ }|r|  |¡ | j ¡  |  |¡ dS )Nr<   )rj   rg   r   rB   rW   )r,   rD   r9   r   r   r   r'   r  s    


zIOBinding.save_a_copyc          
   C   s°   |   ¡  | j dd¡}| jdkr.| d| j¡}|  |¡}y:t|dƒ&}| |¡ | ¡  t	 
| ¡ ¡ W d Q R X dS  tk
rª } ztjdt|ƒ| jd dS d }~X Y nX d S )	Nz1.0zend-1cr   ÚwbTz	I/O Error)rG   F)Úfixlastliner   ÚgetrT   ÚreplaceÚencoder!   ÚwriteÚflushr6   ÚfsyncÚfilenorM   rN   rO   rP   )r,   r9   r   r[   rX   rZ   r   r   r   rg   z  s    



zIOBinding.writefilec          
   C   sÚ   t |tƒr|S | jdkr&t| d¡ S y
| d¡S  tk
rD   Y nX yt|ƒ}d }W n* tk
r€ } z|}d }W d d }~X Y n
X |sŠd}|r¶y
| |¡S  tk
r´   d| }Y nX tj	dd| | j
d t| d¡ S )Nr]   zutf-8r   zInvalid encoding '%s'z	I/O Errorz%s.
Saving as UTF-8)rG   )r   r   r)   r   ro   ÚUnicodeErrorr   r   rN   rO   r   )r,   r[   rd   ZfailedrZ   r   r   r   ro   ‹  s4    




zIOBinding.encodec             C   s&   | j  d¡}|dkr"| j  dd¡ d S )Nzend-2cr   zend-1c)r   rm   rI   )r,   Úcr   r   r   rl   ²  s    zIOBinding.fixlastlinec             C   sd  t jddt j| jd}|s(| j ¡  dS d }|  ¡ }|r>| j}|rJ|d kr€tjdd\}}|}t	 
|¡ |  |¡s€t	 |¡ dS t	j}d}|dkrªt d	d
d¡}	|	d }	n|dkrÂt d	d
d¡}	nd}|r6|	t |¡ }	t	 |	d¡}
|
 ¡  ¡ }|
 
¡ }|rd| | }|rPdt|	ƒ | }t jd|| jd nd| }t jd|| jd |r`t	 |¡ dS )NZPrintzPrint to Default Printer)r^   r_   re   rG   r<   Z	IDLE_tmp_)ÚprefixTÚposixÚmainZGeneralzprint-command-posixz 2>&1Úntzprint-command-winFÚrz#Printing failed (exit status 0x%x)
zPrinting command: %s
zPrint status)rG   z-Printing is not enabled for this platform: %s)rN   ZaskokcancelZOKr   rB   r0   r9   ÚtempfileZmkstempr6   r/   rg   Úunlinkr   r   Z	GetOptionÚshlexZquoteÚpopenrL   ÚstripÚreprrO   Zshowinfo)r,   rD   rf   ZtempfilenameZsavedr9   ZtfdÚplatformZprintPlatformZcommandÚpipeÚoutputZstatusr_   r   r   r   r*   ·  sX    







zIOBinding.print_window))zPython filesz
*.py *.pywÚTEXT)z
Text filesz*.txtr„   )z	All filesÚ*Údarwinz.pyr   c             C   s<   |   d¡\}}| js(tj| j| jd| _| jj||d}|S )Nr!   )rG   Ú	filetypes)Ú
initialdirÚinitialfile)ÚdefaultfilenameÚ
opendialogÚtkFileDialogZOpenr   r‡   Úshow)r,   ÚdirÚbaser9   r   r   r   r?   ô  s    zIOBinding.askopenfiler!   c             C   sV   | j rtj | j ¡S | jr$| jdfS yt ¡ }W n tk
rH   d}Y nX |dfS d S )Nr   )r9   r6   r7   r   r:   ÚgetcwdrM   )r,   ÚmodeÚpwdr   r   r   rŠ   ü  s    

zIOBinding.defaultfilenamec             C   s@   |   d¡\}}| js,tj| j| j| jd| _| jj||d}|S )Nr#   )rG   r‡   Údefaultextension)rˆ   r‰   )rŠ   Ú
savedialogrŒ   ZSaveAsr   r‡   r“   r   )r,   rŽ   r   r9   r   r   r   rj     s    zIOBinding.asksavefilec             C   s   | j jr| j  |¡ dS )z-Update recent file list on all editor windowsN)r   r>   Zupdate_recent_files_list)r,   r9   r   r   r   rW     s    zIOBinding.updaterecentfileslist)NN)r!   ))Ú__name__Ú
__module__Ú__qualname__r-   r/   r0   r1   r3   r.   r5   r9   r:   r;   r!   ZeolÚreÚcompilerR   r6   ÚlineseprT   rA   rQ   rC   r#   r%   r'   rg   ro   rl   r*   r‹   r”   r‡   Úsysr   r“   r?   rŠ   rj   rW   r   r   r   r   r   p   s@   
,
$D'2

r   c       	      C   s’   ddl m}m} || ƒ}| d¡ tt|  ¡  d¡dd … ƒ\}}| d||d f ¡ G dd	„ d	ƒ}||ƒ}| ¡  | 	¡  ||ƒ}t
|ƒ d S )
Nr   )ÚToplevelÚTextzTest IOBindingú+r   z+%d+%dé¯   c               @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )z_io_binding.<locals>.MyEditWinc             S   s`   || _ d | _| j  d| j¡ | j  d| j¡ | j  d| j¡ | j  d| j¡ | j  d| j¡ d S )Nz<Control-o>z<Control-p>z<Control-s>z<Alt-s>z<Control-c>)r   r>   r    r!   Úprintr#   ÚsaveasÚsavecopy)r,   r   r   r   r   r-     s    z'_io_binding.<locals>.MyEditWin.__init__c             S   s   dS )Nr   r   )r,   r   r   r   r0   '  ó    z(_io_binding.<locals>.MyEditWin.get_savedc             S   s   d S )Nr   )r,   r2   r   r   r   r1   (  r£   z(_io_binding.<locals>.MyEditWin.set_savedc             S   s   d S )Nr   )r,   r   r   r   r3   )  r£   z)_io_binding.<locals>.MyEditWin.reset_undoc             S   s   | j  d¡ d S )Nz<<open-window-from-file>>)r   Úevent_generate)r,   rD   r   r   r   r!   *  s    z#_io_binding.<locals>.MyEditWin.openc             S   s   | j  d¡ d S )Nz<<print-window>>)r   r¤   )r,   rD   r   r   r   r    ,  s    z$_io_binding.<locals>.MyEditWin.printc             S   s   | j  d¡ d S )Nz<<save-window>>)r   r¤   )r,   rD   r   r   r   r#   .  s    z#_io_binding.<locals>.MyEditWin.savec             S   s   | j  d¡ d S )Nz<<save-window-as-file>>)r   r¤   )r,   rD   r   r   r   r¡   0  s    z%_io_binding.<locals>.MyEditWin.saveasc             S   s   | j  d¡ d S )Nz<<save-copy-of-window-as-file>>)r   r¤   )r,   rD   r   r   r   r¢   2  s    z'_io_binding.<locals>.MyEditWin.savecopyN)r•   r–   r—   r-   r0   r1   r3   r!   r    r#   r¡   r¢   r   r   r   r   Ú	MyEditWin  s   r¥   )Ztkinterrœ   r   r^   ÚmapÚintZgeometryr   ZpackrB   r   )	rG   rœ   r   ÚrootÚxÚyr¥   r   r   r   r   r   Ú_io_binding  s    
 r«   Ú__main__)rx   zidlelib.idle_test.test_iomenur	   F)Ú	verbosityÚexit)Úrun)1r   r   r6   r˜   r}   r›   r{   Ztkinter.filedialogZ
filedialogrŒ   Ztkinter.messageboxZ
messageboxrN   Ztkinter.simpledialogr   ZidlelibZidlelib.configr   Ztestingrc   ZlocaleÚ	setlocaleÚLC_CTYPEÚImportErrorÚErrorZlocale_decoder   ZgetdefaultlocaleZlocale_encodingr   r   Únl_langinfoÚCODESETÚ	NameErrorri   Ú
ValueErrorÚlowerr™   ÚASCIIr   r   r   r   r«   r•   Zunittestrx   Zidlelib.idle_test.htestr¯   r   r   r   r   Ú<module>   sj   
)   *$
