B
    uG_˜P  ã               @   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dZnÞyd dlZe ejd¡ W n eejfk
r¸   Y nX ejdkrÎdZdZn”ye ej¡Zerêe  e¡ W nX ee e!fk
rD   y e "¡ d	 Zer"e  e¡ W n e#e!fk
r>   Y nX Y nX erZe $¡ Zd
ZndZd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ÚsurrogateescapeÚ Úwin32é   ÚstrictÚasciiz&^[ \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   ú0/opt/hscpipe/8.4/lib/python3.7/idlelib/iomenu.pyÚcoding_specB   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__p   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   r3   ˆ   s    zIOBinding.get_savedc             C   s   | j  |¡ d S )N)r!   Ú	set_saved)r/   Úflagr   r   r   r4   ‹   s    zIOBinding.set_savedc             C   s   | j  ¡  d S )N)r!   Ú
reset_undo)r/   r   r   r   r6   Ž   s    zIOBinding.reset_undoNc             C   s
   || _ d S )N)r1   )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Údirnamer4   r1   )r/   r<   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 )NÚinterpÚbreakÚcancel)r!   ÚflistÚaskopenfileÚgetattrr<   r3   r$   Úloadfiler"   Ú	focus_setÚ	maybesave)r/   ÚeventZeditFilerB   r<   Ú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>   rM   r6   r4   Zmark_setZyviewÚupdaterecentfileslist)	r/   r<   ÚfÚ	two_linesr   ÚmsgÚcharsZ	convertedZfirsteolr   r   r   rE   Ô   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ÚmessagerK   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initialvaluerK   T)Ú
startswithr   r   ÚUnicodeDecodeErrorr,   r   r   rR   rS   r"   rT   r!   Úupdater   Úencoding)r/   r]   r   r_   Úencr   r   r   r   rU   ø   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)rb   rc   ÚdefaultrK   rA   Úno)r3   r<   rR   ZaskyesnocancelZYESr"   r&   rF   )r/   rc   ÚconfirmrI   r   r   r   rG   <  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@   )	r<   r(   Ú	writefiler4   r!   Ústore_file_breaksÚAttributeErrorr"   rF   )r/   rH   r   r   r   r&   R  s    

zIOBinding.savec             C   sf   |   ¡ }|rN|  |¡rN|  |¡ |  d¡ y| j ¡  W n tk
rL   Y nX | j ¡  |  	|¡ dS )Nr   r@   )
Úasksavefilerl   r>   r4   r!   rm   rn   r"   rF   r[   )r/   rH   r<   r   r   r   r(   _  s    




zIOBinding.save_asc             C   s.   |   ¡ }|r|  |¡ | j ¡  |  |¡ dS )Nr@   )ro   rl   r"   rF   r[   )r/   rH   r<   r   r   r   r*   m  s    


zIOBinding.save_a_copyc          
   C   sŠ   |   ¡ }|  |¡}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 )NÚwbTz	I/O Error)rK   F)ÚfixnewlinesÚencoder$   ÚwriteÚflushr9   ÚfsyncÚfilenorQ   rR   rS   rT   r"   )r/   r<   r"   r_   r\   r^   r   r   r   rl   u  s    


zIOBinding.writefilec             C   sT   | j  d¡dkr*t| jdƒs*| j  dd¡ | j  dd¡}| jdkrP| d| j¡}|S )z/Return text with final 
 if needed and os eols.zend-2cr   r?   zend-1cz1.0)r"   ÚgetÚhasattrr!   rM   rX   Úreplace)r/   r"   r   r   r   rq   ƒ  s    
zIOBinding.fixnewlinesc          
   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 )Nra   zutf-8r
   zInvalid encoding '%s'z	I/O Errorz%s.
Saving as UTF-8)rK   )r   r   r,   r   rr   ÚUnicodeErrorr   r   rR   rS   r"   )r/   r_   rh   Zfailedr^   r   r   r   rr     s4    




