B
    v9ap7  ã               @   s  d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZedkrddlmZ edddd ddlm Z  e eeeƒ dS )a|  
Dialogs that query users and verify the answer before accepting.

Query is the generic base class for a popup dialog.
The user must either enter a valid answer or close the dialog.
Entries are validated when <Return> is entered or [Ok] is clicked.
Entries are ignored when [Cancel] or [X] are clicked.
The 'return value' is .result set to either a valid answer or None.

Subclass SectionName gets a name for a new config file section.
Configdialog uses it for new highlight theme and keybinding set names.
Subclass ModuleName gets a name for File => Open Module.
Subclass HelpSource gets menu item and path for additions to Help menu.
é    N)Ú
executableÚplatform)ÚToplevelÚ	StringVarÚ
BooleanVarÚWÚEÚS)ÚFrameÚButtonÚEntryÚLabelÚCheckbutton)Ú
filedialog)ÚFontc                   sd   e Zd ZdZdi dddœdd„Zddd	„Zddd„Zdd„ Zddd„Zddd„Z	‡ fdd„Z
‡  ZS )ÚQueryzoBase class for getting verified answer from a user.

    For this base class, accept any non-blank string.
    Ú F)Útext0Ú
used_namesÚ_htestÚ_utestc      	      C   sX  t  | |¡ |  ¡  || _|  |¡ || _|| _|| _|  |¡ |  	¡  | j
 dd¡}|dkr”y| j
 dd| jdd¡ W n   Y nX |  d| j¡ |  d	| j¡ |  d
| j¡ |  d| j¡ |  d| j¡ | jddd |  ¡  |  ¡  |  d| ¡ | ¡ d |  ¡ d   | ¡ |s2| ¡ d |  ¡ d  nd f ¡ |sT|  ¡  |  ¡  dS )aö  Create popup, do not return until tk widget destroyed.

        Additional subclass init must be done before calling this
        unless  _utest=True is passed to suppress wait_window().

        title - string, title of popup dialog
        message - string, informational message to display
        text0 - initial value for entry
        used_names - names already in use
        _htest - bool, change box location when running htest
        _utest - bool, leave window hidden and not modal
        ÚtkÚwindowingsystemZaquaz!::tk::unsupported::MacWindowStyleZstyleZmoveableModalr   z<Command-.>z<Key-Escape>ZWM_DELETE_WINDOWz<Key-Return>z
<KP_Enter>F)ZheightÚwidthz+%d+%dé   é–   N)r   Ú__init__ZwithdrawÚparentÚtitleÚmessager   r   Z	transientZgrab_setr   ZcallZ_wZbindÚcancelZprotocolÚokZ	resizableÚcreate_widgetsZupdate_idletasksZgeometryZwinfo_rootxZwinfo_widthZwinfo_reqwidthZwinfo_rootyZwinfo_heightZwinfo_reqheightZ	deiconifyZwait_window)	Úselfr   r   r   r   r   r   r   r   © r$   ú/usr/lib/python3.7/query.pyr   %   sB    


*zQuery.__init__ÚOKc             C   s.  t | dd | _}|jdddd |jddd t|dd	| jd
}t| | jƒ| _t	|d| jd| _
| j
 ¡  tdd| jd| _t|dd| jd| _t||d| jd| _t|d| jd| _|jddddtd | j
jddddtt ddgd | jjddddtt d | jjdddd | jjdddd d S )Né
   )Zpaddingr   Znews)ÚcolumnÚrowÚstickyé   )ZweightÚwÚleft)ÚanchorÚjustifyÚtexté   )r   ÚtextvariableZTkCaptionFontT)ÚnameÚexistsÚrootú Úred)r0   Ú
foregroundÚfontZactive)r0   ÚdefaultÚcommandZCancel)r0   r;   é   é   )r(   r)   Ú
columnspanÚpadxr*   )r(   r)   r>   r?   r*   Úpadyr   éc   )r(   r)   r?   )r
   ÚframeÚgridZgrid_columnconfigurer   r   r   r   Zentryvarr   ÚentryÚ	focus_setr   r   Ú
error_fontÚentry_errorr   r!   Z	button_okr    Zbutton_cancelr   r   )r#   Úok_textrB   Z
entrylabelr$   r$   r%   r"   W   s.    

zQuery.create_widgetsNc             C   s   d| |p| j d< d S )NzERROR: r0   )rG   )r#   r   Zwidgetr$   r$   r%   Ú	showerrors   s    zQuery.showerrorc             C   s.   d| j d< | j ¡  ¡ }|s*|  d¡ dS |S )zReturn non-blank entry or None.r   r0   zblank line.N)rG   rD   ÚgetÚstriprI   )r#   rD   r$   r$   r%   Úentry_okw   s    

