GRAYBYTE WORDPRESS FILE MANAGER5547

Server IP : 198.54.121.189 / Your IP : 216.73.216.140
System : Linux premium69.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
PHP Version : 7.4.33
Disable Function : NONE
cURL : ON | WGET : ON | Sudo : OFF | Pkexec : OFF
Directory : /opt/alt/python36/lib64/python3.6/idlelib/
Upload Files :
Current_dir [ Not Writeable ] Document_root [ Writeable ]

Command :


Current File : /opt/alt/python36/lib64/python3.6/idlelib//squeezer.py
"""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.
"""
import re

import tkinter as tk
from tkinter.font import Font
import tkinter.messagebox as tkMessageBox

from idlelib.config import idleConf
from idlelib.textview import view_text
from idlelib.tooltip import Hovertip
from idlelib import macosx


def count_lines_with_wrapping(s, linewidth=80, tabwidth=8):
    """Count 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.
    """
    pos = 0
    linecount = 1
    current_column = 0

    for m in re.finditer(r"[\t\n]", s):
        # process the normal chars up to tab or newline
        numchars = m.start() - pos
        pos += numchars
        current_column += numchars

        # deal with tab or newline
        if s[pos] == '\n':
            linecount += 1
            current_column = 0
        else:
            assert s[pos] == '\t'
            current_column += tabwidth - (current_column % tabwidth)

            # if a tab passes the end of the line, consider the entire tab as
            # being on the next line
            if current_column > linewidth:
                linecount += 1
                current_column = tabwidth

        pos += 1 # after the tab or newline

        # avoid divmod(-1, linewidth)
        if current_column > 0:
            # If the length was exactly linewidth, divmod would give (1,0),
            # even though a new line hadn't yet been started. The same is true
            # if length is any exact multiple of linewidth. Therefore, subtract
            # 1 before doing divmod, and later add 1 to the column to
            # compensate.
            lines, column = divmod(current_column - 1, linewidth)
            linecount += lines
            current_column = column + 1

    # process remaining chars (no more tabs or newlines)
    current_column += len(s) - pos
    # avoid divmod(-1, linewidth)
    if current_column > 0:
        linecount += (current_column - 1) // linewidth
    else:
        # the text ended with a newline; don't count an extra line after it
        linecount -= 1

    return linecount


class ExpandingButton(tk.Button):
    """Class 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).
    """
    def __init__(self, s, tags, numoflines, squeezer):
        self.s = s
        self.tags = tags
        self.numoflines = numoflines
        self.squeezer = squeezer
        self.editwin = editwin = squeezer.editwin
        self.text = text = editwin.text

        # the base Text widget of the PyShell object, used to change text
        # before the iomark
        self.base_text = editwin.per.bottom

        button_text = "Squeezed text (%d lines)." % self.numoflines
        tk.Button.__init__(self, text, text=button_text,
                           background="#FFFFC0", activebackground="#FFFFE0")

        button_tooltip_text = (
            "Double-click to expand, right-click for more options."
        )
        Hovertip(self, button_tooltip_text, hover_delay=80)

        self.bind("<Double-Button-1>", self.expand)
        if macosx.isAquaTk():
            # AquaTk defines <2> as the right button, not <3>.
            self.bind("<Button-2>", self.context_menu_event)
        else:
            self.bind("<Button-3>", self.context_menu_event)
        self.selection_handle(
            lambda offset, length: s[int(offset):int(offset) + int(length)])

        self.is_dangerous = None
        self.after_idle(self.set_is_dangerous)

    def set_is_dangerous(self):
        dangerous_line_len = 50 * self.text.winfo_width()
        self.is_dangerous = (
            self.numoflines > 1000 or
            len(self.s) > 50000 or
            any(
                len(line_match.group(0)) >= dangerous_line_len
                for line_match in re.finditer(r'[^\n]+', self.s)
            )
        )

    def expand(self, event=None):
        """expand 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.
        """
        if self.is_dangerous is None:
            self.set_is_dangerous()
        if self.is_dangerous:
            confirm = tkMessageBox.askokcancel(
                title="Expand huge output?",
                message="\n\n".join([
                    "The squeezed output is very long: %d lines, %d chars.",
                    "Expanding it could make IDLE slow or unresponsive.",
                    "It is recommended to view or copy the output instead.",
                    "Really expand?"
                ]) % (self.numoflines, len(self.s)),
                default=tkMessageBox.CANCEL,
                parent=self.text)
            if not confirm:
                return "break"

        self.base_text.insert(self.text.index(self), self.s, self.tags)
        self.base_text.delete(self)
        self.squeezer.expandingbuttons.remove(self)

    def copy(self, event=None):
        """copy event handler

        Copy the original text to the clipboard.
        """
        self.clipboard_clear()
        self.clipboard_append(self.s)

    def view(self, event=None):
        """view event handler

        View the original text in a separate text viewer window.
        """
        view_text(self.text, "Squeezed Output Viewer", self.s,
                  modal=False, wrap='none')

    rmenu_specs = (
        # item structure: (label, method_name)
        ('copy', 'copy'),
        ('view', 'view'),
    )

    def context_menu_event(self, event):
        self.text.mark_set("insert", "@%d,%d" % (event.x, event.y))
        rmenu = tk.Menu(self.text, tearoff=0)
        for label, method_name in self.rmenu_specs:
            rmenu.add_command(label=label, command=getattr(self, method_name))
        rmenu.tk_popup(event.x_root, event.y_root)
        return "break"


