import os
os.chdir('/home/duhan/github/CTPConverter')
import cppheader
from pandas import DataFrame
import pandas as pd
from pandas.io.excel import ExcelWriter
#%% 获取ctp所有的方法名称
# 获取CThostFtdcTraderSpi的所有方法(以OnRsp,OnRtn,OnErrRtn开头)
# 获取CThostFtdcTraderApi的所有方法(以Req开头)
TraderApi_h = cppheader.getCppHeader('include/ThostFtdcTraderApi.h',
                                     ['TRADER_API_EXPORT'])
MdApi_h = cppheader.getCppHeader('include/ThostFtdcMdApi.h', ['MD_API_EXPORT'])
ApiStruct_h = cppheader.getCppHeader('include/ThostFtdcUserApiStruct.h')
typedefDict = cppheader.getTypedefDict('include/ThostFtdcUserApiDataType.h')
enumDict = cppheader.getEnumDict('include/ThostFtdcUserApiDataType.h')
SpiClass = cppheader.getClass(TraderApi_h, 'CThostFtdcTraderSpi')
ApiClass = cppheader.getClass(TraderApi_h, 'CThostFtdcTraderApi')
MdSpiClass = cppheader.getClass(MdApi_h, 'CThostFtdcMdSpi')
MdApiClass = cppheader.getClass(MdApi_h, 'CThostFtdcMdApi')
#%% 读取api和spi类中的所有函数
apiMethodInfoDict = cppheader.getClassMethods(ApiClass, 'public')
spiMethodInfoDict = cppheader.getClassMethods(SpiClass, 'public')
mdApiMethodInfoDict = cppheader.getClassMethods(MdApiClass, 'public')
mdSpiMethodInfoDict = cppheader.getClassMethods(MdSpiClass, 'public')
#合并api和spi函数,形成完整函数集
#这里之所以可能讲所有函数合并,在于api和spi方法没有重复,请看下面语句:
#set(spiMethodInfoDict.keys()).intersection(apiMethodInfoDict.keys())
methodInfoDict = {}
methodInfoDict.update(apiMethodInfoDict)
methodInfoDict.update(spiMethodInfoDict)
mdMethodInfoDict = {}
Example #2
0
def loadCtpHeaderData():
    '''
    读取提供模板生态代码的ctp api函数信息
    '''

    # 如果存在模板数据文件直接从数据文件中读取
    if os.path.exists(templateData):
        data = pickle.load(open(templateData, 'rU'))
        return data

    # c++头文件的定义信息
    TraderApi_h = cppheader.getCppHeader('include/ThostFtdcTraderApi.h',
                                         ['TRADER_API_EXPORT'])
    MdApi_h = cppheader.getCppHeader('include/ThostFtdcMdApi.h',
                                     ['MD_API_EXPORT'])
    ApiStruct_h = cppheader.getCppHeader('include/ThostFtdcUserApiStruct.h')
    typedefDict = cppheader.getTypedefDict(
        'include/ThostFtdcUserApiDataType.h')
    enumDict = cppheader.getEnumDict('include/ThostFtdcUserApiDataType.h')

    # 从头文件信息中获取API和SPI的类定义信息
    ApiClass = cppheader.getClass(TraderApi_h, 'CThostFtdcTraderApi')
    SpiClass = cppheader.getClass(TraderApi_h, 'CThostFtdcTraderSpi')
    MdSpiClass = cppheader.getClass(MdApi_h, 'CThostFtdcMdSpi')
    MdApiClass = cppheader.getClass(MdApi_h, 'CThostFtdcMdApi')

    # 读取api和spi类中的所有函数
    apiMethodInfoDict = cppheader.getClassMethods(ApiClass, 'public')
    spiMethodInfoDict = cppheader.getClassMethods(SpiClass, 'public')
    mdApiMethodInfoDict = cppheader.getClassMethods(MdApiClass, 'public')
    mdSpiMethodInfoDict = cppheader.getClassMethods(MdSpiClass, 'public')

    # 合并生成一个API和SPI的函数全集,这样可以方便我们用名称类获取函数信息
    # API类和SPI类中没有重名的函数,所以可以这么做.
    # 确认API和SPI没有重复:set(SpiMethods.keys()).intersection(ApiMethods.keys())
    methodInfoDict = {}
    methodInfoDict.update(apiMethodInfoDict)
    methodInfoDict.update(spiMethodInfoDict)

    mdMethodInfoDict = {}
    mdMethodInfoDict.update(mdApiMethodInfoDict)
    mdMethodInfoDict.update(mdSpiMethodInfoDict)

    # 简化函数信息保留我们需要信息,并对未解决类型进行处理
    methodDict = {}
    for mi in methodInfoDict.itervalues():
        method = {}
        method['name'] = mi['name']
        method['returns'] = mi['returns']
        method['remark'] = mi['doxygen'].decode('utf8')
        parameters = []
        for pi in mi['parameters']:
            parameter = {}
            parameter['name'] = pi['name']
            parameter['type'] = pi['type']
            parameter['raw_type'] = pi['raw_type']
            parameter['pointer'] = pi['pointer']
            parameter['unresolved'] = pi['unresolved']
            parameters.append(parameter)
        method['parameters'] = parameters
        methodDict[method['name']] = method

    mdMethodDict = {}
    for mi in mdMethodInfoDict.itervalues():
        method = {}
        method['name'] = mi['name']
        method['returns'] = mi['returns']
        method['remark'] = mi['doxygen'].decode('utf8')
        parameters = []
        for pi in mi['parameters']:
            parameter = {}
            parameter['name'] = pi['name']
            parameter['type'] = pi['type']
            parameter['raw_type'] = pi['raw_type']
            parameter['pointer'] = pi['pointer']
            parameter['unresolved'] = pi['unresolved']
            parameters.append(parameter)
        method['parameters'] = parameters
        mdMethodDict[method['name']] = method

    # 将函数分成:Req,OnRsp,OnRtn,OnErrRtn,OnRspError五个大类
    # 每一类函数的参数结构都是相同的
    reqMethodDict = {
        k: v
        for k, v in methodDict.iteritems() if k.startswith('Req')
    }
    onRspMethodDict = {
        k: v
        for k, v in methodDict.iteritems()
        if k.startswith('OnRsp') and k != 'OnRspError'
    }
    onRtnMethodDict = {
        k: v
        for k, v in methodDict.iteritems() if k.startswith('OnRtn')
    }
    onErrRtnMethodDict = {
        k: v
        for k, v in methodDict.iteritems() if k.startswith('OnErrRtn')
    }
    onRspErrorMethodDict = {
        k: v
        for k, v in methodDict.iteritems() if k == 'OnRspError'
    }

    mdReqMethodDict = {
        k: v
        for k, v in mdMethodDict.iteritems() if k.startswith('Req')
    }
    mdOnRspMethodDict = {
        k: v
        for k, v in mdMethodDict.iteritems()
        if k.startswith('OnRsp') and k != 'OnRspError'
    }

    # 生成所有结构体定义的关键信息的字典结构
    structDict = {}
    structInfoDict = cppheader.getClasses(ApiStruct_h)
    for si in structInfoDict.viewvalues():
        struct = {}
        struct['name'] = si['name']
        struct['remark'] = si['doxygen'].decode('utf8')
        fields = []
        for pi in si['properties']['public']:
            field = {}
            field['name'] = pi['name']
            field['type'] = pi['type']
            field['raw_type'] = pi['raw_type']
            field['pointer'] = pi['pointer']
            field['unresolved'] = pi['unresolved']
            field['remark'] = pi['doxygen'].decode('utf8')
            fields.append(field)
        struct['fields'] = fields
        structDict[si['name']] = struct

    # 数据打包到字典中
    data = {
        'reqMethodDict': reqMethodDict,
        'onRspMethodDict': onRspMethodDict,
        'onRtnMethodDict': onRtnMethodDict,
        'onErrRtnMethodDict': onErrRtnMethodDict,
        'onRspErrorMethodDict': onRspErrorMethodDict,
        'mdReqMethodDict': mdReqMethodDict,
        'mdOnRspMethodDict': mdOnRspMethodDict,
        'structDict': structDict,
        'typedefDict': typedefDict,
        'mdMethodDict': mdMethodDict,
    }

    #  保存模板数据以便下次访问提高速度
    pickle.dump(data, open(templateData, "wb"))
    return data
