a  ze:@s4dZddlZddlZddlZddlZddlmZmZddl m Z m Z m Z m Z mZmZddlmZmZmZmZmZddl mZddlmZddlmZGd d d e ZGd d d eZGd ddeZGdddeZGdddeZGdddeZ e!dkr0ddl"m#Z#e#ddddddl$m%Z%e%eee dS)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 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) executableplatform)Toplevel StringVar BooleanVarWES)FrameButtonEntryLabel Checkbutton) filedialog)Font) _setup_dialogcsleZdZdZdidddddZddd Zd d Zdd dZddZdddZ dddZ fddZ Z S)QueryzoBase class for getting verified answer from a user. For this base class, accept any non-blank string. Ftext0 used_names_htest_utestcCs.||_||_||_||_t||||||||sL| t ||j dkrl| d|j | d|j |d|j | d|j| d|j|||d||d|d||s|d|dnd f|jd d d |s*||d S) aCreate 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 Zaquaz z ZWM_DELETE_WINDOWz z z+%d+%dF)ZheightwidthN)parentmessagerrr__init__ZwithdrawtitleZ transientZgrab_setrZ_windowingsystembindcancelprotocolokcreate_widgetsZupdate_idletasksZgeometryZ winfo_rootxZ winfo_widthZwinfo_reqwidthZ winfo_rootyZ winfo_heightZwinfo_reqheightZ resizableZ deiconifyZ wait_window)selfrrrrrrrr&A/opt/bitninja-python-dojo/embedded/lib/python3.9/idlelib/query.pyr&sF    zQuery.__init__OKcCs6t|dd|_}|jdddd|jdddt|dd |jd }t||j|_t |d |jd |_ |j t d d|j d|_t|dd|jd|_|jddddtd|j jddddttddgd|jjddddttd|t||d|jd|_t|d|jd|_|jjdddd|jjdddddS)zCreate entry (rows, extras, buttons. Entry stuff on rows 0-2, spanning cols 0-2. Buttons on row 99, cols 1, 2. )Zpaddingrnews)columnrowsticky)ZweightwleftanchorZjustifytext)r textvariableZ TkCaptionFontT)nameexistsroot redr3Z foregroundZfontr+r, columnspanpadxr-r+r,r?r@r-padyrZactive)r3defaultcommandZCancel)r3rDc)r+r,r@N)r framegridZgrid_columnconfigurer rrrZentryvarr entry focus_setrr error_font entry_errorrr create_extrar r#Z button_okr!Z button_cancel)r%Zok_textrFZ entrylabelr&r&r'r$Xs>  zQuery.create_widgetscCsdSNr&r%r&r&r'rL}zQuery.create_extraNcCsd||p|jd<dS)NzERROR: r3)rK)r%rZwidgetr&r&r' showerrorszQuery.showerrorcCs$|j}|s |ddS|S)zReturn non-blank entry or None.z blank line.N)rHgetstriprP)r%rHr&r&r'entry_oks  zQuery.entry_okcCs8d|jd<|}|dur*||_|n |jdS)zIf entry is valid, bind it to 'result' and destroy tk widget. Otherwise leave dialog open for user to correct entry or cancel. rr3N)rKrSresultdestroyrHrI)r%eventrHr&r&r'r#s   zQuery.okcCsd|_|dS)z0Set dialog result to None and destroy tk widget.N)rTrU)r%rVr&r&r'r!sz Query.cancelcs|tdSrM)Z grab_releasesuperrUrN __class__r&r'rUsz Query.destroy)r()N)N)N) __name__ __module__ __qualname____doc__rr$rLrPrSr#r!rU __classcell__r&r&rXr'r!s 2 %   rcs0eZdZdZdddfdd ZddZZS) SectionNamez*Get a name for a config file section name.Frrcstj||||||ddS)N)rrrrWr)r%rrrrrrrXr&r'rszSectionName.__init__cCsV|j}|s |ddSt|dkr:|ddS||jvrR|ddS|S)z2Return sensible ConfigParser section name or None.no name specified.Nr4z"name is longer than 30 characters.zname is already in use.)rHrQrRrPlenr)r%r6r&r&r'rSs     zSectionName.entry_okrZr[r\r]rrSr^r&r&rXr'r_sr_cs0eZdZdZdddfdd ZddZZS) ModuleNamez-Get a module name for Open Module menu entry.Fr`cstj||||||ddS)Nrrrra)r%rrrrrrrXr&r'rszModuleName.__init__c Cs|j}|s |ddSztj|}Wn8ttfyh}z|t |WYd}~dSd}~00|dur|ddSt |j tj j s|ddSz|j |}Wnbty|dYdStyz|j }Wn"ty |dYYdS0Yn0|S)z0Return entered module name as file path or None.rbNzmodule not found.znot a source-based module.z%loader does not support get_filename.zloader failed to get filename.)rHrQrRrP importlibutil find_spec ValueError ImportErrorstr isinstanceloaderabc SourceLoader get_filenameAttributeError TypeError)r%r6specmsgZ file_pathr&r&r'rSs6      zModuleName.entry_okrdr&r&rXr'resrec@seZdZdZddZdS)Gotoz1Get a positive line number for editor Go To Line.cCsLzt|j}Wnty0|dYdS0|dkrH|ddS|S)Nznot a base 10 integer.rznot a positive integer.)intrHrQrjrP)r%linenor&r&r'rSs   z Goto.entry_okN)rZr[r\r]rSr&r&r&r'rvsrvcs\eZdZdZddidddfdd ZddZd d Zd d Zej Z d dZ ddZ Z S) HelpSourcez,Get menu name and help source for Help menu.rF)menuitemfilepathrrrc s(||_d}tj|||||||ddS)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. zName for item on Help menu:rN)r{rWr) r%rrrzr{rrrrrXr&r'rs zHelpSource.__init__cCs|j}t|dddd}t||j|_t||jdd|_t|dd|jd }t|d d |j d |_ |j d ddddd gt d|jj d dddt t dd gd|j dddt td|j j d dddt t ddS)zAdd path widjets to rows 10-12.r/r0z,Help File Path: Enter URL or browse for filer1()r5rZBrowse)r3rrDr9r:r;rr)r<r=)r+r,r?r@rBr- rrA)r+r,r@r- r>N)rFr rr{pathvarr pathr browse_filerJ path_errorrGrrr )r%rFZ pathlabelZbrowser&r&r'rLs,zHelpSource.create_extracCstj||dj||dS)N)r filetypes)Z initialdirZ initialfile)rZOpenZshow)r%rZinitdirZinitfiler&r&r' askfilenameszHelpSource.askfilenamecCsgd}|j}|r(tj|\}}nHd}tdddkrhtjtjtd}tj |spt }nt }| |||}|r|j |dS)N))z HTML Filesz *.htm *.htmlTEXT)z PDF Filesz*.pdfr)zWindows Help Filesz*.chm)z Text Filesz*.txtr)z All Files*r<winZDoc) rrQosrsplitrjoindirnamerisdirgetcwdrset)r%rrdirbasefiler&r&r'r!s   zHelpSource.browse_filecCs||j}|s$|d|jdS|dsx|dddkrJ|dd}tj|sh|d|jdStdkrxd|}|S) z(Simple validity check for menu file pathzno help file path specified.N)zwww.httpr=zfile:zhelp file path does not exist.darwinzfile://) rrQrRrPr startswithrr7r)r%rr&r&r'path_ok9s   zHelpSource.path_okcCs6d|jd<|}|}|dus*|dur.dS||fS)z,Return apparently valid (name, path) or Nonerr3N)ritem_okr)r%r6rr&r&r'rSJs zHelpSource.entry_ok) rZr[r\r]rrLrrr_rSrrr^r&r&rXr'rys rycsBeZdZdZgdddfdd ZddZdd Zd d ZZS) CustomRunzGet settings for custom run of module. 1. Command line arguments to extend sys.argv. 2. Whether to restart Shell or not. F)cli_argsrrcs d}tj||||||ddS)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:rfNra)r%rrrrrrrXr&r'rYs zCustomRun.__init__cCsj|j}t|dd|_t||jdddd}t|dd|jd|_|jd d d d d d|jjd dd d dddS)zAdd run mode on rows 10-12.T)valueFz Restart shell)variableZonvalueZoffvaluer3r9r:r;rr)r<r=r/r>rZweN)rFr restartvarrr rJZ args_errorrG)r%rFrestartr&r&r'rLes zCustomRun.create_extrac CsZ|j}ztj|dd}Wn4tyT}z|t|WYd}~dSd}~00|S)z6Validity check and parsing for command line arguments.T)posixN)rHrQrRshlexrrjrPrl)r%Z cli_stringrerrr&r&r' cli_args_okrszCustomRun.cli_args_okcCs&|}|j}|durdS||fS)z4Return apparently valid (cli_args, restart) or None.N)rrrQ)r%rrr&r&r'rS|s zCustomRun.entry_ok) rZr[r\r]rrLrrSr^r&r&rXr'rQs   r__main__)mainzidlelib.idle_test.test_queryrF) verbosityexit)run)&r]importlib.utilrg importlib.abcrrsysrrtkinterrrrrrr Z tkinter.ttkr r r r rrZ tkinter.fontrZtkinter.simpledialogrrr_rervryrrZZunittestrZidlelib.idle_test.htestrr&r&r&r's*    *\2