自动生成类名头文件,自动生成类名头,该Python脚本程序的


该Python脚本程序的功能是在./inc/目录下自动生成类头文件包含文件。

在大工程中,有很多类实义在不同层次的头文件中,在需要引用的时候则到包含它们,如需要使用自定义的TobBar,StatusBar,StateMachine类:

include "widget/bar/topbar.h"

include "widget/bar/statusbar.h"

include "stateManager/statemachine.h"

...

引用者不仅要知道类的名称还要知道类定义的文件所在的路径。很麻烦,如果可以如下包含就好了:

include "TopBar"

include "StatusBar"

include "StateMachine"

直接引用类名,不用关心头文件名称与所在路径。

实现方法是,在./inc/目录下以类名生成包含文件,在包含文件中加入类所在的头文件。如TopBar,在./inc/下创建TopBar文件,TopBar文件内容为:

include ".././widget/bar/topbar.h"

这个脚本程序就是遍历当前目录下的所有头文件,查找出每个头文件中定义的类名,并在./inc/目录以类名创建包含文件。

#!/usr/bin/env python#coding=utf-8''' This script is due to build class include file in ./inc/It finds all head file in current path, fetch class name define ineach head files. If this class is specified in class_list.in Then, create a file name as class name in ./inc/, put C/C++ include string inside.File    : make_include_file.py[2013-10-10 V1.0] create and first commit[2013-10-11 V1.1]     (1) In last version the include path is ".././xxxx/xx.h". it should be        "../xxxx/xx.h"    (2) /*--aa.h--*/ in head file can't be recognized, but it works in         this version.'''import osimport reoutput_include_path = 'inc'built_list_file = 'Include.lst'class_need_file = 'Include.lst'class_need_list = {}built_file_list = []def read_file_lines(file_name) :    rfile = open(file_name, 'r')    lines = rfile.readlines()    rfile.close()    return linesdef find_class_define_in_line(line) :    m = re.search('^\\s*class\\s+([A-Za-z_]\\w*)', line)    if m :         if not re.search('^\\s*class\\s+[A-Za-z_]\\w*\\s*;', line) :             return m.group(1)    else :        m = re.search('^\\s*/\\*--\\s*([A-Za-z0-9_\\.]*)\\s*--\\*/', line)        if m :            return m.group(1)def get_class_list_in_file(file_name) :    class_list = []    for line in read_file_lines(file_name) :        class_name = find_class_define_in_line(line)        if class_name :            class_list.append(class_name)    return class_listdef build_class_include_file(class_name, file_name) :     context = '#include "../%s"\\n' % file_name[2:]    built_file_list.append(class_name)    wfile = open("./%s/%s" % (output_include_path, class_name), "a")    wfile.write(context)    wfile.close()def do_head_file(file_name) :    class_list = get_class_list_in_file(file_name)    for class_name in class_list :        if class_name in class_need_list :            build_class_include_file(class_name, file_name)            class_need_list[class_name] += 1            #print "%s --> %s" % (class_name, file_name)def find_head_files(path) :    tmp = os.popen('find %s -type f -name "*.hpp" -o -name "*.h"' % (path))    file_list = []    for line in tmp.readlines():        file_list.append(line.strip('\\n'))    return file_listdef build() :    file_list = find_head_files("./")    for file_name in file_list :         do_head_file(file_name)    wfile = open('./%s/%s' % (output_include_path, built_list_file), 'w')    for built_file in built_file_list :        wfile.write(built_file + '\\n')    wfile.close()def summarize() :    undo, done, more = '', '', ''    for class_name in class_need_list :        count = class_need_list[class_name]        if count == 0 :             undo += (class_name + ' ')        elif count > 1 :             more += (class_name + ' ')        else :            done += (class_name + ' ')    print '-' * 80    if done != '' :        print '[DONE]:\\n', done, '\\n'    if more != '' :        print '[FOUND MORE THAN ONCE]:\\n', more, '\\n'    if undo != '' :        print '[NOT FOUND]:\\n', undo, '\\n'def clear() :    if not os.path.exists(output_include_path) :        os.mkdir(output_include_path)        return    list_file = './%s/%s' % (output_include_path, built_list_file)    if not os.path.exists(list_file) :        return    for line in read_file_lines(list_file) :        file_name = './%s/%s' % (output_include_path, line.strip('\\n'))        if os.path.exists(file_name) :            os.remove(file_name)    os.remove(list_file)def load() :    if not os.path.exists(class_need_file) :        print 'ERROR: need ', class_need_file        quit()    for line in read_file_lines(class_need_file) :        line = line.strip('\\n')        if re.match('^#', line) : continue        if re.match('^\\s*[A-Za-z0-9_\\.]*\\s*$', line) :            class_need_list[line.strip()] = 0if __name__ == "__main__" :    clear()    load()    build()    summarize()#该片段来自于http://byrx.net

评论关闭