#! /Usr/bin/python #-*-Coding: GBK -*- # Writer: xmnathan # Annotate the py file Import re Import OS Import ConfigParser Python = 'note' Def ReadIni (path, section, option): # file path, chapter, keyword # Read ini Cf = ConfigParser. ConfigParser () Cf. read (path) Value = cf. get (section, option) # If getint () is used, the data type is directly read as an integer. Return value Def IsPassLine (strLine ): # Whether the row can be ignored # Ignore the regular expression list of rows RegularExpressions = ["""/'.*#.*/'""","""/".*#.*/"""", """/'/'/'. *#. */'/'/'""","""/"/"/". *#. */"] For One in RegularExpressions: Zz = re. compile (One) If re. search (zz, strLine) = None: Continue Else: Return True # ignore if a match exists. Return False Def ReadFile (FileName ): # Reading and processing files Fobj = open (FileName, 'R ') AllLines = fobj. readlines () Fobj. close () NewStr ='' LogStr = '/n % 20 s/N' % (FileName. split (' // ') [-1]) # output log Nline = 0 For eachiline in AllLines: Index = eachline. find ('#') # obtain the location index with the comment clause '#' If index =-1 or nline <3 or IsPassLine (eachline ): If eachiline. strip ()! = '': # Exclude empty rows NewStr = NewStr + eachiline Else: If index! = 0: NewStr = NewStr + eachiline [: index] + '/N' # extract the comments LogStr + = "ChangeLine: % s/t % s" % (nline, eachline [index:]) Nline + = 1 Return NewStr, LogStr Def MakeCleanFile (SrcPath, DescPath, FileList ): FLog = open (DescPath + '// 'employee 'cleannotelog.txt', 'w ') For File in FileList: CurStr, LogStr = ReadFile (SrcPath + '//' + File) FNew = open (DescPath + '//' + File, 'w ') FNew = write (curStr) FNew. close () FLog. write (LogStr) FLog. close () Def Main (): # Obtain the path of the source folder and target folder from ini IniPath = OS. getcwd () + '//' + PtName + '. ini' SrcPath = ReadIni (IniPath, PyName, 'srcpath ') # source folder DescPath = ReadIni (IniPath, PyName, 'descpath') # destination folder # If the destination folder does not exist, create it If not OS. path. exists (DescPath ): OS. makedirs (DescPath) FileList = [] For files in OS. walk (SrcPath ): For FileName in files [2]: If FileName. split ('.') [-1] = 'py ': FileList. append (FileName) MakeCleanFile (SrcPath, DescPath, FileList) If _ name __= = '_ main __': Main () Print '>>> End <' OS. system ('pause ') |