python 代码格式规范脚本_python脚本处理代码注释规范化

python脚本 实现注释的规范化处理:

示例:

1.

源代码:

int speed = 4;//设置速度为4

处理后:

//设置速度为4

int speed = 4;

2.

源代码:

else{//否则

处理后:

//否则

else{

3.

源代码:

}else{//否则

处理后:

//否则

}else{

4.

源代码:

if(condition == true){//如果为真

处理后:

//如果为真

if(condition == true){

5.

源代码:

json处理:(js)

var testJson = {

name:”lmn”,//名字

age:”25”//年龄

}

处理后:

不进行处理。

说多无益,直接上代码:

#encoding:utf-8

'''

可能性如下:

1.单独一行注释:

2.有代码有注释

3 }else{//

4.多行注释

/**

* //获取所有配件重量总和

* @param index 将要装备的第几个配件槽

* @param unitId 将要装备配件ID

*/

5.

1: "tankmuzzle",//炮管 11

满足要求:

所有的注释都单独一行,除了JSON的注释

保持与下一行同样缩进

'''

import os

#文件列表

fileList = []

writeFileList = []

#代码注释格式声明

codeAnnotationDec = '//'

# str:将要处理的code saveList:将要存入的列表

def dealCodeWithSpace(lines,willSaveList):

# lines=lines.strip('\n')

#计算代码首部缩进空格长度 去掉尾部空格长度 - 去掉首尾空格长度

headSpaceLen = len(lines.rstrip())-len(lines.strip())

#先添加注释(注释代码+首部缩进)

# willSaveList.append(" "*headSpaceLen+lines[lines.find(codeAnnotationDec):len(lines)])

tempAnno = lines[lines.find(codeAnnotationDec): ].rstrip()

if tempAnno.find('\n')>-1:

willSaveList.append(" "*headSpaceLen+tempAnno)

else:

willSaveList.append(" "*headSpaceLen+tempAnno+'\n')

willSaveList.append('\n')

if len(willSaveList)>=2:

if willSaveList[len(willSaveList) -1] == '\n' and willSaveList[len(willSaveList) -2].find(codeAnnotationDec)>-1:

willSaveList.pop()

#再添加代码:

tempCode = lines[0:lines.find(codeAnnotationDec)]

if tempCode.find('\n')>-1:

willSaveList.append(tempCode)

else:

willSaveList.append(tempCode)

willSaveList.append('\n')

# willSaveList.append(lines[0:lines.find(codeAnnotationDec)].rstrip()+'\n')

def doDealCode(readFilePath,writeFile):

#如果传递进来的是文件,就直接加入处理文件列表

if os.path.isfile(readFilePath):

fileList.append(dir.decode("utf-8"))

writeFileList.append(dir.decode("utf-8"))

#把读取目录的所有文件加入文件列表

elif os.path.isdir(readFilePath):

for singleFile in os.listdir(readFilePath):

fileList.append(os.path.join(readFilePath,singleFile))

writeFileList.append(os.path.join(writeFile,singleFile))

if 1:

fileIndex = 0

for readSingleFile in fileList:

#处理后的代码行,每行都会插入列表中

willSaveList =[]

fileIndex = fileList.index(readSingleFile)

#初步处理文件行,依次加入一个列表中

with open(readSingleFile,'r') as fileLine:

while True:

#整行读取数据

lines = fileLine.readline()

if not lines:

break

#如果该行不为空

else:

# 该行有 单行注释

if lines.find(codeAnnotationDec)>-1:

#该行为存粹一行注释

if lines.strip().find(codeAnnotationDec) == 0:

willSaveList.append(lines)

#该行既有代码,也有注释

else:

#处理json文件,默认注释和代码放在一行:

if lines.find(":")>-1:

willSaveList.append(lines)

#处理 }else{ 情况 #存在三种情况:1: else{//dfsdfdsfsdf 2:}else{//dsfdfdsf 3.if(j == index){//计算的重量,用将要替换的配件的重量

elif len(lines[0:lines.find(codeAnnotationDec)].rstrip())>0 and lines[0:lines.find(codeAnnotationDec)].rstrip()[-1] == '{':

#第一种情况处理:

if lines.strip().find("else") == 0:

dealCodeWithSpace(lines,willSaveList)

#第二种 和第三种 情况处理

else:

#第三种情况:

if lines.strip().find('if') == 0:

dealCodeWithSpace(lines,willSaveList)

#第二种情况

else:

#计算代码首部缩进空格长度 去掉尾部空格长度 - 去掉首尾空格长度

headSpaceLen = len(lines.rstrip())-len(lines.strip())

# 1.在 else 前面插入一行:

willSaveList.append(lines[0:lines.find("else")]+'\n')

# 求出将要处理的字符串 else{ 缩进要和上一行 { 保持一致

willDealCode = headSpaceLen*' '+lines[lines.find("else"): ]

# 2.按照 code;//dsfdsfsdf 的格式进行处理

dealCodeWithSpace(willDealCode,willSaveList)

#不含特殊情况的注释情况:例如:code;//annotation

else:

dealCodeWithSpace(lines,willSaveList)

#该行没有注释(一行代码或者换行)

else:

# willSaveList[len(willSaveList)-1].scrip().find('\n') == 0:

willSaveList.append(lines)

pass

#写入同名文件到另一个目录下

with open(writeFileList[fileIndex],'w') as tempWrite:

print("------ WRITE SUC------",writeFileList[fileIndex]);

print(writeFileList[fileIndex])

for line in willSaveList:

tempWrite.write(line)

# print("******* SUC *******");

#读取文件的路径

readFilePath = "./readFile"

#写入文件的路径

writeFile = "./writeFile"

#开始处理文件

doDealCode(readFilePath,writeFile)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值