Example #3
0
def loadCtpHeaderData():
    """
    读取提供模板生态代码的ctp api函数信息
    """

    # 如果存在模板数据文件直接从数据文件中读取
    if os.path.exists(templateData) :
        data = pickle.load(open(templateData, 'rU' ))
        return data

    # c++头文件的定义信息
    TraderApi_h = cppheader.getCppHeader('include/ThostFtdcTraderApi.h',['TRADER_API_EXPORT'])
    MdApi_h = cppheader.getCppHeader('include/ThostFtdcMdApi.h',['MD_API_EXPORT'])
    ApiStruct_h = cppheader.getCppHeader('include/ThostFtdcUserApiStruct.h')
    typedefDict = cppheader.getTypedefDict('include/ThostFtdcUserApiDataType.h')
    enumDict = cppheader.getEnumDict('include/ThostFtdcUserApiDataType.h')

    # 从头文件信息中获取API和SPI的类定义信息
    ApiClass = cppheader.getClass(TraderApi_h,'CThostFtdcTraderApi')
    SpiClass = cppheader.getClass(TraderApi_h,'CThostFtdcTraderSpi')
    MdSpiClass = cppheader.getClass(MdApi_h,'CThostFtdcMdSpi')
    MdApiClass = cppheader.getClass(MdApi_h,'CThostFtdcMdApi')

    # 读取api和spi类中的所有函数
    apiMethodInfoDict = cppheader.getClassMethods(ApiClass,'public')
    spiMethodInfoDict = cppheader.getClassMethods(SpiClass,'public')
    mdApiMethodInfoDict = cppheader.getClassMethods(MdApiClass,'public')
    mdSpiMethodInfoDict = cppheader.getClassMethods(MdSpiClass,'public')

    # 合并生成一个API和SPI的函数全集,这样可以方便我们用名称类获取函数信息
    # API类和SPI类中没有重名的函数,所以可以这么做.
    # 确认API和SPI没有重复:set(SpiMethods.keys()).intersection(ApiMethods.keys())
    methodInfoDict = {}
    methodInfoDict.update(apiMethodInfoDict)
    methodInfoDict.update(spiMethodInfoDict)

    mdMethodInfoDict = {}
    mdMethodInfoDict.update(mdApiMethodInfoDict)
    mdMethodInfoDict.update(mdSpiMethodInfoDict)

    # 简化函数信息保留我们需要信息,并对未解决类型进行处理
    methodDict = {}
    for mi in methodInfoDict.itervalues() :
        method = {}
        method['name'] = mi['name']
        method['returns'] = mi['returns']
        method['remark'] = mi['doxygen'].decode('utf8')
        parameters=[]
        for pi in mi['parameters'] :
            parameter = {}
            parameter['name'] = pi['name']
            parameter['type'] = pi['type']
            parameter['raw_type'] = pi['raw_type']
            parameter['pointer'] = pi['pointer']
            parameter['unresolved'] = pi['unresolved']
            parameters.append(parameter)
        method['parameters'] = parameters
        methodDict[method['name']] = method

    mdMethodDict = {}
    for mi in mdMethodInfoDict.itervalues() :
        method = {}
        method['name'] = mi['name']
        method['returns'] = mi['returns']
        method['remark'] = mi['doxygen'].decode('utf8')
        parameters=[]
        for pi in mi['parameters'] :
            parameter = {}
            parameter['name'] = pi['name']
            parameter['type'] = pi['type']
            parameter['raw_type'] = pi['raw_type']
            parameter['pointer'] = pi['pointer']
            parameter['unresolved'] = pi['unresolved']
            parameters.append(parameter)
        method['parameters'] = parameters
        mdMethodDict[method['name']] = method

    # 将函数分成:Req,OnRsp,OnRtn,OnErrRtn,OnRspError五个大类
    # 每一类函数的参数结构都是相同的
    reqMethodDict = {k:v for k,v in methodDict.iteritems() if k.startswith('Req')}
    onRspMethodDict = {k:v for k,v in methodDict.iteritems() if k.startswith('OnRsp') and k != 'OnRspError'}
    onRtnMethodDict = {k:v for k,v in methodDict.iteritems() if k.startswith('OnRtn')}
    onErrRtnMethodDict = {k:v for k,v in methodDict.iteritems() if k.startswith('OnErrRtn')}
    onRspErrorMethodDict = {k:v for k,v in methodDict.iteritems() if k == 'OnRspError'}

    mdReqMethodDict = {k:v for k,v in mdMethodDict.iteritems() if k.startswith('Req')}
    mdOnRspMethodDict = {k:v for k,v in mdMethodDict.iteritems() if k.startswith('OnRsp') and k != 'OnRspError'}

    # 生成所有结构体定义的关键信息的字典结构
    structDict = {}
    structInfoDict =  cppheader.getClasses(ApiStruct_h)
    for si in  structInfoDict.viewvalues():
        struct = {}
        struct['name'] = si['name']
        struct['remark'] = si['doxygen'].decode('utf8')
        fields = []
        for pi in si['properties']['public']:
            field = {}
            field['name'] = pi['name']
            field['type'] = pi['type']
            field['raw_type'] = pi['raw_type']
            field['pointer'] = pi['pointer']
            field['unresolved'] = pi['unresolved']
            field['remark'] = pi['doxygen'].decode('utf8')
            fields.append(field)
        struct['fields'] = fields
        structDict[si['name']] = struct


    # 数据打包到字典中
    data = {
        'reqMethodDict':reqMethodDict,
        'onRspMethodDict':onRspMethodDict,
        'onRtnMethodDict':onRtnMethodDict,
        'onErrRtnMethodDict':onErrRtnMethodDict,
        'onRspErrorMethodDict':onRspErrorMethodDict,
        'mdReqMethodDict':mdReqMethodDict,
        'mdOnRspMethodDict':mdOnRspMethodDict,
        'structDict':structDict,
        'typedefDict':typedefDict,
        'mdMethodDict':mdMethodDict,
    }

    #  保存模板数据以便下次访问提高速度
    pickle.dump( data, open(templateData,"wb") )
    return data
