用来删除菊花公司代码中大量无用的函数头部注释的python剃刀
删除前很多函数都有不少基本无用注释,整个注释中除了 函数说明这一项有用外,其他像函数名称,输入参数,输出参数,返回值等等,对于你阅读代码基本无用。完全可以删去,换个眼前干净
/************************************** functionName: dddddddd TTTT: ttttttttttttttt aaaaaaaaaaaa aaaaaaaaaaaa useful notes of the functions input:dddddddddd author:ddddddddddddddd ************************************/ int main(int argc, char const *argv[]) { /***********/ int d = 0; char a[] = {'1','b','d'}; /***********/ return 0; }
删除之后,仅保留头部注释中 有用的 函数说明一项,看着舒服多了。
/************************************** TTTT: ttttttttttttttt aaaaaaaaaaaa aaaaaaaaaaaa useful notes of the functions ************************************/ int main(int argc, char const *argv[]) { /***********/ int d = 0; char a[] = {'1','b','d'}; /***********/ return 0; }
下面是实现这个小功能的python代码
1 ''' 2 function: clean these nonsense function notes 3 author: fpj @ 2013.7.5 4 ''' 5 6 import os, re 7 8 ''' 9 walk all .c files in the [root] directory 10 ''' 11 def GetAllFiles(root): 12 for rootpath, dirList, fileList in os.walk(root): 13 print("display all files in: %s \r directory: %s \r file:%s \r" % (rootpath, dirList, fileList) ) 14 for file in fileList: 15 if re.match(r'\w*.c', file): 16 cCleaner(os.path.join(root,file)) 17 18 for directory in dirList: 19 GetAllFiles(os.path.join(root,directory)) 20 21 ''' 22 clean the nonsense function notes in [cleanedFile] 23 ''' 24 def cCleaner(toCleanedFileName): 25 if not os.path.exists(toCleanedFileName): 26 print("ERROR! the file[%s] is not exist.\r" % toCleanedFileName) 27 return 28 29 tmpFileName = toCleanedFileName + "_tmp" 30 cleanedFile = open(tmpFileName, 'a+', encoding='utf-8') 31 32 file = open(toCleanedFileName, 'r+', encoding='utf-8') 33 inNonsenseScope = False # judge if the line is in nonsense documents 34 isUsefulNotes = False # judge if the line is useful notes when the line is in nonsense documents 35 for line in file: 36 if re.match(r'^\s?\/\*{1,8}', line) and not(re.search(r'\*{1,}/', line)): # match the /**********, if there are any SPACE ahead, it will be matched. 37 inNonsenseScope = True 38 print("%s" % line, end='', file = cleanedFile) 39 continue 40 41 if re.search(r'\*{1,}/', line): # search the **********/, if there are any SPACE ahead, it will be matched. 42 inNonsenseScope = False 43 print("%s" % line, end='', file = cleanedFile) 44 continue 45 46 if inNonsenseScope: # lines of Nonsense Function documents 47 if re.search('^\s?TTTT', line): # TTTT means these useful notes of functions 48 isUsefulNotes = True 49 print("%s" % line, end='', file = cleanedFile) 50 continue 51 52 if re.search('^\s?NNNN', line): # NNNN means these nonsense notse of functions 53 isUsefulNotes = False 54 continue 55 56 if isUsefulNotes: # useful notes of function maybe more than one line 57 print("%s" % line, end='', file = cleanedFile) 58 continue 59 60 if not inNonsenseScope: # lines of Not function documents 61 print("%s" % line, end='', file = cleanedFile) 62 63 # close the file and delete the old one then rename it 64 file.close() 65 cleanedFile.flush() 66 cleanedFile.close() 67 68 os.remove(toCleanedFileName) 69 os.renames(tmpFileName, toCleanedFileName) 70 71 # # # main script # # # 72 # input the directory these .c files included. 73 root = input('Enter directory:') 74 if not os.path.isdir(root): 75 root = os.getcwd() 76 print("Error! you can't give a valid file path;so we will start from current directory[%s]!\r" % root) 77 78 # walk all .c files in the [root] directory 79 GetAllFiles(root)

浙公网安备 33010602011771号