o
    ǋ|cc:                     @   sZ  d Z g dZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ i ZG dd	 d	ZG d
d deZG dd deZG dd dejZG dd deZG dd deZ G dd dejZ!dd Z"dd Z#dd Z$d)ddZ%d)dd Z&d*d"d#Z'd$d% Z(d&d' Z)e*d(kre)  dS dS )+aU  File selection dialog classes.

Classes:

- FileDialog
- LoadFileDialog
- SaveFileDialog

This module also presents tk common file dialogues, it provides interfaces
to the native file dialogues available in Tk 4.2 and newer, and the
directory dialogue available in Tk 8.3 and newer.
These interfaces were written by Fredrik Lundh, May 1997.
)
FileDialogLoadFileDialogSaveFileDialogOpenSaveAs	Directoryaskopenfilenameasksaveasfilenameaskopenfilenamesaskopenfileaskopenfilesasksaveasfileaskdirectory    N)FrameLEFTYESBOTTOMEntryTOPButtonTkXToplevelRIGHTYENDListboxBOTH	Scrollbar)Dialog)commondialog)_setup_dialogc                   @   s   e Zd ZdZdZd$ddZejdddfdd	Zd$d
dZ	dd Z
dd Zdd Zdd Zdd Zdd Zd$ddZdd Zdd Zd$ddZd d! Zd"d# ZdS )%r   a  Standard file selection dialog -- no checks on selected file.

    Usage:

        d = FileDialog(master)
        fname = d.go(dir_or_file, pattern, default, key)
        if fname is None: ...canceled...
        else: ...open file...

    All arguments to go() are optional.

    The 'key' argument specifies a key in the global dictionary
    'dialogstates', which keeps track of the values for the directory
    and pattern arguments, overriding the values passed in (it does
    not keep track of the default argument!).  If no key is specified,
    the dialog keeps no memory of previous state.  Note that memory is
    kept even when the dialog is canceled.  (All this emulates the
    behavior of the Macintosh file selection dialogs.)

    zFile Selection DialogNc                 C   s  |d u r| j }|| _d | _t|| _| j | | j| t| j t| j| _| jj	t
