3 ^3@sdZddlZddlZddlmZddljZddl m Z ddl m Z ddl mZddlmZdd d ZGd d d ejZGdddZejedkrddlmZedddddS)a An IDLE extension to avoid having very long texts printed in the shell. A common problem in IDLE's interactive shell is printing of large amounts of text into the shell. This makes looking at the previous history difficult. Worse, this can cause IDLE to become very slow, even to the point of being completely unusable. This extension will automatically replace long texts with a small button. Double-cliking this button will remove it and insert the original text instead. Middle-clicking will copy the text to the clipboard. Right-clicking will open the text in a separate viewing window. Additionally, any output can be manually "squeezed" by the user. This includes output written to the standard error stream ("stderr"), such as exception messages and their tracebacks. N)Font)idleConf) view_text)Hovertip)macosxPc Csd}d}d}xtjd|D]}|j|}||7}||7}||dkrT|d7}d}n4||dksdt||||7}||kr|d7}|}|d7}|dkrt|d|\}} ||7}| d}qW|t||7}|dkr||d|7}n|d8}|S)zCount the number of lines in a given string. Lines are counted as if the string was wrapped so that lines are never over linewidth characters long. Tabs are considered tabwidth characters long. rz[\t\n]  )refinditerstartAssertionErrordivmodlen) s linewidthtabwidthposZ linecountZcurrent_columnmnumcharslinescolumnr(/usr/lib64/python3.6/idlelib/squeezer.pycount_lines_with_wrappings2   rc@sJeZdZdZddZddZdddZdd d Zdd d ZdZ ddZ dS)ExpandingButtonaClass for the "squeezed" text buttons used by Squeezer These buttons are displayed inside a Tk Text widget in place of text. A user can then use the button to replace it with the original text, copy the original text to the clipboard or view the original text in a separate window. Each button is tied to a Squeezer instance, and it knows to update the Squeezer instance when it is expanded (and therefore removed). c s|_||_||_||_|j|_}|j|_}|jj|_d|j}t j j |||dddd}t ||dd|j d|jtjr|j d |jn|j d |j|jfd d d|_|j|jdS) NzSqueezed text (%d lines).z#FFFFC0z#FFFFE0)textZ backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r)Z hover_delayzz z cst|t|t|S)N)int)offsetZlength)rrr}sz*ExpandingButton.__init__..)rtags numoflinessqueezereditwinrperbottom base_texttkButton__init__rZbindexpandrZisAquaTkcontext_menu_eventZselection_handle is_dangerousZ after_idleset_is_dangerous) selfrr"r#r$r%rZ button_textZbutton_tooltip_textr)rrr+as(      zExpandingButton.__init__csLd|jj|jdkpDt|jdkpDtfddtjd|jD|_dS)N2iiPc3s |]}t|jdkVqdS)rN)rgroup).0Z line_match)dangerous_line_lenrr sz3ExpandingButton.set_is_dangerous..z[^\n]+) r winfo_widthr#rranyr r r.)r0r)r4rr/s   z ExpandingButton.set_is_dangerousNcCs|jdkr|j|jrTtjddjddddg|jt|jftj|j d}|sTd S|j j |j j ||j|j |j j||jjj|dS) aAexpand event handler This inserts the original text in place of the button in the Text widget, removes the button and updates the Squeezer instance. If the original text is dangerously long, i.e. expanding it could cause a performance degradation, ask the user for confirmation. NzExpand huge output?z z5The squeezed output is very long: %d lines, %d chars.z2Expanding it could make IDLE slow or unresponsive.z5It is recommended to view or copy the output instead.zReally expand?)titlemessagedefaultparentbreak)r.r/ tkMessageBoxZ askokcanceljoinr#rrZCANCELrr(insertindexr"deleter$expandingbuttonsremove)r0eventZconfirmrrrr,s$   zExpandingButton.expandcCs|j|j|jdS)zMcopy event handler Copy the original text to the clipboard. N)Zclipboard_clearZclipboard_appendr)r0rDrrrcopyszExpandingButton.copycCst|jd|jddddS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFZnone)ZmodalZwrapN)rrr)r0rDrrrviews zExpandingButton.viewrErFcCsf|jjdd|j|jftj|jdd}x&|jD]\}}|j|t||dq2W|j |j |j dS)Nr?z@%d,%dr)Ztearoff)labelZcommandr<) rZmark_setxyr)ZMenu rmenu_specsZ add_commandgetattrZtk_popupZx_rootZy_root)r0rDZrmenurGZ method_namerrrr-s z"ExpandingButton.context_menu_event)N)N)NrErErFrF)rLrM) __name__ __module__ __qualname____doc__r+r/r,rErFrJr-rrrrrVs !   rc@s4eZdZdZeddZddZddZdd Zd S) SqueezerzReplace long outputs in the shell with a simple button. This avoids IDLE's shell slowing down considerably, and even becoming completely unresponsive, when very long outputs are written. cCstjdddddd|_dS)z!Load class variables from config.mainPyShellzauto-squeeze-min-linesrr1)typer:N)rZ GetOptionauto_squeeze_min_lines)clsrrrreloadszSqueezer.reloadcsX|_|j_|jj_g_ddlm}t||rTf|j ffdd }||_ dS)aeInitialize settings for Squeezer. editwin is the shell's Editor window. self.text is the editor window text widget. self.base_test is the actual editor window Tk text widget, rather than EditorWindow's wrapper. self.expandingbuttons is the list of all buttons representing "squeezed" output. r)rTcs|dkr|||Sj|}|jkr0|||St|||}jdtjjd|dddjdjjdtj j j |dS)NstdoutZiomark)windowpadxpady) count_linesrVrZ mark_gravityr)ZRIGHT window_createZseeupdateZLEFTrBappend)rr"writer#expandingbutton)r0rrrmywrites      z"Squeezer.__init__..mywriteN) r%rr&r'r(rBZidlelib.pyshellrT isinstancerc)r0r%rTrer)r0rrr+s     zSqueezer.__init__cshjj}jjj}|dtfddd D8}tjjjjjdd}||jd}t|||S) abCount the number of lines in a given text. Before calculation, the tab width and line length of the text are fetched, so that up-to-date values are used. Lines are counted as if the string was wrapped so that lines are never over linewidth characters long. Tabs are considered tabwidth characters long. csg|]}tjjj|qSr)rr%rcget)r3opt)r0rr sz(Squeezer.count_lines..borderr]font)name0)rkr]) r%Zget_tk_tabwidthrr6sumrrhZmeasurer)r0rrrrlr)r0rr_ s  zSqueezer.count_linesc Cs|jjtj}x$dD]}||krPqW|jjdS|jj|tjd\}}|jj||}t|dkr|ddkr|jjd|}|d d}|j j |||j |}t ||||}|jj ||d d d t|j} x0| dko|jj|j| dd |r| d8} qW|jj| |dS)zsqueeze-current-text event handler Squeeze the block of text inside which contains the "insert" cursor. If the insert cursor is not in a squeezable block of text, give the user a small warning and do nothing. rYstderrr<z+1crr r z%s-1cNrZr[)r\r]r^>)rYrprr)r tag_namesr)ZINSERTZbellZ tag_prevrangegetrr@r(rAr_rr`rBZcomparer?) r0rDrsZtag_namerendrr#rdirrrsqueeze_current_text_event*s,          z#Squeezer.squeeze_current_text_eventN) rNrOrPrQ classmethodrXr+r_rwrrrrrRs  4rR__main__)rSzidlelib.idle_test.test_squeezerrgF) verbosityexit)rr)rQr Ztkinterr)Z tkinter.fontrZtkinter.messageboxZ messageboxr=Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrr*rrRrXrNZunittestrSrrrrs        9s