用Python生成QT Creator工程

由于一些原因无法使用Source Insight之类的工具看代码,VIM之类神器熟悉程度也有限,所以就在QT Creator上想了一个办法。

原理:观察QT Creator的工程文件发现主要由两个文件组成(.creator 和 .files)。 其中最重要的是.files文件,这个文件由文件列表组成。OK,这要用python遍历源码目录并生成相应的.creator 和 .files 即可。

以下是源码:

   
   
   
   
1 # ------------------------------------------------------------------------------- 2 # Name: QTProjectGenerator 3 # Purpose: create QT project file. 4 # 5 # Author: Icove 6 # 7 # Created: 29/01/2013 8 # Copyright: None 9 # Licence: None 10 # ------------------------------------------------------------------------------- 11 12 import os 13 14 # Prioritize the ignore list. It mains if one of extension both in ignore list and target list, this extension will be ignord 15 ignore_file_ext = [] # [".csv", ".gz", ".tgz"] 16 target_file_ext = [] # [".c", ".cpp", ".h", ".hpp", ".h", ".cc", ".hxx", ".cxx", ".config", ".xml", ".mk", ".component", ".feature", ".java"] 17 18 def main(): 19 # project name, default by "project" 20 prjectname = raw_input( " Input Project Name: " ) 21 prjectname = prjectname.strip() 22 if prjectname == "" : 23 prjectname = " project " 24 25 # List the folder in current folder. 26 dirs = os.listdir( " ./ " ) 27 index = 0 28 dirlist = [] 29 for i in dirs: 30 if os.path.isdir(i): 31 print " [%02d]%s " % (index, i) 32 dirlist.append(i) 33 index += 1 34 while ( 1 ): 35 chooseid = raw_input( " Please Choose a folder: " ) 36 try : 37 folderid = int(chooseid) 38 if folderid >= len(dirlist) or (folderid < 0): 39 print " Input Error!! " 40 inputtxt = raw_input( ''' Press any key to choose agian or press "Q" to quit ''' ) 41 inputtxt = inputtxt.strip() 42 if inputtxt == " q " or inputtxt == " Q " : 43 break 44 else : 45 continue 46 sourcefolder = dirlist[folderid] 47 print sourcefolder 48 prjfile = open( " .\\ " + prjectname + " .creator " , " w " ) 49 print >> prjfile, " [General] " 50 prjfile.close() 51 52 prjlistfile = open( " .\\ " + prjectname + " .files " , " w " ) 53 for path, dirs, files in os.walk( " .\\ " + sourcefolder + " \\ " ): 54 for name in files: 55 # Ignore extension in ignore_file_ext 56 if (len(ignore_file_ext) <> 0) and (os.path.splitext(name)[ 1 ] in ignore_file_ext): 57 continue 58 # process extension in target_file_ext 59 if (len(target_file_ext) == 0) or (os.path.splitext(name)[ 1 ] in target_file_ext): 60 filepath = path + " \\ " + name 61 filepath = filepath.replace( " .\\ " , "" ) 62 print filepath 63 print >> prjlistfile, filepath 64 print " Compeleted!! " 65 break 66 except : 67 print " Somethings Error!! " 68 inputtxt = raw_input( ''' Press any key to work agian or press "Q" to quit ''' ) 69 inputtxt = inputtxt.strip() 70 if inputtxt == " q " or inputtxt == " Q " : 71 break 72 else : 73 continue 74 if __name__ == ' __main__ ' : 75 main() 76

浏览cocos2d-x源码效果:

image

缺陷:Qt creator 把代码所有的index都放到内存中,这就注定了无法浏览大型项目源码。

你可能感兴趣的:(用Python生成QT Creator工程)