[Pydev-cvs] org.python.pydev/PySrc debug.py,NONE,1.1 tipper.py,NONE,1.1 inspect.py,1.1,1.2 introspec
Brought to you by:
fabioz
From: Dana M. <dan...@us...> - 2004-08-09 21:10:55
|
Update of /cvsroot/pydev/org.python.pydev/PySrc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26350/PySrc Modified Files: inspect.py introspect.py jintrospect.py Added Files: debug.py tipper.py Log Message: revamp of the autocompletion code to use only a CPython interpreter --- NEW FILE: debug.py --- import sys debugToggle = 0 def debug(name, value=None): if debugToggle == 0: return if value == None: print >> sys.stderr, "DBG:",name else: print >> sys.stderr, "DBG:%s = %s" % (name, value) Index: introspect.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/introspect.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** introspect.py 25 May 2004 21:42:14 -0000 1.2 --- introspect.py 9 Aug 2004 21:10:45 -0000 1.3 *************** *** 18,22 **** import tokenize import types ! from debug import * try: --- 18,22 ---- import tokenize import types ! from debug import debug try: *************** *** 26,54 **** False = 1==0 ! def getAutoCompleteList(command='', locals=None, includeMagic=1, includeSingle=1, includeDouble=1): """Return list of auto-completion options for command. ! The list of options will be based on the locals namespace.""" attributes = [] # Get the proper chunk of code from the command. root = getRoot(command, terminator='.') ! debug( "getAutoCompleteList: root ", root) try: if locals is not None: object = eval(root, locals) ! debug( "getAutoCompleteList: object w locals ", object) else: object = eval(root) ! debug( "getAutoCompleteList: object ", object) except: debug( "getAutoCompleteList: could not eval object ") pass else: ! attributes = getAttributeNames(object, includeMagic, includeSingle, includeDouble) ! debug( "getAutoCompleteList: attributes ", attributes) return attributes ! def getAttributeNames(object, includeMagic=1, includeSingle=1, includeDouble=1): --- 26,55 ---- False = 1==0 ! ! def getAutoCompleteList(command='', locals=None, includeMagic=1, includeSingle=1, includeDouble=1): """Return list of auto-completion options for command. ! The list of options will be based on the locals namespace.""" attributes = [] # Get the proper chunk of code from the command. root = getRoot(command, terminator='.') ! debug( "introspect:getAutoCompleteList: root ", root) try: if locals is not None: object = eval(root, locals) ! debug( "introspect:getAutoCompleteList: object w locals ", object) else: object = eval(root) ! debug( "introspect:getAutoCompleteList: object ", object) except: debug( "getAutoCompleteList: could not eval object ") pass else: ! attributes = getAttributeNames(object, includeMagic, includeSingle, includeDouble) ! debug( "getAutoCompleteList: attributes ", attributes) return attributes ! def getAttributeNames(object, includeMagic=1, includeSingle=1, includeDouble=1): *************** *** 59,64 **** # Add some attributes that don't always get picked up. If # they don't apply, they'll get filtered out at the end. ! attributes += ['__bases__', '__class__', '__dict__', '__name__', ! 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] if includeMagic: --- 60,65 ---- # Add some attributes that don't always get picked up. If # they don't apply, they'll get filtered out at the end. ! attributes += ['__bases__', '__class__', '__dict__', '__name__', ! 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name'] if includeMagic: *************** *** 82,86 **** attributes = [attribute for attribute in attributes \ if hasattr(object, attribute)] ! debug( "getAttributeNames: attributes ",attributes) return attributes --- 83,87 ---- attributes = [attribute for attribute in attributes \ if hasattr(object, attribute)] ! debug( "getAttributeNames: attributes ",attributes) return attributes *************** *** 90,94 **** def getAllAttributeNames(object): """Return dict of all attributes, including inherited, for an object. ! Recursively walk through a class and all base classes. """ --- 91,95 ---- def getAllAttributeNames(object): """Return dict of all attributes, including inherited, for an object. ! Recursively walk through a class and all base classes. """ *************** *** 149,153 **** def getCallTip(command='', locals=None): """For a command, return a tuple of object name, argspec, tip text. ! The call tip information will be based on the locals namespace.""" calltip = ('', '', '') # object name, argspec, tip text. --- 150,154 ---- def getCallTip(command='', locals=None): """For a command, return a tuple of object name, argspec, tip text. ! The call tip information will be based on the locals namespace.""" calltip = ('', '', '') # object name, argspec, tip text. *************** *** 209,217 **** def getRoot(command, terminator=None): """Return the rightmost root portion of an arbitrary Python command. ! Return only the root portion that can be eval()'d without side effects. The command would normally terminate with a '(' or '.'. The terminator and anything after the terminator will be dropped.""" command = command.split('\n')[-1] #~ print "DBG: command", command --- 210,220 ---- def getRoot(command, terminator=None): """Return the rightmost root portion of an arbitrary Python command. ! Return only the root portion that can be eval()'d without side effects. The command would normally terminate with a '(' or '.'. The terminator and anything after the terminator will be dropped.""" + debug("introspect: getRoot:command >>>", command) + debug("<<< introspect: getRoot:command:") command = command.split('\n')[-1] #~ print "DBG: command", command *************** *** 289,293 **** root = prefix + root ! return root def getTokens(command): --- 292,296 ---- root = prefix + root ! return root def getTokens(command): *************** *** 295,299 **** command = str(command) # In case the command is unicode, which fails. f = cStringIO.StringIO(command) ! # tokens is a list of token tuples, each looking like: # (type, string, (srow, scol), (erow, ecol), line) tokens = [] --- 298,302 ---- command = str(command) # In case the command is unicode, which fails. f = cStringIO.StringIO(command) ! # tokens is a list of token tuples, each looking like: # (type, string, (srow, scol), (erow, ecol), line) tokens = [] *************** *** 315,319 **** # feeding in fragments of Python code. pass ! return tokens def rtrimTerminus(command, terminator=None): --- 318,322 ---- # feeding in fragments of Python code. pass ! return tokens def rtrimTerminus(command, terminator=None): *************** *** 380,381 **** --- 383,422 ---- return constructor return None + + def printAttributes (theDoc, locals): + print "the Doc:", theDoc, '\n' + attributes = getAutoCompleteList(theDoc) + #~ attributes = ['first', 'second' , 'third'] + for a in attributes: + print "attribute:", a + print 'done!' + + theDoc = """ + import sys,os + class AClass: + def __init__(self, attribute): + self.attribute = attribute + def doSomething(self, value): + try: + self.attribute += value + print "attribute >>>", self.attribute + except TypeError: + print 'Error unable to concat ', self.attribute, 'and', value + + + a = AClass('Hello ') + b = 'world' + a.doSomething(b) + a.doSomething(42) + a. + """ + theDoc = """ + import os + os. + """ + theActivation = "a." + if __name__ == '__main__': + import sys,os + #~ printAttributes(sys.argv[1], sys.argv[2]) + printAttributes(theDoc, theActivation) + --- NEW FILE: tipper.py --- import jintrospect from debug import debug from code import InteractiveInterpreter import sys,string class Tipper: BANNER = "Tipper" def __init__(self): # command buffer self.buffer = [] self.locals = {} self.interp = Interpreter(self, self.locals) # add some handles to our objects self.locals['console'] = self def evalText(self, line): """ Triggered when enter is pressed """ #~ offsets = self.__lastLine() #~ text = self.doc.getText(offsets[0], offsets[1]-offsets[0]) #~ text = text[:-1] # chomp \n #~ self.buffer.append(text) self.buffer.append(line) source = "\n".join(self.buffer) more = self.interp.runsource(source) if more: debug("runSource output:", more) else: self.buffer = [] def showCompletions(self, line): # get the display point before writing text # otherwise magicCaretPosition is None debug('showCompletions:line:', line) line = line[:-1] # remove \n debug("showCompletions:self.locals:", self.locals) try: list = jintrospect.getAutoCompleteList(line, self.locals) debug("showCompletions:list:",list) return list except Exception, e: # TODO handle this gracefully print >> sys.stderr, e return def showClosure(self, line): debug('showClosure:line:', line) debug("showClosure:self.locals", self.locals) line = line[:-1] # remove \n (name, argspec, tip) = jintrospect.getCallTipJava(line, self.locals) return (name, argspec, tip) class Interpreter(InteractiveInterpreter): def __init__(self, tipper, locals): InteractiveInterpreter.__init__(self, locals) self.tipper= tipper def write(self, data): print >> sys.stdout, data[:-1] def GenerateTip (theDoc): debug("GenerateTip:theDoc:", theDoc) t = Tipper() theDoc = theDoc.split("\n") while len(theDoc) > 0 and theDoc[len(theDoc)-1] == "": #chomp empty trailers theDoc = theDoc[:-1] activationString = theDoc[len(theDoc)-1] debug("activationString:", activationString) theDoc = theDoc[:-1] for line in theDoc: debug("next line:", line) t.evalText(line) if activationString[len(activationString)-1] != '\n': activationString += '\n' if activationString[len(activationString)-2] == '.': list = t.showCompletions(activationString) for l in list: print >> sys.stdout, "tip:",l debug("GenerateTip:done!!") if activationString[len(activationString)-2] == '(': tuple = t.showClosure("a.doSomething(\n") #~ debug("showClosure:name", tuple[0]) #~ debug("showClosure:argspec", tuple[1]) #~ debug("showClosure:tip", tuple[2]) tip = str(tuple[2]) print >> sys.stdout, "tip:",tip[string.rindex(tip, '(')+1:] if __name__ == '__main__': import sys,os #~ printAttributes(sys.argv[1], sys.argv[2]) f = open (sys.argv[1]) theDoc = f.read() GenerateTip(theDoc) Index: jintrospect.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/jintrospect.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** jintrospect.py 25 May 2004 21:36:56 -0000 1.1 --- jintrospect.py 9 Aug 2004 21:10:45 -0000 1.2 *************** *** 3,28 **** from introspect import * import string ! from debug import * ! __author__ = "Don Coleman <dco...@ch...>" __cvsid__ = "$Id$" ! def getAutoCompleteList(command='', locals=None, includeMagic=1, includeSingle=1, includeDouble=1): """Return list of auto-completion options for command. ! The list of options will be based on the locals namespace.""" attributes = [] # Get the proper chunk of code from the command. root = getRoot(command, terminator='.') ! debug( "JgetAutoCompleteList: root ", root) try: if locals is not None: object = eval(root, locals) else: object = eval(root) except: return attributes ! debug( "JgetAutoCompleteList: object ", object) if ispython(object): # use existing code --- 3,29 ---- from introspect import * import string ! from debug import debug __author__ = "Don Coleman <dco...@ch...>" __cvsid__ = "$Id$" ! def getAutoCompleteList(command='', locals=None, includeMagic=1, includeSingle=1, includeDouble=1): """Return list of auto-completion options for command. ! The list of options will be based on the locals namespace.""" attributes = [] # Get the proper chunk of code from the command. root = getRoot(command, terminator='.') ! debug( "getAutoCompleteList: root ", root) try: if locals is not None: object = eval(root, locals) + debug( "getAutoCompleteList: object w locals ", object) else: object = eval(root) + debug( "getAutoCompleteList:locals is None object ", object) except: return attributes ! if ispython(object): # use existing code *************** *** 31,35 **** methods = methodsOf(object.__class__) attributes = [eachMethod.__name__ for eachMethod in methods] ! return attributes --- 32,36 ---- methods = methodsOf(object.__class__) attributes = [eachMethod.__name__ for eachMethod in methods] ! return attributes *************** *** 70,77 **** except AttributeError: pass ! tipList = [] argspec = '' # not using argspec for Java ! if inspect.isbuiltin(object): # inspect.isbuiltin() fails for Jython --- 71,78 ---- except AttributeError: pass ! tipList = [] argspec = '' # not using argspec for Java ! if inspect.isbuiltin(object): # inspect.isbuiltin() fails for Jython *************** *** 93,97 **** tip = "%s(%s)" % (constructor.name, paramString) tipList.append(tip) ! elif inspect.ismethod(object): method = object --- 94,98 ---- tip = "%s(%s)" % (constructor.name, paramString) tipList.append(tip) ! elif inspect.ismethod(object): method = object *************** *** 106,110 **** for eachParam in eachMethod.parameterTypes: paramList.append(eachParam.__name__) ! paramString = string.join(paramList,', ') --- 107,111 ---- for eachParam in eachMethod.parameterTypes: paramList.append(eachParam.__name__) ! paramString = string.join(paramList,', ') *************** *** 119,123 **** tip = "%s(%s) -> %s" % (eachMethod.name, paramString, eachMethod.returnType) tipList.append(tip) ! # else: # print "Not a java class :(" --- 120,124 ---- tip = "%s(%s) -> %s" % (eachMethod.name, paramString, eachMethod.returnType) tipList.append(tip) ! # else: # print "Not a java class :(" *************** *** 125,129 **** calltip = (name, argspec, string.join(tipList,"\n")) return calltip ! def ispython(object): """ --- 126,130 ---- calltip = (name, argspec, string.join(tipList,"\n")) return calltip ! def ispython(object): """ *************** *** 134,138 **** pycode = 0 pyinstance = 0 ! if inspect.isclass(object): try: --- 135,139 ---- pycode = 0 pyinstance = 0 ! if inspect.isclass(object): try: *************** *** 156,160 **** # print "object", object, "pyclass", pyclass, "pycode", pycode, "returning", pyclass | pycode ! return pyclass | pycode | pyinstance --- 157,201 ---- # print "object", object, "pyclass", pyclass, "pycode", pycode, "returning", pyclass | pycode ! return pyclass | pycode | pyinstance + def printAttributes (theDoc, theActivation): + debug("printAttributes:theDoc:", theDoc) + debug("printAttributes:locals >>>", dir(locals)) + attributes = getAutoCompleteList(theActivation) + #~ attributes = getAutoCompleteList(theDoc) + #~ attributes = ['first', 'second' , 'third'] + debug("ATTRS >>>", attributes) + debug("<<< ATTRS DONE") + for attr in attributes: + print >> sys.stdout, attr + debug('done!') + + theDoc = """ + import sys,os + class AClass: + def __init__(self, attribute): + self.attribute = attribute + def doSomething(self, value): + try: + self.attribute += value + print "attribute >>>", self.attribute + except TypeError: + print 'Error unable to concat ', self.attribute, 'and', value + + + a = AClass('Hello ') + b = 'world' + a.doSomething(b) + a.doSomething(42) + a. + """ + #~ theDoc = """ + #~ import sys + #~ sys. + #~ """ + theActivation = "sys." + if __name__ == '__main__': + import sys,os + #~ printAttributes(sys.argv[1], sys.argv[2]) + printAttributes(theDoc, theActivation) Index: inspect.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/inspect.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** inspect.py 25 May 2004 21:36:56 -0000 1.1 --- inspect.py 9 Aug 2004 21:10:45 -0000 1.2 *************** *** 29,33 **** import sys, os, types, string, re, dis, imp, tokenize ! # ----------------------------------------------------------- type-checking def ismodule(object): --- 29,33 ---- import sys, os, types, string, re, dis, imp, tokenize ! from debug import debug # ----------------------------------------------------------- type-checking def ismodule(object): *************** *** 581,585 **** ## We don't have co_code, so skip this for now ! ## ## # The following acrobatics are for anonymous (tuple) arguments. ## for i in range(nargs): --- 581,585 ---- ## We don't have co_code, so skip this for now ! ## ## # The following acrobatics are for anonymous (tuple) arguments. ## for i in range(nargs): |