快速查找类定义文件位置或者函数定义文件位置,快速查找函数,#!/usr/bin/p
#!/usr/bin/python''' this file is used to find a class or function define file'''import sysimport osimport dircachepythonPath = list(set(sys.path))#delete repeat pathcurrentPath = os.getcwd()print currentPathallPythonFiles = []def stdPath(listPath): '''this function is used to change path to absolute path, such "./abc"-->"/mycurrentdir/abc", and the function is also delete repeat path, such as './root and './root/' is regarded as same path ''' global currentPath mylist = listPath retlist = [] #strList = ';'.join(listPath) if not isinstance(listPath, list): print 'method is only surported for list type' return [] for mypath in mylist: if len(mypath) == 0: mypath = currentPath elif mypath[0] != '/': if mypath[0] == '.': mypath = currentPath + mypath[1:] print mypath else: mypath = currentPath + mypath hasRepeat = False for mypath2 in retlist: if mypath2.find(mypath) >= 0 or mypath.find(mypath2) >=0: hasRepeat = True break if hasRepeat: continue retlist.append(mypath) return retlistdef appendFolderFlag(folderstr): ''' if the folderstr is not ends with '/' ,we will append '\\' for the folderstr''' if folderstr.endswith('/'): return folderstr folderstr += '/' return folderstrdef getPythonFilesIn(folder): ''' this function return all files end of '.py' in the folder Currently, this method doesn't surpport a link's folder ''' folder = appendFolderFlag(folder) if not os.path.exists(folder): print 'folder :%s not found' % folder return [] if not os.path.isdir(folder): print "current, method only surpports query under directory ,ensure % is a directory file" % (folder) return [] pythonFiles = os.listdir(folder) filelist = [] for myfile in pythonFiles: myfile = folder+myfile if os.path.isdir(myfile) or os.path.islink(myfile): continue if myfile.endswith('.py'): filelist.append(myfile) return filelistdef getFoldersIn(folder): ''' this function get folders under the folder ''' folder = appendFolderFlag(folder) if not os.path.exists(folder): print 'folder: %s not found' % folder return [] if not os.path.isdir(folder): print 'ensure %s is a folder' % (folder) return [] myfiles = os.listdir(folder) retvalue = [] for mysubfolder in myfiles: mysubfolder = folder + appendFolderFlag(mysubfolder) if os.path.isdir(mysubfolder): retvalue.append(mysubfolder) return retvaluedef getAllPythonFilesIn(folder): ''' this function get all python file *.py, include subfolder under the folder ''' global allPythonFiles if not os.path.exists(folder): print 'folder :%s not found' % folder return [] files = getPythonFilesIn(folder) allPythonFiles = allPythonFiles + files folders = getFoldersIn(folder) for mysubfolder in folders: getAllPythonFilesIn(mysubfolder)def hasTypeDefInFile(file, typename, whichtype='class'): ''' this method find python .py type file to find whether there typename define ''' if not os.path.exists(file): print 'file : %s not found' % file return False content = '' findit = False try: f = open(file) content = f.read() except: raise ("can't read file %s" % file) else: f.close() for str in content.splitlines(): if isDefLine(str,typename, whichtype): #start to judge if the line is a class define of typename findit = True return finditdef isDefLine(str,defname,whichtype='class'): '''this function is used to if the str is a whichtyp define with name defname''' if whichtype == 'class': return isClassDefLine(str,defname) elif whichtype == 'function': return isFunctionDefLine(str,defname) else: print "currently,it doesn't surpport the type define judge: %s " % whichtype return Falsedef isClassDefLine(linestr, typename): '''this class judge if linestr is a class define of typename ''' linestr = linestr.lstrip() retvalue = False words = linestr.split() if len(words) >= 2 and words[0] == 'class': if words[1] == typename: retvalue = True if words[1].startswith(typename + '('): retvalue = True return retvaluedef isFunctionDefLine(linestr, functionname): ''' this function is used to judge if linestr is a function define line''' linestr = linestr.lstrip() retvalue = False words = linestr.split() if len(words) >= 2 and words[0] == 'def': if words[1] == functionname: retvalue = True if words[1].startswith(functionname + '('): retvalue = True return retvaluedef getAllPythonFilesInFolders(folders): ''' this function used to find all python file such as "name.py" style, it mainly call getAllPythonFileIn to implement this function ''' folders = stdPath(folders) global allPythonFiles allPythonFiles = [] for folder in folders: getAllPythonFilesIn(folder)def whereTypeDefInFiles(files, typename,whichtype='class'): ''' this function is used to get typename's class define by search all file in files it mainly call hasClassDefInFile() ''' retvalue = [] for file in files: if not os.path.exists(file): print "file :%s not found " (file) elif hasTypeDefInFile(file, typename,whichtype): retvalue.append(file) return retvaluedef whereTypeDefInFolders(folders, typename,whichtype='class'): ''' this function is used to locate class position ,it mainly call hasClassDefInFiles''' getAllPythonFilesInFolders(folders) files = allPythonFiles return whereTypeDefInFiles(files, typename,whichtype)def printTypeDefPositionInFolders(folders,typename,whichtype='class'): '''just call whereTypeDefInFolders() to print results''' returnList = whereTypeDefInFolders(folders,typename,whichtype) print 'search result of %s %s is :' % (whichtype,typename) for position in returnList: print positionprintTypeDefPositionInFolders(pythonPath,'__init__','function')print hasTypeDefInFile("/usr/lib/python2.6/site-packages/slip/util/hookable.py","wrap_method", "function")print whereTypeDefInFolders(['/usr/lib/python2.6/site-packages'],'wrap_method','function')print whereTypeDefInFolders(['.'],'whereTypeDefInFolders','function')#该片段来自于http://byrx.net
评论关闭