Python 实现C、C++程序注释英文翻译插件

Python 实现C、C++程序注释英文翻译插件


1.安装必要依赖包
2.安装核心功能包translators 查看
3.编写文本输入输出函数
4.编写正则表达式分析文本内容
5.实现替换式翻译跟随式翻译
6.展示案例
7.Keil实践提示


【实现】

import sys
import uuid
import requests
import hashlib
import time
import re
from importlib import reload
import chardet
import json
import codecs
import time
import translators as ts


def connect(q):
        return  str(ts.translate_text(q,'bing','auto','cn'))
    
def readtext(addr):
        global content
        content = str(open(addr, 'r',encoding='utf-8',errors="ignore").read())
        #print("read: ",content)
        
def savetext(addr,str0):
        open(addr, 'w',encoding='UTF-8',errors="ignore").write(str0)

def test_replace_0(match):
        if match.group(1)!="null":
                matchAllStr = match.group(0)
                matchSubStr0 = match.group(1)
                matchSubStr1 = match.group(2)
                
                index0_start = match.span(0)[0]
                index0_end = match.span(0)[1]
                
                index1_start = match.span(1)[0]
                index1_end = match.span(1)[1]
                
                index2_start = match.span(2)[0]
                index2_end = match.span(2)[1]
                
                print("匹配内容:\t",matchAllStr,":",index0_start,":",index0_end,"\n翻译内容:\t",matchSubStr1,":",index2_start,":",index2_end)
                #formatEndStr=str(matchAllStr[index_n:index2_start-index0_start] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
                #formatEndStr=str(connect(matchAllStr,matchSubStr1,index2_start-index0_start,index2_end-index0_start))
                formatEndStr=str(matchAllStr[0:index2_start-index0_start] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
                print("翻译结果:\t",formatEndStr,"\r\n\r\n")
                return formatEndStr 



def test_add_0(match):
        if match.group(1)!="null":
                matchAllStr = match.group(0)
                matchSubStr0 = match.group(1)
                matchSubStr1 = match.group(2)
                
                index0_start = match.span(0)[0]
                index0_end = match.span(0)[1]
                
                index1_start = match.span(1)[0]
                index1_end = match.span(1)[1]
                
                index2_start = match.span(2)[0]
                index2_end = match.span(2)[1]

                width = index2_start - index0_start - 2
                if matchAllStr.find('\n',0,width) == -1:
                        index_n = 0
                else:
                        index_n = matchAllStr.find('\n',0,width) + 1
                index_temp = width - index_n
                
                print("匹配内容:\t",matchAllStr,":",index0_start,":",index0_end,"\n翻译内容:\t",matchSubStr1,":",index2_start,":",index2_end)
                formatEndStr=str(matchAllStr[index1_end - index0_start:index2_start-index0_start+1] + connect(matchSubStr1) + matchAllStr[index2_end-index0_start:])
                print("翻译结果:\t",formatEndStr,"\r\n\r\n")
                return matchAllStr + "\n" + (" " * index_temp)  + formatEndStr



if __name__ == "__main__":
        if len(sys.argv)>3:
                old_content = open(sys.argv[1], 'rb').read()
                print(chardet.detect(old_content))
                source_encoding = chardet.detect(old_content).get('encoding')
                if source_encoding != None:
                        if source_encoding != 'utf-8' and source_encoding != 'utf-8-SIG':
                                new_content = old_content.decode(source_encoding, 'ignore')
                                codecs.open(sys.argv[1], 'w', encoding='UTF-8').write(str(new_content))
                                #print("转换utf-8编码成功de-en")
                        readtext(sys.argv[1])
                        if sys.argv[3] == '1':
                                print("Content:\n",content,"\n***********************************************************\n")
                                
                                FormatStr0 =re.sub(r'(\s*)//([^\r\n]*)',test_replace_0,content)          
                                print("FormatStr0:\n",FormatStr0,"\n***********************************************************\n")
                                
                                FormatStr1=re.sub(r'(\s)*/\*{1}([^/]*)\*/',test_replace_0,FormatStr0)         
                                print("FormatStr1:\n",FormatStr1,"\n***********************************************************\n")
                                
                                savetext(sys.argv[2],FormatStr1)
                        else:
                                print("Content:\n",content,"\n***********************************************************\n")
                                
                                FormatStr0 =re.sub(r'(\s*)//([^\r\n]*)',test_add_0,content)          
                                print("FormatStr0:\n",FormatStr0,"\n***********************************************************\n")
                                
                                FormatStr1=re.sub(r'(\s)*/\*{1}([^/]*)\*/',test_add_0,FormatStr0)         
                                print("FormatStr1:\n",FormatStr1,"\n***********************************************************\n")
                                
                                savetext(sys.argv[2],FormatStr1)


【展示】
在这里插入图片描述

/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/
****************************************************************************
 * 版权所有 (c) 2019, Nations Technologies Inc.
 *
 * 保留所有权利。
 * ****************************************************************************
 *
 * 以源代码和二进制形式重新分发和使用,有或没有
 * 修改,只要满足以下条件,就允许:
 *
 * - 重新分发源代码必须保留上述版权声明,
 * 此条件列表和免责声明如下。
 *
 * 国家名称不得用于认可或推广源自
 * 本软件未经事先明确书面许可。
 *
 *免责声明:本软件由各国“按原样”提供,任何明示或
 * 默示保证,包括但不限于以下默示保证:
 * 适销性、特定用途的适用性和不侵权是
 *否认。在任何情况下,各国均不对任何直接、间接、
 * 附带、特殊、惩戒性或后果性损害(包括但不限于
 * 仅限于采购替代商品或服务;使用、数据、
 * 或利润;或业务中断),但原因和任何理论
 * 责任,无论是合同、严格责任还是侵权行为(包括
 * 疏忽或其他)因使用本软件而以任何方式产生,
 *即使被告知这种损害的可能性。
 * ****************************************************************************/
/*------------------- IWDG registers bit mask ----------------------------*/
/*-------------------- IWDG 寄存器位掩码----------------------------*/

/* KEY register bit mask */
 /* 按键寄存器位掩码*/
#define KEY_ReloadKey ((uint16_t)0xAAAA)
#define KEY_EnableKey ((uint16_t)0xCCCC)

  //Enables or disables write access to IWDG_PR and IWDG_RLR registers.
   //E启用或禁用对IWDG_PR和IWDG_RLR寄存器的写访问。
 // IWDG_WriteAccess new state of write access to IWDG_PR and IWDG_RLR registers.
 // IWDG_WriteAccess对IWDG_PR和IWDG_RLR寄存器的新写访问状态。
 //  This parameter can be one of the following values:
 // 此参数可以是以下值之一:
 
    /* Check the parameters */
      /* 检查参数*/
 
    /* Check the parameters */
      /* 检查参数*/
 
    /* Check the parameters */
      /* 检查参数*/
 
/**
 * @brief  Checks whether the specified IWDG flag is set or not.
 * @param IWDG_FLAG specifies the flag to check.
 *   This parameter can be one of the following values:
 *     @arg IWDG_PVU_FLAG Prescaler Value Update on going
 *     @arg IWDG_CRVU_FLAG Reload Value Update on going
 * @return The new state of IWDG_FLAG (SET or RESET).
 */
  /***
 * @brief 检查是否设置了指定的 IWDG 标志。
 * @param IWDG_FLAG指定要检查的标志。
 * 此参数可以是以下值之一:
 * @arg IWDG_PVU_FLAG 预分频器价值更新正在进行中
 * @arg IWDG_CRVU_FLAG 重新加载值更新正在进行中
 * @return IWDG_FLAG的新状态(设置或重置)。*/

/*****************************************************************************
 * 版权所有 (c) 2019, Nations Technologies Inc.
 *
 * 保留所有权利。
 * ****************************************************************************
 *
 * 以源代码和二进制形式重新分发和使用,有或没有
 * 修改,只要满足以下条件,就允许:
 *
 * - 重新分发源代码必须保留上述版权声明,
 * 此条件列表和免责声明如下。
 *
 * 国家名称不得用于认可或推广源自
 * 本软件未经事先明确书面许可。
 *
 *免责声明:本软件由各国“按原样”提供,任何明示或
 * 默示保证,包括但不限于以下默示保证:
 * 适销性、特定用途的适用性和不侵权是
 *否认。在任何情况下,各国均不对任何直接、间接、
 * 附带、特殊、惩戒性或后果性损害(包括但不限于
 * 仅限于采购替代商品或服务;使用、数据、
 * 或利润;或业务中断),但原因和任何理论
 * 责任,无论是合同、严格责任还是侵权行为(包括
 * 疏忽或其他)因使用本软件而以任何方式产生,
 *即使被告知这种损害的可能性。
 * ****************************************************************************/
/*------------------- IWDG 寄存器位掩码----------------------------*/

/*按键寄存器位掩码*/
#define KEY_ReloadKey ((uint16_t)0xAAAA)
#define KEY_EnableKey ((uint16_t)0xCCCC)

  //启用或禁用对IWDG_PR和IWDG_RLR寄存器的写访问。
 //IWDG_WriteAccess对IWDG_PR和IWDG_RLR寄存器的新写访问状态。
 //此参数可以是以下值之一:
 
    /*检查参数*/
 
    /*检查参数*/
 
    /*检查参数*/
 
/**
 * @brief 检查是否设置了指定的 IWDG 标志。
 * @param IWDG_FLAG指定要检查的标志。
 * 此参数可以是以下值之一:
 * @arg IWDG_PVU_FLAG 预分频器价值更新正在进行中
 * @arg IWDG_CRVU_FLAG 重新加载值更新正在进行中
 * @return IWDG_FLAG的新状态(设置或重置)。*/

【提示】
1.参数3个,源文件、目标文件、翻译模式
2.Keil软件使用:查看
3.此文缺少访问超时等待续翻译代码段,暂时没空添加


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种高级编程语言,而C是一种低级编程语言。在某些情况下,我们可能想要将Python程序打包成一个C程序,这样可以提高程序的执行效率和性能。 有两种常见的方法可以实现Python程序打包成C程序。第一种方法是使用Cython(C-Extensions for Python)库。Cython是一个Python编程语言的扩展,它允许将Python代码编译成等效的C代码。通过使用Cython,我们可以使用Python语法编写程序,并将其转换为C代码,然后进行编译和链接生成一个独立的C程序。 第二种方法是使用Ctypes(C-compatible data types)库。Ctypes是Python标准库中的一部分,它允许Python代码调用C语言编写的库函数。通过使用Ctypes,我们可以将Python程序中需要通过C代码实现的部分分离出来,并将其封装为一个C库。然后,我们可以使用C编译器将C代码编译成一个独立的C程序。 无论是使用Cython还是Ctypes,将Python程序打包成C程序可以提高程序的执行效率和性能。由于C语言是一种编译语言,因此C程序的执行速度往往比Python程序快很多。此外,将Python程序打包成C程序还可以提高程序的可移植性,因为生成的C程序可以在不同的平台上进行编译和执行。 总而言之,Python程序可以通过使用Cython或Ctypes库进行打包,以实现生成一个独立的C程序。这样做可以提高程序的执行效率和性能,并提高程序的可移植性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值