python之自动生成C++的父子双向继承关系

## toClass.py

import os,sys,re
import shutil,string

#[OK]
class CObject(object):
    def __init__(self,name,href):
        self._name = name
        self._href = href
        self._children = []
        self._parent = None
    def setHref(self,href):
        self._href = href
    def setParent(self,parent):
        self._parent = parent
    def addChild(self,child):
        self._children.append(child)
    def childCount(self):
        return len(self._children)
    def parent(self):
        return self._parent
    def children(self):
        return self._children
    def toAnchor(self):
        anchor_str = r'<a href="%s" style="border:1px black solid;color:red;font-weight:bold;">%s</a>' % (self._href,self._name)
        bgColor = "gray"
        color = "red"
        link = "#"
        if len(self._children) > 0:
            bgColor = "red"
            color = "blue"
            link = "child/%s.html" % (self._name)
        c_str = r'<a href="%s" style="margin-left:-8;background:%s;border:1px black solid;color:%s;font-weight:bold;">%s</a>' % (link,bgColor,color,'-')
        return "%s\n%s" % (anchor_str,c_str)
    def flushChild(self):
        fname = r'%s/child/%s.html' % (os.getcwd(),self._name)
        ret = ""
        for ch in self._children:
            ch_str = r'<div style="border:1px black solid;color:red;font-weight:bold;">%s</div>' % (ch)
            ret = "%s%s%s" % (ret,ch_str,"<hr>")
        with open(fname,'w') as f:
            f.write(ret)
    def name(self):
        return self._name
    def href(self):
        return self._href

#[OK]
gCount = 0
def getList(dirname,pFunc):
    global gCount
    try:
        ls=os.listdir(dirname)
    except:
        print dirname,'is access deny'
    else:
        for file in ls:
            temp = os.path.join(dirname,file)
            if(os.path.isdir(temp)):
                getList(temp,pFunc)
            else:
                gCount = gCount + 1
                if re.search(r'.h$',file):
                    pFunc(dirname,file)
#[OK]
def loadSiteList(file):
    file = open(file,"r")
    sites = file.readlines()
    return sites
#[OK]
def removeBR(site):
    result = re.search(r'\n',site)
    if result:
        return site.replace('\n','')
    return site
#[OK]
def process(dirname,file):
    global gCount
    #[1]
    #print '[%d]->%s' % (gCount,file)
    #[2]
    path = os.path.join(dirname,file)
    list = loadSiteList(path)
    #[3]
    for str in list:
        parseString(str,path)
#[OK]
def getClassObject(str):
    #print str
    if re.match(r'\s*\*.*\sclass\s',str):
        return None
    if re.match(r'\s*\/\/',str):
        return None
    if re.match(r'\s*\/\*',str):
        return None
    if re.search(r'\w+\sclass\s(\w+)',str):
        return None
    if re.search(r'\w+class\s(\w+)',str):#[subclass of]
        return None
    if re.search(r'\/\/.*\sclass\s',str):
        return None
    if re.search(r'\s*class\s+\w+;',str):
        return None
    if re.search(r'template\s*<class\s+(\w+)',str):
        return None
    if re.search(r'template\s*<typename\s+(\w+)',str):
        return None
    if re.search(r'[\*\(]class\s\w+',str):
        return None
    if re.search(r'class\s\w+[\[\]\;\,]',str):
        return None
    res = re.search('\s*class\s+(\w+)\s*[^\;,]',str)
    if res:
        return res.group(1)
    return None

#[OK]
def getParent(str):
    res = re.search(':\s*public\s+(\w+)\s*',str)
    if res:
        return res.group(1)
    res = re.search(':\s*publi\s*(\w+)$',str)
    if res:
        return res.group(1)
    return None

#[Test]
objList = []
objMapper = {}
def parseString(str,path):
    global objList,objMapper
    str = removeBR(str)
    obj = getClassObject(str)
    if obj != None:
        if obj in objList:
            return
        else:
            objList.append(obj)
            href = toRelPath(path)
            parent = getParent(str)
            if parent == "Parent":
            	parent = "Template"
            #currentNode
            if obj in objMapper:
                objMapper[obj].setHref(href)
                objMapper[obj].setParent(parent)
            else:
                gObj = CObject(obj,href)
                gObj.setParent(parent)
                objMapper[obj] = gObj
            #parentNode
            if parent in objMapper:
                objMapper[parent].addChild(obj)
            else:
                pObj = CObject(parent,None)
                objMapper[parent] = pObj
                objMapper[parent].addChild(obj)

def link_form(dest):
    return '-->%s' % (dest)

baseLength = len(os.getcwd())
def toRelPath(aPath):
    global baseLength
    relPath = aPath[baseLength+1:]
    return relPath.replace('\\','/')

def getPath(obj):
    global objList,objMapper
    if obj in objList:
        return toRelPath(objMapper[obj].href())
    else:
        return "#"
#[OK]
def showResult():
    global objList,objMapper
    for obj in objList:
        tmp = obj
        print objMapper[obj].toAnchor(),
        objMapper[obj].flushChild()
        while tmp:
            tmp = objMapper[tmp].parent()
            if tmp in objMapper:
                name = objMapper[tmp].name()
                if name != None:
                    if name != "RefCounted" and name != "Template":
                        print link_form(objMapper[tmp].toAnchor())
                    else:
                        ref = r'<a href="#" style="border:1px red solid;color:black;font-weight:bold;">%s</a>' % (tmp)
                        print link_form(ref)
        print '<hr>'

#[0-getData]
if not os.path.exists("child"):
    os.mkdir("child")
getList(os.path.join(os.getcwd()),process)

#[1-output]
print '<div style="width:2000;border:1px red solid;">'
showResult()
print '</div>'

@@用法说明

C++源码放置于脚本同级目录,然后执行如下脚本

toClass.py > index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值