class Squeezer:
    """Replace 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.
    """
    @classmethod
    def reload(cls):
        """Load class variables from config."""
        cls.auto_squeeze_min_lines = idleConf.GetOption(
            "main", "PyShell", "auto-squeeze-min-lines",
            type="int", default=50,
        )

    def __init__(self, editwin):
        """Initialize 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.
        """
        self.editwin = editwin
        self.text = text = editwin.text

        # Get the base Text widget of the PyShell object, used to change text
        # before the iomark. PyShell deliberately disables changing text before
        # the iomark via its 'text' attribute, which is actually a wrapper for
        # the actual Text widget. Squeezer, however, needs to make such changes.
        self.base_text = editwin.per.bottom

        self.expandingbuttons = []
        from idlelib.pyshell import PyShell  # done here to avoid import cycle
        if isinstance(editwin, PyShell):
            # If we get a PyShell instance, replace its write method with a
            # wrapper, which inserts an ExpandingButton instead of a long text.
            def mywrite(s, tags=(), write=editwin.write):
                # only auto-squeeze text which has just the "stdout" tag
                if tags != "stdout":
                    return write(s, tags)

                # only auto-squeeze text with at least the minimum
                # configured number of lines
                numoflines = self.count_lines(s)
                if numoflines < self.auto_squeeze_min_lines:
                    return write(s, tags)

                # create an ExpandingButton instance
                expandingbutton = ExpandingButton(s, tags, numoflines,
                                                  self)

                # insert the ExpandingButton into the Text widget
                text.mark_gravity("iomark", tk.RIGHT)
                text.window_create("iomark", window=expandingbutton,
                                   padx=3, pady=5)
                text.see("iomark")
                text.update()
                text.mark_gravity("iomark", tk.LEFT)

                # add the ExpandingButton to the Squeezer's list
                self.expandingbuttons.append(expandingbutton)

            editwin.write = mywrite

    def count_lines(self, s):
        """Count 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.
        """
        # Tab width is configurable
        tabwidth = self.editwin.get_tk_tabwidth()

        # Get the Text widget's size
        linewidth = self.editwin.text.winfo_width()
        # Deduct the border and padding
        linewidth -= 2*sum([int(self.editwin.text.cget(opt))
                            for opt in ('border', 'padx')])

        # Get the Text widget's font
        font = Font(self.editwin.text, name=self.editwin.text.cget('font'))
        # Divide the size of the Text widget by the font's width.
        # According to Tk8.5 docs, the Text widget's width is set
        # according to the width of its font's '0' (zero) character,
        # so we will use this as an approximation.
        # see: http://www.tcl.tk/man/tcl8.5/TkCmd/text.htm#M-width
        linewidth //= font.measure('0')

        return count_lines_with_wrapping(s, linewidth, tabwidth)

    def squeeze_current_text_event(self, event):
        """squeeze-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.
        """
        # set tag_name to the first valid tag found on the "insert" cursor
        tag_names = self.text.tag_names(tk.INSERT)
        for tag_name in ("stdout", "stderr"):
            if tag_name in tag_names:
                break
        else:
            # the insert cursor doesn't have a "stdout" or "stderr" tag
            self.text.bell()
            return "break"

        # find the range to squeeze
        start, end = self.text.tag_prevrange(tag_name, tk.INSERT + "+1c")
        s = self.text.get(start, end)

        # if the last char is a newline, remove it from the range
        if len(s) > 0 and s[-1] == '\n':
            end = self.text.index("%s-1c" % end)
            s = s[:-1]

        # delete the text
        self.base_text.delete(start, end)

        # prepare an ExpandingButton
        numoflines = self.count_lines(s)
        expandingbutton = ExpandingButton(s, tag_name, numoflines, self)

        # insert the ExpandingButton to the Text
        self.text.window_create(start, window=expandingbutton,
                                padx=3, pady=5)

        # insert the ExpandingButton to the list of ExpandingButtons, while
        # keeping the list ordered according to the position of the buttons in
        # the Text widget
        i = len(self.expandingbuttons)
        while i > 0 and self.text.compare(self.expandingbuttons[i-1],
                                          ">", expandingbutton):
            i -= 1
        self.expandingbuttons.insert(i, expandingbutton)

        return "break"


