404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@18.219.119.163: ~ $
"""Pop up a reminder of how to call a function.

Call Tips are floating windows which display function, class, and method
parameter and docstring information when you type an opening parenthesis, and
which disappear when you type a closing parenthesis.
"""
import inspect
import re
import sys
import textwrap
import types

from idlelib import calltip_w
from idlelib.hyperparser import HyperParser
import __main__


class Calltip:

    def __init__(self, editwin=None):
        if editwin is None:  # subprocess and test
            self.editwin = None
        else:
            self.editwin = editwin
            self.text = editwin.text
            self.active_calltip = None
            self._calltip_window = self._make_tk_calltip_window

    def close(self):
        self._calltip_window = None

    def _make_tk_calltip_window(self):
        # See __init__ for usage
        return calltip_w.CalltipWindow(self.text)

    def _remove_calltip_window(self, event=None):
        if self.active_calltip:
            self.active_calltip.hidetip()
            self.active_calltip = None

    def force_open_calltip_event(self, event):
        "The user selected the menu entry or hotkey, open the tip."
        self.open_calltip(True)
        return "break"

    def try_open_calltip_event(self, event):
        """Happens when it would be nice to open a calltip, but not really
        necessary, for example after an opening bracket, so function calls
        won't be made.
        """
        self.open_calltip(False)

    def refresh_calltip_event(self, event):
        if self.active_calltip and self.active_calltip.tipwindow:
            self.open_calltip(False)

    def open_calltip(self, evalfuncs):
        self._remove_calltip_window()

        hp = HyperParser(self.editwin, "insert")
        sur_paren = hp.get_surrounding_brackets('(')
        if not sur_paren:
            return
        hp.set_index(sur_paren[0])
        expression  = hp.get_expression()
        if not expression:
            return
        if not evalfuncs and (expression.find('(') != -1):
            return
        argspec = self.fetch_tip(expression)
        if not argspec:
            return
        self.active_calltip = self._calltip_window()
        self.active_calltip.showtip(argspec, sur_paren[0], sur_paren[1])

    def fetch_tip(self, expression):
        """Return the argument list and docstring of a function or class.

        If there is a Python subprocess, get the calltip there.  Otherwise,
        either this fetch_tip() is running in the subprocess or it was
        called in an IDLE running without the subprocess.

        The subprocess environment is that of the most recently run script.  If
        two unrelated modules are being edited some calltips in the current
        module may be inoperative if the module was not the last to run.

        To find methods, fetch_tip must be fed a fully qualified name.

        """
        try:
            rpcclt = self.editwin.flist.pyshell.interp.rpcclt
        except AttributeError:
            rpcclt = None
        if rpcclt:
            return rpcclt.remotecall("exec", "get_the_calltip",
                                     (expression,), {})
        else:
            return get_argspec(get_entity(expression))


def get_entity(expression):
    """Return the object corresponding to expression evaluated
    in a namespace spanning sys.modules and __main.dict__.
    """
    if expression:
        namespace = sys.modules.copy()
        namespace.update(__main__.__dict__)
        try:
            return eval(expression, namespace)
        except BaseException:
            # An uncaught exception closes idle, and eval can raise any
            # exception, especially if user classes are involved.
            return None

# The following are used in get_argspec and some in tests
_MAX_COLS = 85
_MAX_LINES = 5  # enough for bytes
_INDENT = ' '*4  # for wrapped signatures
_first_param = re.compile(r'(?<=\()\w*\,?\s*')
_default_callable_argspec = "See source or doc"
_invalid_method = "invalid method signature"
_argument_positional = "\n['/' marks preceding arguments as positional-only]\n"

