Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: CallTips.py
"""CallTips.py - An IDLE Extension to Jog Your Memory
[0] Fix | Delete
[1] Fix | Delete
Call Tips are floating windows which display function, class, and method
[2] Fix | Delete
parameter and docstring information when you type an opening parenthesis, and
[3] Fix | Delete
which disappear when you type a closing parenthesis.
[4] Fix | Delete
[5] Fix | Delete
"""
[6] Fix | Delete
import __main__
[7] Fix | Delete
import re
[8] Fix | Delete
import sys
[9] Fix | Delete
import textwrap
[10] Fix | Delete
import types
[11] Fix | Delete
[12] Fix | Delete
from idlelib import CallTipWindow
[13] Fix | Delete
from idlelib.HyperParser import HyperParser
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
class CallTips:
[17] Fix | Delete
[18] Fix | Delete
menudefs = [
[19] Fix | Delete
('edit', [
[20] Fix | Delete
("Show call tip", "<<force-open-calltip>>"),
[21] Fix | Delete
])
[22] Fix | Delete
]
[23] Fix | Delete
[24] Fix | Delete
def __init__(self, editwin=None):
[25] Fix | Delete
if editwin is None: # subprocess and test
[26] Fix | Delete
self.editwin = None
[27] Fix | Delete
return
[28] Fix | Delete
self.editwin = editwin
[29] Fix | Delete
self.text = editwin.text
[30] Fix | Delete
self.calltip = None
[31] Fix | Delete
self._make_calltip_window = self._make_tk_calltip_window
[32] Fix | Delete
[33] Fix | Delete
def close(self):
[34] Fix | Delete
self._make_calltip_window = None
[35] Fix | Delete
[36] Fix | Delete
def _make_tk_calltip_window(self):
[37] Fix | Delete
# See __init__ for usage
[38] Fix | Delete
return CallTipWindow.CallTip(self.text)
[39] Fix | Delete
[40] Fix | Delete
def _remove_calltip_window(self, event=None):
[41] Fix | Delete
if self.calltip:
[42] Fix | Delete
self.calltip.hidetip()
[43] Fix | Delete
self.calltip = None
[44] Fix | Delete
[45] Fix | Delete
def force_open_calltip_event(self, event):
[46] Fix | Delete
"""Happens when the user really wants to open a CallTip, even if a
[47] Fix | Delete
function call is needed.
[48] Fix | Delete
"""
[49] Fix | Delete
self.open_calltip(True)
[50] Fix | Delete
[51] Fix | Delete
def try_open_calltip_event(self, event):
[52] Fix | Delete
"""Happens when it would be nice to open a CallTip, but not really
[53] Fix | Delete
necessary, for example after an opening bracket, so function calls
[54] Fix | Delete
won't be made.
[55] Fix | Delete
"""
[56] Fix | Delete
self.open_calltip(False)
[57] Fix | Delete
[58] Fix | Delete
def refresh_calltip_event(self, event):
[59] Fix | Delete
"""If there is already a calltip window, check if it is still needed,
[60] Fix | Delete
and if so, reload it.
[61] Fix | Delete
"""
[62] Fix | Delete
if self.calltip and self.calltip.is_active():
[63] Fix | Delete
self.open_calltip(False)
[64] Fix | Delete
[65] Fix | Delete
def open_calltip(self, evalfuncs):
[66] Fix | Delete
self._remove_calltip_window()
[67] Fix | Delete
[68] Fix | Delete
hp = HyperParser(self.editwin, "insert")
[69] Fix | Delete
sur_paren = hp.get_surrounding_brackets('(')
[70] Fix | Delete
if not sur_paren:
[71] Fix | Delete
return
[72] Fix | Delete
hp.set_index(sur_paren[0])
[73] Fix | Delete
expression = hp.get_expression()
[74] Fix | Delete
if not expression or (not evalfuncs and expression.find('(') != -1):
[75] Fix | Delete
return
[76] Fix | Delete
arg_text = self.fetch_tip(expression)
[77] Fix | Delete
if not arg_text:
[78] Fix | Delete
return
[79] Fix | Delete
self.calltip = self._make_calltip_window()
[80] Fix | Delete
self.calltip.showtip(arg_text, sur_paren[0], sur_paren[1])
[81] Fix | Delete
[82] Fix | Delete
def fetch_tip(self, expression):
[83] Fix | Delete
"""Return the argument list and docstring of a function or class
[84] Fix | Delete
[85] Fix | Delete
If there is a Python subprocess, get the calltip there. Otherwise,
[86] Fix | Delete
either fetch_tip() is running in the subprocess itself or it was called
[87] Fix | Delete
in an IDLE EditorWindow before any script had been run.
[88] Fix | Delete
[89] Fix | Delete
The subprocess environment is that of the most recently run script. If
[90] Fix | Delete
two unrelated modules are being edited some calltips in the current
[91] Fix | Delete
module may be inoperative if the module was not the last to run.
[92] Fix | Delete
[93] Fix | Delete
To find methods, fetch_tip must be fed a fully qualified name.
[94] Fix | Delete
[95] Fix | Delete
"""
[96] Fix | Delete
try:
[97] Fix | Delete
rpcclt = self.editwin.flist.pyshell.interp.rpcclt
[98] Fix | Delete
except AttributeError:
[99] Fix | Delete
rpcclt = None
[100] Fix | Delete
if rpcclt:
[101] Fix | Delete
return rpcclt.remotecall("exec", "get_the_calltip",
[102] Fix | Delete
(expression,), {})
[103] Fix | Delete
else:
[104] Fix | Delete
entity = self.get_entity(expression)
[105] Fix | Delete
return get_arg_text(entity)
[106] Fix | Delete
[107] Fix | Delete
def get_entity(self, expression):
[108] Fix | Delete
"""Return the object corresponding to expression evaluated
[109] Fix | Delete
in a namespace spanning sys.modules and __main.dict__.
[110] Fix | Delete
"""
[111] Fix | Delete
if expression:
[112] Fix | Delete
namespace = sys.modules.copy()
[113] Fix | Delete
namespace.update(__main__.__dict__)
[114] Fix | Delete
try:
[115] Fix | Delete
return eval(expression, namespace)
[116] Fix | Delete
except BaseException:
[117] Fix | Delete
# An uncaught exception closes idle, and eval can raise any
[118] Fix | Delete
# exception, especially if user classes are involved.
[119] Fix | Delete
return None
[120] Fix | Delete
[121] Fix | Delete
def _find_constructor(class_ob):
[122] Fix | Delete
# Given a class object, return a function object used for the
[123] Fix | Delete
# constructor (ie, __init__() ) or None if we can't find one.
[124] Fix | Delete
try:
[125] Fix | Delete
return class_ob.__init__.im_func
[126] Fix | Delete
except AttributeError:
[127] Fix | Delete
for base in class_ob.__bases__:
[128] Fix | Delete
rc = _find_constructor(base)
[129] Fix | Delete
if rc is not None: return rc
[130] Fix | Delete
return None
[131] Fix | Delete
[132] Fix | Delete
# The following are used in get_arg_text
[133] Fix | Delete
_MAX_COLS = 85
[134] Fix | Delete
_MAX_LINES = 5 # enough for bytes
[135] Fix | Delete
_INDENT = ' '*4 # for wrapped signatures
[136] Fix | Delete
[137] Fix | Delete
def get_arg_text(ob):
[138] Fix | Delete
'''Return a string describing the signature of a callable object, or ''.
[139] Fix | Delete
[140] Fix | Delete
For Python-coded functions and methods, the first line is introspected.
[141] Fix | Delete
Delete 'self' parameter for classes (.__init__) and bound methods.
[142] Fix | Delete
The next lines are the first lines of the doc string up to the first
[143] Fix | Delete
empty line or _MAX_LINES. For builtins, this typically includes
[144] Fix | Delete
the arguments in addition to the return value.
[145] Fix | Delete
'''
[146] Fix | Delete
argspec = ""
[147] Fix | Delete
try:
[148] Fix | Delete
ob_call = ob.__call__
[149] Fix | Delete
except BaseException:
[150] Fix | Delete
if type(ob) is types.ClassType: # old-style
[151] Fix | Delete
ob_call = ob
[152] Fix | Delete
else:
[153] Fix | Delete
return argspec
[154] Fix | Delete
[155] Fix | Delete
arg_offset = 0
[156] Fix | Delete
if type(ob) in (types.ClassType, types.TypeType):
[157] Fix | Delete
# Look for the first __init__ in the class chain with .im_func.
[158] Fix | Delete
# Slot wrappers (builtins, classes defined in funcs) do not.
[159] Fix | Delete
fob = _find_constructor(ob)
[160] Fix | Delete
if fob is None:
[161] Fix | Delete
fob = lambda: None
[162] Fix | Delete
else:
[163] Fix | Delete
arg_offset = 1
[164] Fix | Delete
elif type(ob) == types.MethodType:
[165] Fix | Delete
# bit of a hack for methods - turn it into a function
[166] Fix | Delete
# and drop the "self" param for bound methods
[167] Fix | Delete
fob = ob.im_func
[168] Fix | Delete
if ob.im_self is not None:
[169] Fix | Delete
arg_offset = 1
[170] Fix | Delete
elif type(ob_call) == types.MethodType:
[171] Fix | Delete
# a callable class instance
[172] Fix | Delete
fob = ob_call.im_func
[173] Fix | Delete
arg_offset = 1
[174] Fix | Delete
else:
[175] Fix | Delete
fob = ob
[176] Fix | Delete
# Try to build one for Python defined functions
[177] Fix | Delete
if type(fob) in [types.FunctionType, types.LambdaType]:
[178] Fix | Delete
argcount = fob.func_code.co_argcount
[179] Fix | Delete
real_args = fob.func_code.co_varnames[arg_offset:argcount]
[180] Fix | Delete
defaults = fob.func_defaults or []
[181] Fix | Delete
defaults = list(map(lambda name: "=%s" % repr(name), defaults))
[182] Fix | Delete
defaults = [""] * (len(real_args) - len(defaults)) + defaults
[183] Fix | Delete
items = map(lambda arg, dflt: arg + dflt, real_args, defaults)
[184] Fix | Delete
for flag, pre, name in ((0x4, '*', 'args'), (0x8, '**', 'kwargs')):
[185] Fix | Delete
if fob.func_code.co_flags & flag:
[186] Fix | Delete
pre_name = pre + name
[187] Fix | Delete
if name not in real_args:
[188] Fix | Delete
items.append(pre_name)
[189] Fix | Delete
else:
[190] Fix | Delete
i = 1
[191] Fix | Delete
while ((name+'%s') % i) in real_args:
[192] Fix | Delete
i += 1
[193] Fix | Delete
items.append((pre_name+'%s') % i)
[194] Fix | Delete
argspec = ", ".join(items)
[195] Fix | Delete
argspec = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", argspec)
[196] Fix | Delete
[197] Fix | Delete
lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
[198] Fix | Delete
if len(argspec) > _MAX_COLS else [argspec] if argspec else [])
[199] Fix | Delete
[200] Fix | Delete
if isinstance(ob_call, types.MethodType):
[201] Fix | Delete
doc = ob_call.__doc__
[202] Fix | Delete
else:
[203] Fix | Delete
doc = getattr(ob, "__doc__", "")
[204] Fix | Delete
if doc:
[205] Fix | Delete
for line in doc.split('\n', _MAX_LINES)[:_MAX_LINES]:
[206] Fix | Delete
line = line.strip()
[207] Fix | Delete
if not line:
[208] Fix | Delete
break
[209] Fix | Delete
if len(line) > _MAX_COLS:
[210] Fix | Delete
line = line[: _MAX_COLS - 3] + '...'
[211] Fix | Delete
lines.append(line)
[212] Fix | Delete
argspec = '\n'.join(lines)
[213] Fix | Delete
return argspec
[214] Fix | Delete
[215] Fix | Delete
if __name__ == '__main__':
[216] Fix | Delete
from unittest import main
[217] Fix | Delete
main('idlelib.idle_test.test_calltips', verbosity=2)
[218] Fix | Delete
[219] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function