a
    ze=                     @   s   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lmZ d dlmZ d dl	m
Z
 d dlZd dlmZ d dlmZ ddd	 eD Zd
ZejdkrdZndZG dd dZdd Zedkrd dlmZ edddd d dlmZ ee dS )    N)
filedialog)
messagebox)	askstring)idleConf)py_extensions c                 c   s   | ]}d | V  qdS )*N ).0extr	   r	   $/usr/lib/python3.9/idlelib/iomenu.py	<genexpr>       r   utf-8win32surrogatepasssurrogateescapec                   @   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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ZdZd$ed%fd&d'fZejd(krd)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 )N<<open-window-from-file>><<save-window>><<save-window-as-file>><<save-copy-of-window-as-file>>r   <<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__   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 )Nr   r   r   r   r   )	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_savedr*   r	   r	   r   r-   7   s    zIOBinding.get_savedc                 C   s   | j | d S r,   )r   	set_savedr'   flagr	   r	   r   r.   :   s    zIOBinding.set_savedc                 C   s   | j   d S r,   )r   
reset_undor*   r	   r	   r   r1   =   s    zIOBinding.reset_undoNc                 C   s
   || _ d S r,   )r)   )r'   hookr	   r	   r   set_filename_change_hookB   s    z"IOBinding.set_filename_change_hookc                 C   sF   |rt j|rd | _|| _n$|| _d | _| d | jrB|   d S )N   )ospathisdirfilenamedirnamer.   r)   r'   r8   r	   r	   r   set_filenameH   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getattrr8   r-   r   loadfiler   	focus_set	maybesave)r'   eventZeditFiler?   r8   replyr	   r	   r   r   S   s<    




zIOBinding.openc           	      C   s  zzFt |(}| }|j}|j}d}W d    n1 s<0    Y  W nz ttfy   | jj	  t
ddd| jjd}t||d(}| }|j}|j}d}W d    n1 s0    Y  Y n0 W nh ty } z$tjdt|| jd	 W Y d }~dS d }~0  ty.   tjd
d| | jd	 Y dS 0 t|tsb|d ur\tjdd| jd	 d}tj}| jdd | d  || _|| _| jd| |   | | |r| d | jdd | jd | | dS )NFz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?r   )Zinitialvalueparent)encodingT	I/O ErrorrG   zDecoding ErrorzFile %s
Failed to DecodezMixed Newlinesz:Mixed newlines detected.
The file will be changed on save.1.0endinsert)tokenizer   readrH   newlinesUnicodeDecodeErrorSyntaxErrorr   r   updater   OSErrorr   	showerrorstr
isinstanceshowwarningr5   linesepdeleter;   r$   eol_conventionrM   r1   r.   Zmark_setZyviewupdaterecentfileslist)	r'   r8   fcharsr$   r[   Z	convertedencerrr	   r	   r   rB      sd    &,




zIOBinding.loadfilec                 C   sp   |   rdS d| jpd }tjd|tj| jd}|rPd}| d  |   sbd}n|d u r^d}nd}| j  |S )NZyesz&Do you want to save %s before closing?zthis untitled documentzSave On ClosetitlemessagedefaultrG   r>   no)r-   r8   r   ZaskyesnocancelZYESr   r   rC   )r'   rc   confirmrF   r	   r	   r   rD      s*    

zIOBinding.maybesavec                 C   sX   | j s| | n8| | j rJ| d z| j  W n tyH   Y n0 | j  dS )NTr=   )	r8   r    	writefiler.   r   store_file_breaksAttributeErrorr   rC   r'   rE   r	   r	   r   r      s    

zIOBinding.savec                 C   sd   |   }|rL| |rL| | | d z| j  W n tyJ   Y n0 | j  | 	| dS )Nr4   r=   )
asksavefilerg   r;   r.   r   rh   ri   r   rC   r\   r'   rE   r8   r	   r	   r   r       s    




zIOBinding.save_asc                 C   s.   |   }|r| | | j  | | dS )Nr=   )rk   rg   r   rC   r\   rl   r	   r	   r   r"      s    


zIOBinding.save_a_copyc              
   C   s   |   }| |}zPt|d0}|| |  t|  W d    n1 sT0    Y  W dS  ty } z$t	j