td t| j| _| jj	t
td | jd| j t| j| _| jj	ttd | jd| j t| j| _| jj	ttd t| j| _| jj	ttd t| jd| jdfd| _| jj	tttd | j }| j|dd  |d d   | jd	| j | jd
| j | jj| jdfd t| j| _ | j j	t!td t| jd| j dfd| _"| j"j	t!ttd | j j| j"dfd | j" }| j"|dd  |d d   | j"d	| j# | j"d
| j$ t%| jd| j&d| _'| j'j	t!d t%| jd| jd| _(| j(j	t!td t%| jd| j)d| _*| j*j	td | j+d| j) | jd| j) | jd| j) d S )N)sidefillz<Return>)expandr#   r   set)ZexportselectionZyscrollcommand)r"   r$   r#      z<ButtonRelease-1>z<Double-ButtonRelease-1>Zyview)commandZOK)textr'   )r"   Filter)r"   r$   CancelZWM_DELETE_WINDOWz<Alt-w>z<Alt-W>),titlemaster	directoryr   topZiconnamer!   r   Zbotframepackr   r   r   	selectionbindok_eventfilterr   filter_commandZmidframer   r   r   Zfilesbarr   r   r   filesZbindtagsfiles_select_eventfiles_double_eventZconfigZdirsbarr   dirsdirs_select_eventdirs_double_eventr   
ok_commandZ	ok_buttonZfilter_buttoncancel_commandZcancel_buttonprotocol)selfr,   r+   Zbtags r?   $  /home/conda/feedstock_root/build_artifacts/python-split_1669104742391/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/lib/python3.10/tkinter/filedialog.py__init__:   sr   


 
 zFileDialog.__init__* c                 C   s   |r|t v rt | \| _}ntj|}tj|r|| _n	tj|\| _}| | j| | | | 	  | j
  | j  | j  d | _| j  |rf|  \}}| jr`tj| j}||ft |< | j  | jS N)dialogstatesr-   ospath
expanduserisdirsplit
set_filterset_selectionr4   r0   Z	focus_setr.   Zwait_visibilityZgrab_sethowr,   Zmainloop
get_filterdirnameZdestroy)r>   Zdir_or_filepatterndefaultkeyr-   r?   r?   r@   goz   s*   





zFileDialog.goc                 C   s   || _ | j  d S rD   )rM   r,   quit)r>   rM   r?   r?   r@   rT      s   zFileDialog.quitc                 C      |    d S rD   )r4   r>   eventr?   r?   r@   r:         zFileDialog.dirs_double_eventc                 C   s@   |   \}}| jd}tjtj| j|}| || d S NZactive)	rN   r8   getrF   rG   normpathjoinr-   rK   )r>   rW   dirpatZsubdirr?   r?   r@   r9      s   zFileDialog.dirs_select_eventc                 C   rU   rD   r;   rV   r?   r?   r@   r7      rX   zFileDialog.files_double_eventc                 C   s   | j d}| | d S rY   )r5   rZ   rL   )r>   rW   filer?   r?   r@   r6      s   zFileDialog.files_select_eventc                 C   rU   rD   r_   rV   r?   r?   r@   r2      rX   zFileDialog.ok_eventc                 C   s   |  |   d S rD   )rT   get_selectionr>   r?   r?   r@   r;      s   zFileDialog.ok_commandc                 C   s"  |   \}}zt|}W n ty   | j  Y d S w || _| || |  tj	g}g }|D ] }tj
||}tj
|rG|| q2t||rR|| q2| jdt |D ]	}| jt| q\| jdt |D ]	}| jt| qotj
|  \}	}
|
tjkrd}
| |
 d S )Nr   rC   )rN   rF   listdirOSErrorr,   bellr-   rK   sortpardirrG   r\   rI   appendfnmatchr8   deleter   insertr5   rJ   ra   curdirrL   )r>   rW   r]   r^   namessubdirsZmatchingfilesnamefullnameheadtailr?   r?   r@   r4      s8   

zFileDialog.filter_commandc                 C   sN   | j  }tj|}|dd  tjkstj|r!tj|d}tj|S )NrB   )	r3   rZ   rF   rG   rH   seprI   r\   rJ   )r>   r3   r?   r?   r@   rN      s
   
zFileDialog.get_filterc                 C   s   | j  }tj|}|S rD   )r0   rZ   rF   rG   rH   r>   r`   r?   r?   r@   ra      s   
zFileDialog.get_selectionc                 C   rU   rD   )rT   rV   r?   r?   r@   r<      rX   zFileDialog.cancel_commandc                 C   s   t j|s'zt  }W n ty   d }Y nw |r't j||}t j|}| jdt	 | j
t	t j|p9t j|p<d d S )Nr   rB   )rF   rG   isabsgetcwdrd   r\   r[   r3   rj   r   rk   rl   )r>   r]   r^   pwdr?   r?   r@   rK      s   &zFileDialog.set_filterc                 C   s,   | j dt | j ttj| j| d S )Nr   )r0   rj   r   rk   rF   rG   r\   r-   ru   r?   r?   r@   rL      s   zFileDialog.set_selectionrD   )__name__
__module____qualname____doc__r+   rA   rF   rl   rS   rT   r:   r9   r7   r6   r2   r;   r4   rN   ra   r<   rK   rL   r?   r?   r?   r@   r   !   s$    
@


r   c                   @      e Zd ZdZdZdd ZdS )r   z8File selection dialog which checks that the file exists.zLoad File Selection Dialogc                 C   s0   |   }tj|s| j  d S | | d S rD   )ra   rF   rG   isfiler,   re   rT   ru   r?   r?   r@   r;      s   zLoadFileDialog.ok_commandNry   rz   r{   r|   r+   r;   r?   r?   r?   r@   r          r   c                   @   r}   )r   z@File selection dialog which checks that the file may be created.zSave File Selection Dialogc                 C   s   |   }tj|r-tj|r| j  d S t| jdd|f dddd}|j	dkr,d S ntj
|\}}tj|sB| j  d S | | d S )Nz Overwrite Existing File QuestionzOverwrite existing file %r?Z	questheadr&   )ZYesr*   )r+   r(   ZbitmaprQ   stringsr   )ra   rF   rG   existsrI   r,   re   r   r.   numrJ   rT   )r>   r`   drq   rr   r?   r?   r@   r;      s(   


zSaveFileDialog.ok_commandNr   r?   r?   r?   r@   r      r   r   c                   @   s   e Zd Zdd Zdd ZdS )_Dialogc                 C   s0   zt | jd | jd< W d S  ty   Y d S w )N	filetypes)tupleoptionsKeyErrorrb   r?   r?   r@   _fixoptions2  s
   z_Dialog._fixoptionsc                 C   sP   |r#z|j }W n	 ty   Y nw tj|\}}|| jd< || jd< || _|S )N
initialdirZinitialfile)stringAttributeErrorrF   rG   rJ   r   filenamer>   widgetresultrG   r`   r?   r?   r@   
_fixresult9  s   


