a
    ze:                     @   s4  d Z ddl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 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G dd deZ e!dkr0d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)Font)_setup_dialogc                       sl   e Zd ZdZdi dddddZddd	Z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   s8  || _ || _|| _|| _t| | |   | | | | |sL| 	  t
|  | jdkrl| d| j | d| j | d| j | d| j | d| j |   |   | d| | d |  d   | |s| d |  d  nd	 f  | jd
d
d |s4|   | j  |   dS )a  Create modal popup, return when destroyed.

        Additional subclass init must be done before 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
        Zaquaz<Command-.>z<Key-Escape>ZWM_DELETE_WINDOWz<Key-Return>z
<KP_Enter>z+%d+%d      F)ZheightwidthN)parentmessager   r   r   __init__ZwithdrawtitleZ	transientZgrab_setr   Z_windowingsystembindcancelprotocolokcreate_widgetsZupdate_idletasksZgeometryZwinfo_rootxZwinfo_widthZwinfo_reqwidthZwinfo_rootyZwinfo_heightZwinfo_reqheightZ	resizableZ	deiconifyentry	focus_setZwait_window)selfr   r   r   r   r   r   r    r(   #/usr/lib/python3.9/idlelib/query.pyr   &   sH    



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| _
tdd| jd| _t|d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 |   t||d| jd| _t|d| jd| _| jjdddd | jjdddd dS )zCreate entry (rows, extras, buttons.

        Entry stuff on rows 0-2, spanning cols 0-2.
        Buttons on row 99, cols 1, 2.
        
   )Zpaddingr   news)columnrowsticky   )ZweightwleftanchorZjustifytext   )r   textvariableZTkCaptionFontT)nameexistsroot redr5   Z
foregroundZfont      r-   r.   
columnspanpadxr/   r-   r.   rA   rB   r/   padyr   Zactive)r5   defaultcommandZCancel)r5   rF   c   )r-   r.   rB   N)r
   framegridZgrid_columnconfigurer   r   r   r   Zentryvarr   r%   r   r   
error_fontentry_errorr   r   create_extrar   r#   Z	button_okr!   Zbutton_cancel)r'   Zok_textrH   Z
entrylabelr(   r(   r)   r$   Y   s<    
zQuery.create_widgetsc                 C   s   d S Nr(   r'   r(   r(   r)   rL   }       zQuery.create_extraNc                 C   s   d| |p| j d< d S )NzERROR: r5   )rK   )r'   r   Zwidgetr(   r(   r)   	showerror   s    zQuery.showerrorc                 C   s$   | j   }|s | d dS |S )zReturn non-blank entry or None.zblank line.N)r%   getstriprP   )r'   r%   r(   r(   r)   entry_ok   s
    
zQuery.entry_okc                 C   s8   d| j d< |  }|du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.
        r   r5   N)rK   rS   resultdestroyr%   r&   )r'   eventr%   r(   r(   r)   r#      s    