zQuery.entry_okc             C   s.   |   ¡ }|dk	r || _|  ¡  n
| j ¡  dS )zIf entry is valid, bind it to 'result' and destroy tk widget.

        Otherwise leave dialog open for user to correct entry or cancel.
        N)rL   ÚresultÚdestroyrD   rE   )r#   ÚeventrD   r$   r$   r%   r!   €   s
    
zQuery.okc             C   s   d| _ |  ¡  dS )z0Set dialog result to None and destroy tk widget.N)rM   rN   )r#   rO   r$   r$   r%   r       s    zQuery.cancelc                s   |   ¡  tƒ  ¡  d S )N)Zgrab_releaseÚsuperrN   )r#   )Ú	__class__r$   r%   rN   ’   s    zQuery.destroy)r&   )N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   rI   rL   r!   r    rN   Ú__classcell__r$   r$   )rQ   r%   r       s   1

	

r   c                   s0   e Zd ZdZdddœ‡ fdd„
Zdd„ Z‡  ZS )ÚSectionNamez*Get a name for a config file section name.F)r   r   c               s   t ƒ j||||||d d S )N)r   r   r   )rP   r   )r#   r   r   r   r   r   r   )rQ   r$   r%   r   ›   s    zSectionName.__init__c             C   s`   d| j d< | j ¡  ¡ }|s*|  d¡ dS t|ƒdkrD|  d¡ dS || jkr\|  d¡ dS |S )z2Return sensible ConfigParser section name or None.r   r0   zno name specified.Nr1   z"name is longer than 30 characters.zname is already in use.)rG   rD   rJ   rK   rI   Úlenr   )r#   r3   r$   r$   r%   rL       s    




zSectionName.entry_ok)rR   rS   rT   rU   r   rL   rV   r$   r$   )rQ   r%   rW   —   s   rW   c                   s0   e Zd ZdZdddœ‡ fdd„
Zdd„ Z‡  ZS )Ú
ModuleNamez-Get a module name for Open Module menu entry.F)r   r   c               s   t ƒ j||||||d d S )N)r   r   r   )rP   r   )r#   r   r   r   r   r   r   )rQ   r$   r%   r   ´   s    zModuleName.__init__c          
   C   sÜ   d| j d< | j ¡  ¡ }|s*|  d¡ dS ytj |¡}W n4 tt	fk
rn } z|  t
|ƒ¡ dS d}~X Y nX |dkr†|  d¡ dS t|jtjjƒs¤|  d¡ dS y|j |¡}W n" tk
rÖ   | jd| d dS X |S )	z0Return entered module name as file path or None.r   r0   zno name specified.Nzmodule not foundznot a source-based modulez$loader does not support get_filename)r   )rG   rD   rJ   rK   rI   Ú	importlibÚutilÚ	find_specÚ
ValueErrorÚImportErrorÚstrÚ
isinstanceÚloaderÚabcÚSourceLoaderÚget_filenameÚAttributeError)r#   r3   ÚspecÚmsgZ	file_pathr$   r$   r%   rL   ¹   s.    