z_Dialog._fixresultN)ry   rz   r{   r   r   r?   r?   r?   r@   r   0  s    r   c                   @   r}   )r   Ask for a filename to openZtk_getOpenFilec                 C   sx   t |tr!tdd |D }|rtj|d \}}|| jd< |S |j s5d| jv r5| ||j	|S t
| ||S )Nc                 S   s   g | ]}t |d |qS )r   )getattr).0rr?   r?   r@   
<listcomp>T  s    z#Open._fixresult.<locals>.<listcomp>r   r   multiple)
isinstancer   rF   rG   rJ   r   ZtkZwantobjectsr   Z	splitlistr   r   r?   r?   r@   r   Q  s   

zOpen._fixresultNry   rz   r{   r|   r'   r   r?   r?   r?   r@   r   L      r   c                   @   s   e Zd ZdZdZdS )r   Ask for a filename to save asZtk_getSaveFileN)ry   rz   r{   r|   r'   r?   r?   r?   r@   r   `  s    r   c                   @   r}   )r   zAsk for a directoryZtk_chooseDirectoryc                 C   s6   |rz|j }W n	 ty   Y nw || jd< || _|S )Nr   )r   r   r   r-   )r>   r   r   r?   r?   r@   r   l  s   

zDirectory._fixresultNr   r?   r?   r?   r@   r   g  r   r   c                  K      t di |  S )r   Nr?   r   showr   r?   r?   r@   r   }     r   c                  K   r   )r   Nr?   )r   r   r   r?   r?   r@   r     r   r   c                  K   s   d| d< t di |  S )ztAsk for multiple filenames to open

    Returns a list of filenames or empty list if
    cancel button selected
    r&   r   Nr?   r   r   r?   r?   r@   r	     s   r	   r   c                 K   $   t di | }|rt|| S dS )z8Ask for a filename to open, and returned the opened fileNr?   )r   r   openmoder   r   r?   r?   r@   r
        
r
   c                 K   s8   t di |}|rg }|D ]
}|t||  q|}|S )zAsk for multiple filenames and return the open file
    objects

    returns a list of open file objects or an empty list if
    cancel selected
    Nr?   )r	   rh   r   )r   r   r5   Zofilesr   r?   r?   r@   r     s   r   wc                 K   r   )z;Ask for a filename to save as, and returned the opened fileNr?   )r   r   r   r   r?   r?   r@   r     r   r   c                  K   r   )z-Ask for a directory, and return the file nameNr?   )r   r   r   r?   r?   r@   r     s   r   c            
   	   C   s   t  } |   t| }|jdd}t| }|jdd}t|| d}ddl}zddl}||j	d |
|j}W n ttfyD   Y nw tdgd}zt|d	}|  W n   td
 t| d  Y td|| t }	td|	| dS )zSimple test program.test)rR   zutf-8r   NrC   )z	all filesrB   )r   r   zCould not open File: r&   r   Zsaveas)r   Zwithdrawr   rS   r   printsyslocale	setlocaleLC_ALLnl_langinfoCODESETImportErrorr   r   r   closeexc_infoencoder   )
rootfdZloadfileZsavefileencr   r   ZopenfilenamefpZsaveasfilenamer?   r?   r@   r     s4   

r   __main__)r   )r   )+r|   __all__ri   rF   tkinterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Ztkinter.dialogr   r    Ztkinter.simpledialogr!   rE   r   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   ry   r?   r?   r?   r@   <module>   s8    H I9

	
	,
