diff options
Diffstat (limited to 'python/helpers/pydev/_pydev_imports_tipper.py')
-rw-r--r-- | python/helpers/pydev/_pydev_imports_tipper.py | 118 |
1 files changed, 61 insertions, 57 deletions
diff --git a/python/helpers/pydev/_pydev_imports_tipper.py b/python/helpers/pydev/_pydev_imports_tipper.py index e4b3b863f210..76cf2cdc473f 100644 --- a/python/helpers/pydev/_pydev_imports_tipper.py +++ b/python/helpers/pydev/_pydev_imports_tipper.py @@ -4,6 +4,10 @@ import sys from _pydev_tipper_common import DoFind +try: + xrange +except: + xrange = range #completion types. TYPE_IMPORT = '0' @@ -19,20 +23,20 @@ def _imp(name, log=None): except: if '.' in name: sub = name[0:name.rfind('.')] - + if log is not None: log.AddContent('Unable to import', name, 'trying with', sub) log.AddException() - + return _imp(sub, log) else: s = 'Unable to import module: %s - sys.path: %s' % (str(name), sys.path) if log is not None: log.AddContent(s) log.AddException() - + raise ImportError(s) - + IS_IPY = False if sys.platform == 'cli': @@ -53,9 +57,9 @@ if sys.platform == 'cli': clr.AddReference(name) except: pass #That's OK (not dot net module). - + return _old_imp(initial_name, log) - + def GetFile(mod): @@ -69,19 +73,19 @@ def GetFile(mod): filename = f[:-4] + '.py' if os.path.exists(filename): f = filename - + return f def Find(name, log=None): f = None - + mod = _imp(name, log) parent = mod foundAs = '' - + if inspect.ismodule(mod): f = GetFile(mod) - + components = name.split('.') old_comp = None @@ -94,22 +98,22 @@ def Find(name, log=None): except AttributeError: if old_comp != comp: raise - + if inspect.ismodule(mod): f = GetFile(mod) else: if len(foundAs) > 0: foundAs = foundAs + '.' foundAs = foundAs + comp - + old_comp = comp - + return f, mod, parent, foundAs def Search(data): '''@return file, line, col ''' - + data = data.replace('\n', '') if data.endswith('.'): data = data.rstrip('.') @@ -118,19 +122,19 @@ def Search(data): return DoFind(f, mod), foundAs except: return DoFind(f, parent), foundAs - - + + def GenerateTip(data, log=None): data = data.replace('\n', '') if data.endswith('.'): data = data.rstrip('.') - + f, mod, parent, foundAs = Find(data, log) #print_ >> open('temp.txt', 'w'), f tips = GenerateImportsTipForModule(mod) return f, tips - - + + def CheckChar(c): if c == '-' or c == '.': return '_' @@ -146,7 +150,7 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, name, doc, args, type (from the TYPE_* constants) ''' ret = [] - + if dirComps is None: dirComps = dir(obj_to_complete) if hasattr(obj_to_complete, '__dict__'): @@ -155,22 +159,22 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, dirComps.append('__class__') getCompleteInfo = True - + if len(dirComps) > 1000: - #ok, we don't want to let our users wait forever... + #ok, we don't want to let our users wait forever... #no complete info for you... - + getCompleteInfo = False - + dontGetDocsOn = (float, int, str, tuple, list) for d in dirComps: - + if d is None: continue - + if not filter(d): continue - + args = '' try: @@ -182,18 +186,18 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, if getCompleteInfo: try: retType = TYPE_BUILTIN - + #check if we have to get docs getDoc = True for class_ in dontGetDocsOn: - + if isinstance(obj, class_): getDoc = False break - + doc = '' if getDoc: - #no need to get this info... too many constants are defined and + #no need to get this info... too many constants are defined and #makes things much slower (passing all that through sockets takes quite some time) try: doc = inspect.getdoc(obj) @@ -201,12 +205,12 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, doc = '' except: #may happen on jython when checking java classes (so, just ignore it) doc = '' - - + + if inspect.ismethod(obj) or inspect.isbuiltin(obj) or inspect.isfunction(obj) or inspect.isroutine(obj): try: args, vargs, kwargs, defaults = inspect.getargspec(obj) - + r = '' for a in (args): if len(r) > 0: @@ -250,7 +254,7 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, if major: args = major[major.index('('):] found = True - + if not found: i = doc.find('->') @@ -260,12 +264,12 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, i = doc.find('\n') if i < 0: i = doc.find('\r') - - + + if i > 0: s = doc[0:i] s = s.strip() - + #let's see if we have a docstring in the first line if s[-1] == ')': start = s.find('(') @@ -275,21 +279,21 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, end = s.find(')') if end <= 0: end = len(s) - + args = s[start:end] if not args[-1] == ')': args = args + ')' - - + + #now, get rid of unwanted chars l = len(args) - 1 r = [] - for i in range(len(args)): + for i in xrange(len(args)): if i == 0 or i == l: r.append(args[i]) else: r.append(CheckChar(args[i])) - + args = ''.join(r) if IS_IPY: @@ -305,43 +309,43 @@ def GenerateImportsTipForModule(obj_to_complete, dirComps=None, getattr=getattr, except: pass - + retType = TYPE_FUNCTION - + elif inspect.isclass(obj): retType = TYPE_CLASS - + elif inspect.ismodule(obj): retType = TYPE_IMPORT - + else: retType = TYPE_ATTR - - + + #add token and doc to return - assure only strings. ret.append((d, doc, args, retType)) - + except: #just ignore and get it without aditional info ret.append((d, '', args, TYPE_BUILTIN)) - + else: #getCompleteInfo == False if inspect.ismethod(obj) or inspect.isbuiltin(obj) or inspect.isfunction(obj) or inspect.isroutine(obj): retType = TYPE_FUNCTION - + elif inspect.isclass(obj): retType = TYPE_CLASS - + elif inspect.ismodule(obj): retType = TYPE_IMPORT - + else: retType = TYPE_ATTR #ok, no complete info, let's try to do this as fast and clean as possible #so, no docs for this kind of information, only the signatures ret.append((d, '', str(args), retType)) - + return ret - - + + |