Skip to content

Commit 881d4fd

Browse files
committed
[tools] 增加formatting自动化格式调整脚本(初版)
1 parent 034046c commit 881d4fd

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
File renamed without changes.

tools/tools/formatting.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#
2+
# File : formatting.py
3+
# This file is part of RT-Thread RTOS
4+
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
5+
#
6+
# This program is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License along
17+
# with this program; if not, write to the Free Software Foundation, Inc.,
18+
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
#
20+
# Change Logs:
21+
# Date Author Notes
22+
# 2021-03-02 Meco Man The first version
23+
# 2021-03-04 Meco Man 增加统一转换成UTF-8编码格式功能
24+
25+
26+
#本文件会自动对指定路径下的所有文件包括子文件夹的文件(仅针对.c.h)进行扫描
27+
# 1)将源文件编码统一为UTF-8;
28+
# 2)将TAB键替换为空格;
29+
# 3)将每行末尾多余的空格删除,并统一换行符为'\n';
30+
#使用时只需要双击本文件,输入要扫描的文件夹路径即可
31+
#不能保证100%全部成功转换为UTF-8,有一些编码特殊或识别不准确会在终端打印信息,需人工转换
32+
33+
import os
34+
import chardet
35+
36+
#用空格代替TAB键
37+
#这里并不是简单的将TAB替换成4个空格
38+
#空格个数到底是多少需要计算,因为TAB制表本身有自动对齐的功能
39+
def tab2spaces(line):
40+
list_str = list(line) #字符串变成列表
41+
i = list_str.count('\t')
42+
43+
while i > 0:
44+
ptr = list_str.index('\t')
45+
del list_str[ptr]
46+
space_need_to_insert = 4 - (ptr%4)
47+
j = 0
48+
while j < space_need_to_insert:
49+
list_str.insert(ptr,' ')
50+
j = j+1
51+
52+
i = i-1
53+
54+
line = ''.join(list_str) #列表恢复成字符串
55+
return line
56+
57+
#删除每行末尾多余的空格 统一使用\n作为结尾
58+
def formattail(line):
59+
line = line.rstrip()
60+
line = line + '\n'
61+
return line
62+
63+
#对单个文件进行格式整理
64+
def format_codes(filename):
65+
try:
66+
file=open(filename,'r',encoding = 'utf-8')
67+
file_temp=open('temp','w',encoding = 'utf-8')
68+
for line in file:
69+
line = tab2spaces(line)
70+
line = formattail(line)
71+
file_temp.write(line)
72+
file_temp.close()
73+
file.close()
74+
os.remove(filename)
75+
os.rename('temp',filename)
76+
77+
def get_encode_info(file):
78+
with open(file, 'rb') as f:
79+
code = chardet.detect(f.read())['encoding']
80+
if code == 'EUC-JP': #chardet库容易将含着少量中文的英文字符文档识别为日语编码格式
81+
code = 'GB2312'
82+
elif code == 'ISO-8859-1': #部分文件GB2312码会被识别成ISO-8859-1
83+
code = 'GB2312'
84+
85+
if not (code == 'ascii' or code == 'utf-8' or code == 'GB2312' or code == 'Windows-1252'): # Windows-1252 是由于意法半导体是法国企业's的'是法语的'导致的
86+
if code != None:
87+
print('未处理,需人工确认:'+code+':'+file) #需要人工确认
88+
code = None
89+
90+
return code
91+
92+
#将单个文件转为UTF-8编码
93+
def conver_to_utf_8 (path):
94+
try:
95+
info = get_encode_info(path)
96+
if info == None:
97+
return 0 #0 失败
98+
99+
file=open(path,'rb+')
100+
data = file.read()
101+
string = data.decode(info)
102+
utf = string.encode('utf-8')
103+
file.seek(0)
104+
file.write(utf)
105+
file.close()
106+
return 1 #1成功
107+
except UnicodeDecodeError:
108+
print("UnicodeDecodeError未处理,需人工确认"+path)
109+
return 0
110+
except UnicodeEncodeError:
111+
print("UnicodeEncodeError未处理,需人工确认"+path)
112+
return 0
113+
114+
# 递归扫描目录下的所有文件
115+
def traversalallfile(path):
116+
filelist=os.listdir(path)
117+
for file in filelist:
118+
filepath=os.path.join(path,file)
119+
if os.path.isdir(filepath):
120+
traversalallfile(filepath)
121+
elif os.path.isfile(filepath):
122+
if filepath.endswith(".c") == True or filepath.endswith(".h") == True: #只处理.c和.h文件
123+
if conver_to_utf_8(filepath) == 1: #先把这个文件转为UTF-8编码,1成功
124+
format_codes(filepath) #再对这个文件进行格式整理
125+
126+
def formatfiles():
127+
workpath = input('enter work path: ')
128+
traversalallfile(workpath)
129+
130+
if __name__ == '__main__':
131+
formatfiles()

0 commit comments

Comments
 (0)