Python导入C的头文件

1. 需求

常常有音频转文字的需求,讯飞听见网站效果还不错,赠送5小时免费转写时间,但用完了
想到讯飞有语音开放平台,过去一看,转写SDK仍是要买时间,但语音听打接口是免费的,那就试试
费了点劲注册为开发者,下载了LInux C SDK,测了下demo,没问题。为了速度,仍是用 Python 调接口吧,看上去 ctypes 实现比较天然。python

2. 问题:

SDK 头文件里用enum定义了400+的错误码,为了方便调试,打算导成Python的enum定义(>=3.4)。手动拷贝感受不爽,并且里面混杂着注释less

3. 思路:

SDK库的符号表(被strip过了)
C语言分析器(不考虑)
手动清理复制(算了)网站

4. 选择:

仍是用 gawk(比awk强大)处理下好了,比手动好点,说不许之后还能用...调试

5. 代码:

BEGIN {                         
    RS = "\r\n|{|}|\n"  # less is good for comment handling
    skip = 0

    print "#!/usr/bin/env python3"
    print "# -*- coding: UTF-8 -*-", "\n"
    print "from enum import Enum", "\n"
}

{
    while ($1 == "")
        next
}

# skip one-line comment, no use?
/^[[:blank:]]*(\/\/|#)/ {
    next
}

# skip comment
/^[[:blank:]]*\/\*/,  /\*\// {
    next
}

# get enum definitions
/^enum/, /;/ {
    subn = 0
    for (i=1; i<=NF; i++)
    {
        switch ($i) {
        case /^enum/: printf("class(Enum):")  #header
            break
        case /^;$/: printf("\n")  #tail
            next
        case /^(\/\/|\/\*)/:  #skip comment
            next
        default:  #body
            if (i == 1) printf("\t")
            subn = sub(/,/, "\n", $i)
            printf("%s ", $i)
            break
        }
    }
    if (subn == 0) printf("\n")
}

END {                           
    print "# end of", FILENAME
}
相关文章
相关标签/搜索