Squeezer.reload()


if __name__ == "__main__":
    from unittest import main
    main('idlelib.idle_test.test_squeezer', verbosity=2, exit=False)

    # Add htest.

[ Back ]
Name
Size
Last Modified
Owner / Group
Permissions
Options
..
--
May 20 2024 08:31:55
root / linksafe
0755
Icons
--
May 20 2024 08:31:54
root / linksafe
0755
__pycache__
--
May 20 2024 08:31:54
root / linksafe
0755
idle_test
--
May 20 2024 08:31:55
root / linksafe
0755
CREDITS.txt
1.822 KB
September 04 2021 03:49:41
root / linksafe
0644
ChangeLog
55.039 KB
September 04 2021 03:49:41
root / linksafe
0644
HISTORY.txt
10.07 KB
September 04 2021 03:49:41
root / linksafe
0644
NEWS.txt
38.905 KB
September 04 2021 03:49:41
root / linksafe
0644
NEWS2x.txt
26.535 KB
September 04 2021 03:49:41
root / linksafe
0644
README.txt
9.367 KB
September 04 2021 03:49:41
root / linksafe
0644
TODO.txt
8.279 KB
September 04 2021 03:49:41
root / linksafe
0644
__init__.py
0.387 KB
April 17 2024 17:19:35
root / linksafe
0644
__main__.py
0.155 KB
April 17 2024 17:19:35
root / linksafe
0644
_pyclbr.py
14.843 KB
April 17 2024 17:19:35
root / linksafe
0644
autocomplete.py
9.105 KB
April 17 2024 17:19:35
root / linksafe
0644
autocomplete_w.py
19.364 KB
April 17 2024 17:19:35
root / linksafe
0644
autoexpand.py
3.141 KB
April 17 2024 17:19:35
root / linksafe
0644
browser.py
8.087 KB
April 17 2024 17:19:35
root / linksafe
0644
calltip.py
5.925 KB
April 17 2024 17:19:35
root / linksafe
0644
calltip_w.py
6.944 KB
April 17 2024 17:19:35
root / linksafe
0644
codecontext.py
10.244 KB
April 17 2024 17:19:35
root / linksafe
0644
colorizer.py
11.011 KB
April 17 2024 17:19:35
root / linksafe
0644
config-extensions.def
2.213 KB
September 04 2021 03:49:41
root / linksafe
0644
config-highlight.def
2.624 KB
September 04 2021 03:49:41
root / linksafe
0644
config-keys.def
10.522 KB
September 04 2021 03:49:41
root / linksafe
0644
config-main.def
3.055 KB
September 04 2021 03:49:41
root / linksafe
0644
config.py
37.967 KB
April 17 2024 17:19:35
root / linksafe
0644
config_key.py
13.094 KB
April 17 2024 17:19:35
root / linksafe
0644
configdialog.py
98.688 KB
April 17 2024 17:19:35
root / linksafe
0644
debugger.py
18.649 KB
April 17 2024 17:19:35
root / linksafe
0644
debugger_r.py
11.855 KB
April 17 2024 17:19:35
root / linksafe
0644
debugobj.py
3.96 KB
April 17 2024 17:19:35
root / linksafe
0644
debugobj_r.py
1.057 KB
April 17 2024 17:19:35
root / linksafe
0644
delegator.py
1.019 KB
April 17 2024 17:19:35
root / linksafe
0644
dynoption.py
1.97 KB
April 17 2024 17:19:35
root / linksafe
0644
editor.py
65.698 KB
April 17 2024 17:19:35
root / linksafe
0644
extend.txt
3.557 KB
September 04 2021 03:49:41
root / linksafe
0644
filelist.py
3.805 KB
April 17 2024 17:19:35
root / linksafe
0644
grep.py
6.584 KB
April 17 2024 17:19:35
root / linksafe
0644
help.html
53.822 KB
September 04 2021 03:49:41
root / linksafe
0644
help.py
11.06 KB
April 17 2024 17:19:35
root / linksafe
0644
help_about.py
8.771 KB
April 17 2024 17:19:35
root / linksafe
0644
history.py
3.948 KB
April 17 2024 17:19:35
root / linksafe
0644
hyperparser.py
12.58 KB
April 17 2024 17:19:35
root / linksafe
0644
idle.py
0.443 KB
April 17 2024 17:19:35
root / linksafe
0644
idle.pyw
0.557 KB
September 04 2021 03:49:41
root / linksafe
0644
iomenu.py
20.248 KB
April 17 2024 17:19:35
root / linksafe
0644
macosx.py
9.434 KB
April 17 2024 17:19:35
root / linksafe
0644
mainmenu.py
3.616 KB
April 17 2024 17:19:35
root / linksafe
0644
multicall.py
18.211 KB
April 17 2024 17:19:35
root / linksafe
0644
outwin.py
5.672 KB
April 17 2024 17:19:35
root / linksafe
0644
paragraph.py
6.999 KB
April 17 2024 17:19:35
root / linksafe
0644
parenmatch.py
7.035 KB
April 17 2024 17:19:35
root / linksafe
0644
pathbrowser.py
3.118 KB
April 17 2024 17:19:35
root / linksafe
0644
percolator.py
3.057 KB
April 17 2024 17:19:35
root / linksafe
0644
pyparse.py
19.646 KB
April 17 2024 17:19:35
root / linksafe
0644
pyshell.py
56.375 KB
April 17 2024 17:19:35
root / linksafe
0755
query.py
12.143 KB
April 17 2024 17:19:35
root / linksafe
0644
redirector.py
6.714 KB
April 17 2024 17:19:35
root / linksafe
0644
replace.py
7.326 KB
April 17 2024 17:19:35
root / linksafe
0644
rpc.py
20.642 KB
April 17 2024 17:19:35
root / linksafe
0644
rstrip.py
0.848 KB
April 17 2024 17:19:35
root / linksafe
0644
run.py
16.867 KB
April 17 2024 17:19:35
root / linksafe
0644
runscript.py
7.657 KB
April 17 2024 17:19:35
root / linksafe
0644
scrolledlist.py
4.354 KB
April 17 2024 17:19:35
root / linksafe
0644
search.py
3.09 KB
April 17 2024 17:19:35
root / linksafe
0644
searchbase.py
7.276 KB
April 17 2024 17:19:35
root / linksafe
0644
searchengine.py
7.297 KB
April 17 2024 17:19:35
root / linksafe
0644
squeezer.py
12.996 KB
April 17 2024 17:19:35
root / linksafe
0644
stackviewer.py
4.35 KB
April 17 2024 17:19:35
root / linksafe
0644
statusbar.py
1.407 KB
April 17 2024 17:19:35
root / linksafe
0644
textview.py
5.978 KB
April 17 2024 17:19:35
root / linksafe
0644
tooltip.py
6.334 KB
April 17 2024 17:19:35
root / linksafe
0644
tree.py
14.735 KB
April 17 2024 17:19:35
root / linksafe
0644
undo.py
10.788 KB
April 17 2024 17:19:35
root / linksafe
0644
window.py
2.527 KB
April 17 2024 17:19:35
root / linksafe
0644
zoomheight.py
1.309 KB
April 17 2024 17:19:35
root / linksafe
0644
zzdummy.py
0.938 KB
April 17 2024 17:19:35
root / linksafe
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025
CONTACT ME
Static GIF