zModuleName.entry_ok)rR   rS   rT   rU   r   rL   rV   r$   r$   )rQ   r%   rY   °   s   rY   c                   s`   e Zd ZdZddi dddœ‡ fdd„
Z‡ fdd„Zd	d
„ Zdd„ Zej	Z
dd„ Zdd„ Z	‡  ZS )Ú
HelpSourcez,Get menu name and help source for Help menu.r   F)ÚmenuitemÚfilepathr   r   r   c      	   	      s(   || _ d}tƒ j|||||||d dS )z·Get menu entry and url/local file for Additional Help.

        User enters a name for the Help resource and a web url or file
        name. The user can browse for the file.
        zName for item on Help menu:)r   r   r   r   N)rj   rP   r   )	r#   r   r   ri   rj   r   r   r   r   )rQ   r$   r%   r   Ù   s
    zHelpSource.__init__c                sÔ   t ƒ  ¡  | j}t|dddd}t| | jƒ| _t|| jdd| _t	|dd| j
d	}t|d
d| jd| _|jddddddgtd | jjddddtt ddgd |jdddtt d | jjddddtt d d S )Nr,   r-   z,Help File Path: Enter URL or browse for file)r.   r/   r0   é(   )r2   r   ZBrowseé   )r0   r   r;   r6   r7   )r0   r8   r9   r   r'   r<   r=   )r(   r)   r>   r?   r@   r*   é   r   )r(   r)   r>   r?   r*   r@   )r(   r)   r?   r*   é   )r(   r)   r>   r?   r*   )rP   r"   rB   r   r   rj   Úpathvarr   Úpathr   Úbrowse_filerF   Ú
path_errorrC   r   r   r	   )r#   rB   Z	pathlabelZbrowse)rQ   r$   r%   r"   æ   s"    

zHelpSource.create_widgetsc             C   s   t j| |dj||dS )N)r   Ú	filetypes)Z
initialdirZinitialfile)r   ZOpenZshow)r#   rs   ZinitdirZinitfiler$   r$   r%   Úaskfilenameú   s    zHelpSource.askfilenamec             C   s˜   dddddg}| j  ¡ }|r.tj |¡\}}nHd }td d… dkrntj tj t¡d¡}tj 	|¡svt 
¡ }nt 
¡ }|  |||¡}|r”| j  |¡ d S )	N)z
HTML Filesz*.htm *.htmlÚTEXT)z	PDF Filesz*.pdfru   )zWindows Help Filesz*.chm)z
Text Filesz*.txtru   )z	All FilesÚ*r<   ÚwinZDoc)ro   rJ   Úosrp   Úsplitr   ÚjoinÚdirnamer   ÚisdirÚgetcwdrt   Úset)r#   rs   rp   ÚdirÚbaseÚfiler$   r$   r%   rq     s"    

zHelpSource.browse_filec             C   s|   | j  ¡  ¡ }|s$|  d| j¡ dS | d¡sx|dd… dkrJ|dd… }tj  |¡sh|  d| j¡ dS tdkrxd| }|S )	z(Simple validity check for menu file pathzno help file path specified.N)zwww.Zhttpr=   zfile:zhelp file path does not exist.Údarwinzfile://)	rp   rJ   rK   rI   rr   Ú
startswithrx   r4   r   )r#   rp   r$   r$   r%   Úpath_ok  s    
zHelpSource.path_okc             C   s@   d| j d< d| jd< |  ¡ }|  ¡ }|dks4|dkr8dS ||fS )z,Return apparently valid (name, path) or Noner   r0   N)rG   rr   Úitem_okr„   )r#   r3   rp   r$   r$   r%   rL   *  s
    

zHelpSource.entry_ok)rR   rS   rT   rU   r   r"   rt   rq   rW   rL   r…   r„   rV   r$   r$   )rQ   r%   rh   Õ   s   rh   c                   sF   e Zd ZdZddddœ‡ fdd„
Z‡ fdd„Zd	d
„ Zdd„ Z‡  ZS )Ú	CustomRunz†Get settings for custom run of module.

    1. Command line arguments to extend sys.argv.
    2. Whether to restart Shell or not.
    r   F)Úcli_argsr   r   c               s    d}t ƒ j||||||d d S )Nz$Command Line Arguments for sys.argv:)r   r   r   )rP   r   )r#   r   r   r‡   r   r   r   )rQ   r$   r%   r   :  s    zCustomRun.__init__c                sx   t ƒ jdd | j}t| dd| _t|| jdddd}t|dd	| jd
| _|j	dddddd | jj	dddddd d S )NZRun)rH   T)ÚvalueFzRestart shell)ÚvariableZonvalueZoffvaluer0   r6   r7   )r0   r8   r9   r   é   r<   r=   r,   )r(   r)   r>   r?   r*   rn   Zwe)
rP   r"   rB   r   Ú
restartvarr   r   rF   Z
args_errorrC   )r#   rB   Úrestart)rQ   r$   r%   r"   B  s    

zCustomRun.create_widgetsc          
   C   sV   | j  ¡  ¡ }ytj|dd}W n0 tk
rP } z|  t|ƒ¡ dS d}~X Y nX |S )z6Validity check and parsing for command line arguments.T)ÚposixN)rD   rJ   rK   Úshlexry   r]   rI   r_   )r#   Z
cli_stringr‡   Úerrr$   r$   r%   Úcli_args_okO  s    zCustomRun.cli_args_okc             C   s0   d| j d< |  ¡ }| j ¡ }|dkr(dS ||fS )z3Return apparently valid (cli_args, restart) or Noner   r0   N)rG   r   r‹   rJ   )r#   r‡   rŒ   r$   r$   r%   rL   Y  s    

zCustomRun.entry_ok)	rR   rS   rT   rU   r   r"   r   rL   rV   r$   r$   )rQ   r%   r†   2  s   
r†   Ú__main__)Úmainzidlelib.idle_test.test_queryr   F)Ú	verbosityÚexit)Úrun)!rU   rZ   rx   rŽ   Úsysr   r   Ztkinterr   r   r   r   r   r	   Ztkinter.ttkr
   r   r   r   r   r   Ztkinter.fontr   r   rW   rY   rh   r†   rR   Zunittestr’   Zidlelib.idle_test.htestr•   r$   r$   r$   r%   Ú<module>   s$    w%]/
