o
     JAf:                     @   s6  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rddl"m#Z# e#dddd ddl$m%Z% e%eee  dS 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   s:  || _ || _|| _|| _t| | |   | | | | |s&| 	  t
|  | jdkr6| d| j | d| j | d| j | d| j | d| j |   |   | d| | d |  d   | |s}| d |  d  nd	 f  | jd
d
d |s|   | j  |   dS 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.10/idlelib/query.pyr   &   sJ   



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   }   s    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striprO   )r'   r%   r(   r(   r)   entry_ok   s
   
zQuery.entry_okc                 C   s:   d| j d< |  }|dur|| _|   dS | 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   rR   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)rS   rT   )r'   rU   r(   r(   r)   r!      s   zQuery.cancelc                    s   |    t   d S rM   )Zgrab_releasesuperrT   rN   	__class__r(   r)   rT      s   zQuery.destroy)r*   rM   )__name__
__module____qualname____doc__r   r$   rL   rO   rR   r#   r!   rT   __classcell__r(   r(   rW   r)   r   !   s    
3$


r   c                       0   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                      t  j||||||d d S )N)r   r   r   rV   r   )r'   r   r   r   r   r   r   rW   r(   r)   r         
zSectionName.__init__c                 C   sV   | j   }|s| d dS t|dkr| d dS || jv r)| 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%   rP   rQ   rO   lenr   )r'   r8   r(   r(   r)   rR      s   



zSectionName.entry_okrY   rZ   r[   r\   r   rR   r]   r(   r(   rW   r)   r_      
    r_   c                       r^   )
ModuleNamez-Get a module name for Open Module menu entry.Fr`   c                   ra   )Nr   r   r   rb   )r'   r   r   r   r   r   r   rW   r(   r)   r      rc   zModuleName.__init__c                 C   s  | j   }|s| d dS ztj|}W n ttfy4 } z| t	| W Y d}~dS d}~ww |du r@| d dS t
|jtjjsO| d dS z	|j|}W |S  tyg   | d Y dS  ty   z	|j }W Y |S  ty   | d Y Y dS w w )z0Return entered module name as file path or None.rd   Nzmodule not found.znot a source-based module.z%loader does not support get_filename.zloader failed to get filename.)r%   rP   rQ   rO   	importlibutil	find_spec
ValueErrorImportErrorstr
isinstanceloaderabcSourceLoaderget_filenameAttributeError	TypeError)r'   r8   specmsgZ	file_pathr(   r(   r)   rR      s@   




zModuleName.entry_okrf   r(   r(   rW   r)   rh      rg   rh   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y   | d Y d S w |dkr$| d d S |S )Nznot a base 10 integer.r   znot a positive integer.)intr%   rP   rm   rO   )r'   linenor(   r(   r)   rR      s   

zGoto.entry_okN)rY   rZ   r[   r\   rR   r(   r(   r(   r)   ry      s    ry   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~   rV   r   )	r'   r   r   r}   r~   r   r   r   r   rW   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|\}}n$d }td d dkr4tjtjtd}tj	|s3t
 }nt
 }| |||}|rI| j | d S 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   rP   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s<|dd dkr%|dd }tj |s4| d| j dS tdkr<d| }|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   rP   rQ   rO   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)   rR   J  s   
zHelpSource.entry_ok)rY   rZ   r[   r\   r   rL   r   r   r_   rR   r   r   r]   r(   r(   rW   r)   r|      s    r|   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:ri   Nrb   )r'   r   r   r   r   r   r   rW   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   sX   | j   }z
tj|dd}W |S  ty+ } z| t| W Y d}~dS d}~ww )z6Validity check and parsing for command line arguments.T)posixN)r%   rP   rQ   shlexr   rm   rO   ro   )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   rP   )r'   r   r   r(   r(   r)   rR   |  s   
zCustomRun.entry_ok)	rY   rZ   r[   r\   r   rL   r   rR   r]   r(   r(   rW   r)   r   Q  s    
r   __main__)mainzidlelib.idle_test.test_queryr   F)	verbosityexit)run)&r\   importlib.utilrj   Z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_   rh   ry   r|   r   rY   Zunittestr   Zidlelib.idle_test.htestr   r(   r(   r(   r)   <module>   s.      *\2