#%% 修改到项目目录
import os
os.chdir('/home/duhan/github/CTPConverter')
import cppheader
from pandas import DataFrame
import pandas as pd
from pandas.io.excel import ExcelWriter
#%% 获取ctp所有的方法名称
# 获取CThostFtdcTraderSpi的所有方法(以OnRsp,OnRtn,OnErrRtn开头)
# 获取CThostFtdcTraderApi的所有方法(以Req开头)
TraderApi_h = cppheader.getCppHeader('include/ThostFtdcTraderApi.h',['TRADER_API_EXPORT'])
MdApi_h = cppheader.getCppHeader('include/ThostFtdcMdApi.h',['MD_API_EXPORT'])
ApiStruct_h = cppheader.getCppHeader('include/ThostFtdcUserApiStruct.h')
typedefDict = cppheader.getTypedefDict('include/ThostFtdcUserApiDataType.h')
enumDict = cppheader.getEnumDict('include/ThostFtdcUserApiDataType.h')
SpiClass = cppheader.getClass(TraderApi_h,'CThostFtdcTraderSpi')
ApiClass = cppheader.getClass(TraderApi_h,'CThostFtdcTraderApi')
MdSpiClass = cppheader.getClass(MdApi_h,'CThostFtdcMdSpi')
MdApiClass = cppheader.getClass(MdApi_h,'CThostFtdcMdApi')
#%% 读取api和spi类中的所有函数
apiMethodInfoDict = cppheader.getClassMethods(ApiClass,'public')
spiMethodInfoDict = cppheader.getClassMethods(SpiClass,'public')
mdApiMethodInfoDict = cppheader.getClassMethods(MdApiClass,'public')
mdSpiMethodInfoDict = cppheader.getClassMethods(MdSpiClass,'public')
#合并api和spi函数,形成完整函数集
#这里之所以可能讲所有函数合并,在于api和spi方法没有重复,请看下面语句:
#set(spiMethodInfoDict.keys()).intersection(apiMethodInfoDict.keys())
methodInfoDict = {}
methodInfoDict.update(apiMethodInfoDict)
methodInfoDict.update(spiMethodInfoDict)
mdMethodInfoDict = {}