def get_argspec(ob):
    '''Return a string describing the signature of a callable object, or ''.

    For Python-coded functions and methods, the first line is introspected.
    Delete 'self' parameter for classes (.__init__) and bound methods.
    The next lines are the first lines of the doc string up to the first
    empty line or _MAX_LINES.    For builtins, this typically includes
    the arguments in addition to the return value.
    '''
    argspec = default = ""
    try:
        ob_call = ob.__call__
    except BaseException:
        return default

    fob = ob_call if isinstance(ob_call, types.MethodType) else ob

    try:
        argspec = str(inspect.signature(fob))
    except ValueError as err:
        msg = str(err)
        if msg.startswith(_invalid_method):
            return _invalid_method

    if '/' in argspec:
        """Using AC's positional argument should add the explain"""
        argspec += _argument_positional
    if isinstance(fob, type) and argspec == '()':
        """fob with no argument, use default callable argspec"""
        argspec = _default_callable_argspec

    lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
             if len(argspec) > _MAX_COLS else [argspec] if argspec else [])

    if isinstance(ob_call, types.MethodType):
        doc = ob_call.__doc__
    else:
        doc = getattr(ob, "__doc__", "")
    if doc:
        for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]:
            line = line.strip()
            if not line:
                break
            if len(line) > _MAX_COLS:
                line = line[: _MAX_COLS - 3] + '...'
            lines.append(line)
        argspec = '\n'.join(lines)
    if not argspec:
        argspec = _default_callable_argspec
    return argspec


if __name__ == '__main__':
    from unittest import main
    main('idlelib.idle_test.test_calltip', verbosity=2)

Filemanager

Name Type Size Permission Actions
Icons Folder 0755
__pycache__ Folder 0755
idle_test Folder 0755
CREDITS.txt File 1.82 KB 0644
ChangeLog File 55.04 KB 0644
HISTORY.txt File 10.07 KB 0644
NEWS.txt File 38.91 KB 0644
NEWS2x.txt File 26.54 KB 0644
README.txt File 9.37 KB 0644
TODO.txt File 8.28 KB 0644
__init__.py File 396 B 0644
__main__.py File 159 B 0644
_pyclbr.py File 14.84 KB 0644
autocomplete.py File 9.11 KB 0644
autocomplete_w.py File 19.36 KB 0644
autoexpand.py File 3.14 KB 0644
browser.py File 8.09 KB 0644
calltip.py File 5.92 KB 0644
calltip_w.py File 6.94 KB 0644
codecontext.py File 10.24 KB 0644
colorizer.py File 11.01 KB 0644
config-extensions.def File 2.21 KB 0644
config-highlight.def File 2.62 KB 0644
config-keys.def File 10.52 KB 0644
config-main.def File 3.05 KB 0644
config.py File 37.97 KB 0644
config_key.py File 13.09 KB 0644
configdialog.py File 98.69 KB 0644
debugger.py File 18.65 KB 0644
debugger_r.py File 11.86 KB 0644
debugobj.py File 3.96 KB 0644
debugobj_r.py File 1.06 KB 0644
delegator.py File 1.02 KB 0644
dynoption.py File 1.97 KB 0644
editor.py File 65.7 KB 0644
extend.txt File 3.56 KB 0644
filelist.py File 3.8 KB 0644
grep.py File 6.58 KB 0644
help.html File 53.82 KB 0644
help.py File 11.06 KB 0644
help_about.py File 8.77 KB 0644
history.py File 3.95 KB 0644
hyperparser.py File 12.58 KB 0644
idle.py File 454 B 0644
idle.pyw File 570 B 0644
iomenu.py File 20.25 KB 0644
macosx.py File 9.43 KB 0644
mainmenu.py File 3.62 KB 0644
multicall.py File 18.21 KB 0644
outwin.py File 5.67 KB 0644
paragraph.py File 7 KB 0644
parenmatch.py File 7.04 KB 0644
pathbrowser.py File 3.12 KB 0644
percolator.py File 3.06 KB 0644
pyparse.py File 19.65 KB 0644
pyshell.py File 56.38 KB 0755
query.py File 12.14 KB 0644
redirector.py File 6.71 KB 0644
replace.py File 7.33 KB 0644
rpc.py File 20.64 KB 0644
rstrip.py File 868 B 0644
run.py File 16.87 KB 0644
runscript.py File 7.66 KB 0644
scrolledlist.py File 4.35 KB 0644
search.py File 3.09 KB 0644
searchbase.py File 7.28 KB 0644
searchengine.py File 7.3 KB 0644
squeezer.py File 13 KB 0644
stackviewer.py File 4.35 KB 0644
statusbar.py File 1.41 KB 0644
textview.py File 5.98 KB 0644
tooltip.py File 6.33 KB 0644
tree.py File 14.74 KB 0644
undo.py File 10.79 KB 0644
window.py File 2.53 KB 0644
zoomheight.py File 1.31 KB 0644
zzdummy.py File 961 B 0644