zQuery.okc                 C   s   d| _ |   dS )z0Set dialog result to None and destroy tk widget.N)rT   rU   )r'   rV   r(   r(   r)   r!      s    zQuery.cancelc                    s   |    t   d S rM   )Zgrab_releasesuperrU   rN   	__class__r(   r)   rU      s    zQuery.destroy)r*   )N)N)N)__name__
__module____qualname____doc__r   r$   rL   rP   rS   r#   r!   rU   __classcell__r(   r(   rX   r)   r   !   s   3
$


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   rW   r   )r'   r   r   r   r   r   r   rX   r(   r)   r      s    zSectionName.__init__c                 C   sV   | j   }|s | d dS t|dkr:| d dS || jv rR| d dS |S )z2Return sensible ConfigParser section name or None.no name specified.Nr6   z"name is longer than 30 characters.zname is already in use.)r%   rQ   rR   rP   lenr   )r'   r8   r(   r(   r)   rS      s    



zSectionName.entry_okrZ   r[   r\   r]   r   rS   r^   r(   r(   rX   r)   r_      s   r_   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.Fr`   c                   s   t  j||||||d d S )Nr   r   r   ra   )r'   r   r   r   r   r   r   rX   r(   r)   r      s    zModuleName.__init__c                 C   s  | j   }|s | d dS ztj|}W n8 ttfyh } z| t	| W Y d}~dS d}~0 0 |du r| d dS t
|jtjjs| d dS z|j|}W nb ty   | d Y dS  ty   z|j }W n" ty
   | d Y Y dS 0 Y n0 |S )z0Return entered module name as file path or None.rb   Nzmodule not found.znot a source-based module.z%loader does not support get_filename.zloader failed to get filename.)r%   rQ   rR   rP   	importlibutil	find_spec
ValueErrorImportErrorstr
isinstanceloaderabcSourceLoaderget_filenameAttributeError	TypeError)r'   r8   specmsgZ	file_pathr(   r(   r)   rS      s6    




zModuleName.entry_okrd   r(   r(   rX   r)   re      s   re   c                   @   s   e Zd ZdZdd ZdS )Gotoz1Get a positive line number for editor Go To Line.c                 C   sL   zt | j }W n ty0   | d Y d S 0 |dkrH| d d S |S )Nznot a base 10 integer.r   znot a positive integer.)intr%   rQ   rj   rP   )r'   linenor(   r(   r)   rS      s    

zGoto.entry_okN)rZ   r[   r\   r]   rS   r(   r(   r(   r)   rv      s   rv   c                       s\   e Zd ZdZddi ddd fdd
Z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 )zGet 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   N)r{   rW   r   )	r'   r   r   rz   r{   r   r   r   r   rX   r(   r)   r      s    zHelpSource.__init__c                 C   s   | 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 )zAdd path widjets to rows 10-12.r1   r2   z,Help File Path: Enter URL or browse for filer3   (   )r7   r   ZBrowse   )r5   r   rF   r;   r<   r=   r   r+   r>   r?   )r-   r.   rA   rB   rD   r/      r   rC   )r-   r.   rB   r/      r@   N)rH   r   r   r{   pathvarr   pathr   browse_filerJ   
path_errorrI   r   r   r	   )r'   rH   Z	pathlabelZbrowser(   r(   r)   rL     s,    zHelpSource.create_extrac                 C   s   t j| |dj||dS )N)r   	filetypes)Z
initialdirZinitialfile)r   ZOpenZshow)r'   r   ZinitdirZinitfiler(   r(   r)   askfilename  s    zHelpSource.askfilenamec                 C   s   g d}| j  }|r(tj|\}}nHd }td d dkrhtjtjtd}tj	|spt
 }nt
 }| |||}|r| j | d S )N))z
HTML Filesz*.htm *.htmlTEXT)z	PDF Filesz*.pdfr   )zWindows Help Filesz*.chm)z
Text Filesz*.txtr   )z	All Files*r>   winZDoc)r   rQ   osr   splitr   joindirnamer   isdirgetcwdr   set)r'   r   r   dirbasefiler(   r(   r)   r   !  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.httpr?   zfile:zhelp file path does not exist.darwinzfile://)	r   rQ   rR   rP   r   
startswithr   r9   r   )r'   r   r(   r(   r)   path_ok9  s    
zHelpSource.path_okc                 C   s6   d| j d< |  }|  }|du s*|du r.dS ||fS )z,Return apparently valid (name, path) or Noner   r5   N)r   item_okr   )r'   r8   r   r(   r(   r)   rS   J  s    
zHelpSource.entry_ok)rZ   r[   r\   r]   r   rL   r   r   r_   rS   r   r   r^   r(   r(   rX   r)   ry      s   ry   c                       sB   e Zd ZdZg ddd fdd
Zdd Zdd	 Zd
d Z  ZS )	CustomRunzGet settings for custom run of module.

    1. Command line arguments to extend sys.argv.
    2. Whether to restart Shell or not.
    F)cli_argsr   r   c                   s    d}t  j||||||d dS )zcli_args is a list of strings.

        The list is assigned to the default Entry StringVar.
        The strings are displayed joined by ' ' for display.
        z$Command Line Arguments for sys.argv:rf   Nra   )r'   r   r   r   r   r   r   rX   r(   r)   r   Y  s
    zCustomRun.__init__c                 C   sj   | 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 )zAdd run mode on rows 10-12.T)valueFzRestart shell)variableZonvalueZoffvaluer5   r;   r<   r=   r   r+   r>   r?   r1   r@   r   ZweN)rH   r   
restartvarr   r   rJ   Z
args_errorrI   )r'   rH   restartr(   r(   r)   rL   e  s    
zCustomRun.create_extrac              
   C   sZ   | j   }ztj|dd}W n4 tyT } z| t| W Y d}~dS d}~0 0 |S )z6Validity check and parsing for command line arguments.T)posixN)r%   rQ   rR   shlexr   rj   rP   rl   )r'   Z
cli_stringr   errr(   r(   r)   cli_args_okr  s    zCustomRun.cli_args_okc                 C   s&   |   }| j }|du rdS ||fS )z4Return apparently valid (cli_args, restart) or None.N)r   r   rQ   )r'   r   r   r(   r(   r)   rS   |  s    
zCustomRun.entry_ok)	rZ   r[   r\   r]   r   rL   r   rS   r^   r(   r(   rX   r)   r   Q  s   
r   __main__)mainzidlelib.idle_test.test_queryr   F)	verbosityexit)run)&r]   importlib.utilrg   importlib.abcr   r   sysr   r   tkinterr   r   r   r   r   r	   Ztkinter.ttkr
   r   r   r   r   r   Ztkinter.fontr   Ztkinter.simpledialogr   r   r_   re   rv   ry   r   rZ   Zunittestr   Zidlelib.idle_test.htestr   r(   r(   r(   r)   <module>   s*     *\2