zIOBinding.encodec             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)rb   rc   ri   rK   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)rK   z-Printing is not enabled for this platform: %s)rR   ZaskokcancelZOKr"   rF   r3   r<   ÚtempfileZmkstempr9   r2   rl   Úunlinkr   r   Z	GetOptionÚshlexZquoteÚpopenrP   ÚstripÚreprrS   Zshowinfo)r/   rH   rk   ZtempfilenameZsavedr<   ZtfdÚplatformZprintPlatformZcommandÚpipeÚoutputÚstatusrc   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$   )rK   Ú	filetypes)Ú
initialdirÚinitialfile)ÚdefaultfilenameÚ
opendialogÚtkFileDialogZOpenr"   r   Úshow)r/   ÚdirÚbaser<   r   r   r   rC   ñ  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   )r<   r9   r:   r   r=   ÚgetcwdrQ   )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&   )rK   r   Údefaultextension)rŽ   r   )r   Ú
savedialogr’   ZSaveAsr"   r   r™   r“   )r/   r”   r•   r<   r   r   r   ro     s    zIOBinding.asksavefilec             C   s   | j jr| j  |¡ dS )z-Update recent file list on all editor windowsN)r!   rB   Zupdate_recent_files_list)r/   r<   r   r   r   r[     s    zIOBinding.updaterecentfileslist)NN)r$   ))Ú__name__Ú
__module__Ú__qualname__r0   r2   r3   r4   r6   r1   r8   r<   r=   r>   r$   ZeolÚreÚcompilerV   r9   ÚlineseprX   rE   rU   rG   r&   r(   r*   rl   rq   rr   r-   r‘   rš   r   Úsysr†   r™   rC   r   ro   r[   r   r   r   r   r    k   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"   rB   r#   r$   Úprintr&   ÚsaveasÚsavecopy)r/   r"   r   r   r   r0     s    z'_io_binding.<locals>.MyEditWin.__init__c             S   s   dS )Nr   r   )r/   r   r   r   r3   $  ó    z(_io_binding.<locals>.MyEditWin.get_savedc             S   s   d S )Nr   )r/   r5   r   r   r   r4   %  r©   z(_io_binding.<locals>.MyEditWin.set_savedc             S   s   d S )Nr   )r/   r   r   r   r6   &  r©   z)_io_binding.<locals>.MyEditWin.reset_undoc             S   s   | j  d¡ d S )Nz<<open-window-from-file>>)r"   Úevent_generate)r/   rH   r   r   r   r$   '  s    z#_io_binding.<locals>.MyEditWin.openc             S   s   | j  d¡ d S )Nz<<print-window>>)r"   rª   )r/   rH   r   r   r   r¦   )  s    z$_io_binding.<locals>.MyEditWin.printc             S   s   | j  d¡ d S )Nz<<save-window>>)r"   rª   )r/   rH   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/   rH   r   r   r   r§   -  s    z%_io_binding.<locals>.MyEditWin.saveasc             S   s   | j  d¡ d S )Nz<<save-copy-of-window-as-file>>)r"   rª   )r/   rH   r   r   r   r¨   /  s    z'_io_binding.<locals>.MyEditWin.savecopyN)r›   rœ   r   r0   r3   r4   r6   r$   r¦   r&   r§   r¨   r   r   r   r   Ú	MyEditWin  s   r«   )Ztkinterr¢   r£   rb   ÚmapÚintZgeometryr   ÚpackrF   r    )	rK   r¢   r£   ÚrootÚxÚyr«   r"   r!   r   r   r   Ú_io_binding  s    
 r²   Ú__main__)r}   zidlelib.idle_test.test_iomenur   F)Ú	verbosityÚexit)Úrun)1r   r   r9   rž   r‚   r¡   r€   Ztkinter.filedialogZ
filedialogr’   Ztkinter.messageboxZ
messageboxrR   Ztkinter.simpledialogr   ZidlelibZidlelib.configr   Ztestingrg   ÚerrorsÚlocaleÚ	setlocaleÚLC_CTYPEÚImportErrorÚErrorr†   Únl_langinfoÚCODESETZlocale_encodingr   Ú	NameErrorrn   r   ÚgetdefaultlocaleÚ
ValueErrorÚlowerrŸ   ÚASCIIr   r   r   r    r²   r›   Zunittestr}   Zidlelib.idle_test.htestr¶   r   r   r   r   Ú<module>   sf   
)   ,$