dt|| jd W Y d }~dS d }~0 0 d S )NwbTrI   rJ   F)fixnewlinesencoder   writeflushr5   fsyncfilenorT   r   rU   rV   r   )r'   r8   r   r^   r]   msgr	   r	   r   rg      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-2c
r<   zend-1crK   )r   gethasattrr   rM   r[   replacer'   r   r	   r	   r   rn     s    

zIOBinding.fixnewlinesc              
   C   s   t |tr|S | jdkr"|dS z|dW S  ty@   Y n0 z.|dd}tt|j	\}}||W S  t
y } zt|}W Y d }~n"d }~0  ty   d| }Y n0 tjdd| | jd |dS )Nz	utf-8-sigasciirx   zInvalid encoding '%s'rI   z%s.
Saving as UTF-8rJ   )rW   bytesr$   ro   UnicodeEncodeErrorrN   detect_encodingioBytesIOreadlinerR   rV   r   rU   r   )r'   r^   encodedr_   _r`   Zfailedr	   r	   r   ro     s,    


zIOBinding.encodec                 C   sd  t jddt j| jd}|s(| j  dS d }|  }|r>| j}|rJ|d u rtjdd\}}|}t	
| | |st	| dS t	j}d}|dkrtd	d
d}	|	d }	n|dkrt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 Printerra   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 statusrJ   z-Printing is not enabled for this platform: %s)r   ZaskokcancelZOKr   rC   r-   r8   tempfilemkstempr5   r+   rg   unlinknamer   Z	GetOptionshlexquotepopenrO   stripreprrU   Zshowinfo)r'   rE   rf   ZtempfilenameZsavedr8   ZtfdplatformZprintPlatformZcommandpipeoutputstatusrc   r	   r	   r   r%   ,  sb    





zIOBinding.print_windowzPython filesTEXT)z
Text filesz*.txtr   )z	All filesr   darwinz.py c                 C   s<   |  d\}}| js(tj| j| jd| _| jj||d}|S )Nr   )rG   	filetypesZ
initialdirZinitialfile)defaultfilename
opendialogr   ZOpenr   r   showr'   dirbaser8   r	   r	   r   r@   i  s    zIOBinding.askopenfiler   c                 C   sT   | j rtj| j S | jr$| jdfS zt }W n tyF   d}Y n0 |dfS d S )Nr   )r8   r5   r6   splitr9   getcwdrT   )r'   modepwdr	   r	   r   r   q  s    

zIOBinding.defaultfilenamec                 C   s@   |  d\}}| js,tj| j| j| jd| _| jj||d}|S )Nr   )rG   r   defaultextensionr   )r   
savedialogr   ZSaveAsr   r   r   r   r   r	   r	   r   rk   }  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_listr:   r	   r	   r   r\     s    zIOBinding.updaterecentfileslist)NN)r   )%__name__
__module____qualname__r(   r+   r-   r.   r1   r)   r3   r8   r9   r;   r   r5   rY   r[   rB   rD   r   r    r"   rg   rn   ro   r%   r   r   r   r   sysr   r   r@   r   rk   r\   r	   r	   r	   r   r      s@   
,;
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+r4   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savecopyry   r	   r	   r   r(     s    z'_io_binding.<locals>.MyEditWin.__init__c                 S   s   dS )Nr   r	   r*   r	   r	   r   r-     r   z(_io_binding.<locals>.MyEditWin.get_savedc                 S   s   d S r,   r	   r/   r	   r	   r   r.     r   z(_io_binding.<locals>.MyEditWin.set_savedc                 S   s   d S r,   r	   r*   r	   r	   r   r1     r   z)_io_binding.<locals>.MyEditWin.reset_undoc                 S   s   | j d d S )Nr   r   Zevent_generaterj   r	   r	   r   r     s    z#_io_binding.<locals>.MyEditWin.openc                 S   s   | j d d S )Nr   r   rj   r	   r	   r   r     s    z$_io_binding.<locals>.MyEditWin.printc                 S   s   | j d d S )Nr   r   rj   r	   r	   r   r     s    z#_io_binding.<locals>.MyEditWin.savec                 S   s   | j d d S )Nr   r   rj   r	   r	   r   r     s    z%_io_binding.<locals>.MyEditWin.saveasc                 S   s   | j d d S )Nr   r   rj   r	   r	   r   r     s    z'_io_binding.<locals>.MyEditWin.savecopyN)r   r   r   r(   r-   r.   r1   r   r   r   r   r   r	   r	   r	   r   	MyEditWin  s   r   )tkinterr   r   rb   mapintZgeometryr   packrC   r   )	rG   r   r   rootxyr   r   r   r	   r	   r   _io_binding  s    
 r   __main__)r   zidlelib.idle_test.test_iomenu   F)	verbosityexit)run)r~   r5   r   r   r   rN   r   r   r   Ztkinter.simpledialogr   ZidlelibZidlelib.configr   Zidlelib.utilr   joinrH   r   errorsr   r   r   Zunittestr   Zidlelib.idle_test.htestr   r	   r	   r	   r   <module>   s2   
  t$