def checkCulyBracesFile(self, filePath_: str): _lines = fileUtils.linesFromFile(filePath_) self.analyCurlyBraces(_lines) if self.curlyBracesCount != 0: return False else: return True
def analyseFileInfo(self, srcFolderPath_: str, fileShortNameList_: list = []): _csCodeFolder = fileUtils.getPath(srcFolderPath_, "") _filePathList = folderUtils.getFileListInFolder(_csCodeFolder, [".cs"]) _finalfileDict = {} for _path in _filePathList: _fileShortName = _path.split(srcFolderPath_).pop() _isFilter = False for _filterFileShortName in fileShortNameList_: # 遍历过滤列表 if _fileShortName.find(_filterFileShortName) >= 0: # 存在过滤,就跳过 print("pass : "******"analyse : " + _fileShortName) _lines = fileUtils.linesFromFile(_path) # 类中的Class信息 _classDictList = self.getClassList(_lines, 0, len(_lines) - 1, True) _fileDict = {} _finalfileDict[_fileShortName] = _fileDict for _classDict in _classDictList: _fileDict[_classDict["className"]] = _classDict del _classDict["className"] return _finalfileDict
def addFuncStackLogFile(self, filePath_: str): _lines = fileUtils.linesFromFile(filePath_) _shortFilePath = filePath_.split(self.srcFolderPath).pop() _newLines = ["local LogUtils = require (\"LogUtils\")\n"] for _line in _lines: _funcInfo = {} # a.b = function(c) _funcReg = re.search( r'^\s*([A-Za-z0-9_\.]+)\s*=\s*function\s*\((.*)\)\s*', _line) if not _funcReg: # function a:b(c) / function a.b(c) / function a(c) _funcReg = re.search( r'^\s*function\s+([A-Za-z0-9_\:\.]+)\s*\((.*)\)\s*', _line) if not _funcReg: # local a function(c) _funcReg = re.search( r'^\s*local\s+([0-9a-zA-Z_]+)\s+function\s*\((.*)\)\s*', _line) if not _funcReg: # local a = function(c) _funcReg = re.search( r'^\s*local\s+([A-Za-z0-9_]+)\s*=\s*function\s*\((.*)\)\s*', _line) if not _funcReg: # local function a(c) _funcReg = re.search( r'^\s*local\s+function\s+([0-9a-zA-Z_]+)\s*\((.*)\)\s*', _line) if _funcReg: # 有名函数 # _funcInfo["funcName"] = _funcReg.group(1) # _funcInfo["parameters"] = _funcReg.group(2) # _funcInfo["line"] = _line # self.funcInfoList.append(_funcInfo) _newLines.append( _line.replace( _funcReg.group(0), _funcReg.group(0) + \ "LogUtils.funcIn(\"" + _shortFilePath + "\",\"" + _funcReg.group(1) + "\")\n" ) ) else: _funcReg = re.search(r'\bfunction\s*\((.*)\)$', _line) if _funcReg: # 匿名函数 # _funcInfo["funcName"] = None # _funcInfo["parameters"] = _funcReg.group(1) # _funcInfo["line"] = _line # self.funcInfoList.append(_funcInfo) _newLines.append( _line.replace( _funcReg.group(0), _funcReg.group(0) + \ "LogUtils.funcIn(\"" + _shortFilePath + "\",nil)\n" ) ) else: _newLines.append(_line) return "".join(_newLines)
def analyseJS(self, jsPath_: str): _lines = fileUtils.linesFromFile(jsPath_) # 获取短名 _jsShortName = jsPath_.split(self._jsFolder).pop() _send = [] _on = [] _off = [] _onAndOff = [] _onNotOff = [] for _line in _lines: _isFindBoo = False for _codeFilter in self._jsCodeFilterList: if _line.find(_codeFilter) > 0: _isFindBoo = True continue if not _isFindBoo: # 去除注释行,识别特殊字符 if _line.find("protocal.") > 0 and not _line.strip().startswith("//"): _protoResult = re.search(r'.*\(protocal\.([0-9a-z-A-Z_]+)', _line) if _protoResult: _protoName = _protoResult.group(1) _sendResult = re.search(r'.*send\s*\(\s*protocal\s*\.([0-9a-z-A-Z_]+)', _line) if _sendResult: self._sendLines.append(_line) if not (_protoName in _send): _send.append(_protoName) else: _onResult = re.search(r'.*on\s*\(\s*protocal\s*\.([0-9a-z-A-Z_]+)', _line) if _onResult: self._onLines.append(_line) if not (_protoName in _on): _on.append(_protoName) else: _offResult = re.search(r'.*off\s*\(\s*protocal\s*\.([0-9a-z-A-Z_]+)', _line) if _offResult: self._offLines.append(_line) if not (_protoName in _off): _off.append(_protoName) else: self._otherLines.append(_line) else: self._otherLines.append(_line) # 挂载信息 for _protoName in _on: if _protoName in _off: _onAndOff.append(_protoName) else: _onNotOff.append(_protoName) self._jsProtoRelationDict[_jsShortName] = {} self._jsProtoRelationDict[_jsShortName]["send"] = _send self._jsProtoRelationDict[_jsShortName]["on"] = _on self._jsProtoRelationDict[_jsShortName]["off"] = _off self._jsProtoRelationDict[_jsShortName]["onAndOff"] = _onAndOff self._jsProtoRelationDict[_jsShortName]["onNotOff"] = _onNotOff
def adjustIfElseFile(self, filePath_: str): _lines = fileUtils.linesFromFile(filePath_) _lineLength = len(_lines) _newLines = [] # no 没找到,ing 正在,end 结束 for _lineIdx in range(_lineLength): _line = _lines[_lineIdx] _line = _line.rstrip() _oneLineReg = re.search(r"^[\s\^\"^\']*if\s*\((.*\))(.*);$", _line) if _oneLineReg: _line = self.breakOneLineIf(_line) _newLines.append(_line) return "\n".join(_newLines)
def funcAndEndInOneLine(self, path_: str): _lines = fileUtils.linesFromFile(path_) _errorList = [] # 问题列表 for _line in _lines: # 一行内有两个function。。。 _funcAndEndReg = re.search(r'\bfunction\b.*\bend\b', _line) if _funcAndEndReg: _errorList.append({ "desc": "同一行内有function ... end", "line": _line }) # 错误长度不为零,就记录到错误总汇 if len(_errorList) > 0: self.errorDict[path_.split(self.srcFolderPath).pop()] = _errorList
def splitFileReg(self, folderPath_: str, fileName_: str, targetFolderPath_: str, regStr_: str): _filePath = os.path.join(folderPath_, fileName_) _lines = fileUtils.linesFromFile(_filePath) _chapterCount = 0 # 章节数 _splitLines = [] # 内容缓存 for _i in range(len(_lines)): _line = _lines[_i] # 找到标题,整合上一章 _titleReg = re.search(regStr_, _line) if _titleReg: _nextLine = _lines[_i + 1] # 连续两行都是章节名,那么就去掉下一行 if re.search(regStr_, _nextLine): _lines[_i + 1] = "" _chapterCount = _chapterCount + 1 # 将之前的内容形成一个章节文件 self.linesToFile(targetFolderPath_, _splitLines, _chapterCount) _splitLines = [] # 清空内容记录 _splitLines.append(_line) # 将当前行放入内容记录 # 找到最后,最后一章的内容需要组合 self.linesToFile(targetFolderPath_, _splitLines, _chapterCount)
def pngListFromAltas(self, altasPath_: str): _atlasLines = fileUtils.linesFromFile(altasPath_) _lastLine = None _pngList = [] for _i in range(len(_atlasLines)): _atlasLine = _atlasLines[_i] # 当前以空格起始 if _atlasLine.startswith(" "): # 上一行存在 if _lastLine: # 上一行是图片 _pngList.append(_lastLine) # 上一行清空 _lastLine = None else: # 上一行不存在,上一行不是图片,当前行空格起始也不记录 _lastLine = None else: # 不是空白开始,记录当前行 _lastLine = _atlasLine return _pngList
def doubleFuncOrEndInOneLine(self, path_: str): _lines = fileUtils.linesFromFile(path_) _errorList = [] # 问题列表 for _line in _lines: # 一行内有两个function。。。 _twoFunRegInLine = re.search(r'\bfunction\b.*\bfunction\b', _line) if _twoFunRegInLine: _errorList.append({ "desc": "同一行内有两个 function", "line": _line }) # 一行内是否有两个end _twoEndRegInLine = re.search(r'\bend\b.*\bend\b', _line) if _twoEndRegInLine: _errorList.append({ "desc": "同一行内有两个 end", "line": _line }) # 错误长度不为零,就记录到错误总汇 if len(_errorList) > 0: self.errorDict[path_.split(self.srcFolderPath).pop()] = _errorList
def splitFileWordStartLine(self, folderPath_: str, fileName_: str, targetFolderPath_: str): _filePath = os.path.join(folderPath_, fileName_) _lines = fileUtils.linesFromFile(_filePath) # 章节数 _chapterCount = 0 # 内容缓存 _splitLines = [] # 用于切分的正则 _contentRegStr = r'^\s.*' for _i in range(len(_lines)): _line = _lines[_i] _contentReg = re.search(_contentRegStr, _line) if not _contentReg: _chapterCount = _chapterCount + 1 # 将之前的内容形成一个章节文件 self.linesToFileWordStartLine(targetFolderPath_, _splitLines, _chapterCount) # 清空内容记录 _splitLines = [] # 将当前行放入内容记录 _splitLines.append(_line) # 找到最后,最后一章的内容需要组合 self.linesToFileWordStartLine(targetFolderPath_, _splitLines, _chapterCount)
def getClassListWithNameSpace(self, path_: str): _lines = fileUtils.linesFromFile(path_) _lineLength = len(_lines) _nameSpaceDictList = [] for _lineIdx in range(_lineLength): _line = _lines[_lineIdx] _nameSpaceStr = r'^\s*namespace\s+([a-zA-Z0-9_\.]+)\s+\{' _nameSpaceReg = re.search(_nameSpaceStr, _line) if _nameSpaceReg: _nameSpace = _nameSpaceReg.group(1) _nameSpaceDict = {} _nameSpaceDict["namespace"] = _nameSpace _nameSpaceDictList.append(_nameSpaceDict) # 从下一行开始找,找到第一个没有闭合的 },就是当前 { 对应的 _lineAndCharIdx = self.belongToService.curlyBraces.getCloseCurlyBraces( _lineIdx + 1, _lines) if not _lineAndCharIdx == None: _closeLine = _lines[_lineAndCharIdx[0]] if not _closeLine.strip() == "}": self.raiseError( pyUtils.getCurrentRunningFunctionName(), " <closed,but not }> : " + _closeLine) else: # 正确情况下记录 _classDictList = self.getClassList( _lines, _lineIdx, _lineAndCharIdx[0]) for _classDict in _classDictList: _nameSpaceDict[ _classDict["className"]] = _classDict del _classDict["className"] _lineIdx = _lineAndCharIdx[0] else: self.raiseError(pyUtils.getCurrentRunningFunctionName(), " : not close") else: if _line.find("namespace ") >= 0: print("namespace Warning : " + str(_line)) return _nameSpaceDictList
def addFuncInOutLogFolder(self, srcFolderPath_: str, targetFolderPath_: str, fileClassFuncDict_: dict): for _fileShortName in fileClassFuncDict_.keys(): _fileClassDict = fileClassFuncDict_[_fileShortName] _filePath = srcFolderPath_ + _fileShortName _funcInLineDict = {} _funcOutLineDict = {} for _className in _fileClassDict.keys(): _classFuncDict = _fileClassDict[_className]["funcDict"] for _funcName in _classFuncDict.keys(): _funcList = _classFuncDict[_funcName] for _funcDict in _funcList: _funcInLineDict[ _funcDict["startLineIdx"]] = self.getLogIn( _fileShortName, _className, _funcName, _funcDict["parameters"]) # _funcReturnIdxList = _funcDict["returnLineIdxList"] # for _funcReturnIdx in _funcReturnIdxList: # _funcOutLineDict[_funcReturnIdx] = self.getLogOut(_fileShortName, _className, _funcName) # _funcOutLineDict[_funcDict["endLineIdx"]] = self.getLogOut(_fileShortName, _className, _funcName) # 读取出文件行 _lines = fileUtils.linesFromFile(_filePath) _newLines = [] for _lineIdx in range(len(_lines)): _line = _lines[_lineIdx] # if _lineIdx in _funcOutLineDict: # # 结束在上一行插入 # _newLines.append(_funcOutLineDict[_lineIdx]) _newLines.append(_line) if _lineIdx in _funcInLineDict: # 开始,在下一行插入 _newLines.append(_funcInLineDict[_lineIdx]) _newContent = "\n".join(_newLines) _targetFilePath = targetFolderPath_ + _filePath.split( srcFolderPath_).pop() fileUtils.writeFileWithStr(_targetFilePath, _newContent) # 写入新文件
def printSelf(self): if self.problem == "": return None _printStr = "----------------------------------------------------------------\n" # 万恶分割线 _printStr += "description : " + self.description + "\n" _printStr += "problem : " + self.problem + "\n" _printStr += "solution : " + self.solution + "\n" for _idx in range(len(self.locationList)): _codeLocation = self.locationList[_idx] if fileUtils.getUpperSuffix(_codeLocation.path) == ".CS": _csFilePath = os.path.join(_unityProjectFolderPath, _codeLocation.path) if _codeLocation.path.startswith("Packages/"): _printStr += " " + _codeLocation.path + " < " + str( _codeLocation.line) + " >\n" else: print(_csFilePath + " , " + str(_codeLocation.line)) _code = fileUtils.linesFromFile( _csFilePath, True)[_codeLocation.line - 1] # 获取代码 _printStr += " " + _codeLocation.path + " < " + str( _codeLocation.line) + " > : " + _code + "\n" else: _printStr += " " + _codeLocation.path + "\n" return _printStr
def CSharpRemoveComment(self, filePath_: str): _lines = fileUtils.linesFromFile(filePath_) _lines.append("") # 单双引号 _singleQuotes: bool = False _doubleQuotes: bool = False # @" 起始," 结束 之间 _specialQuotes: bool = False # 多行注释 _commentLines: bool = False # 新行 _newLines = [] _lineNum = 0 for _line in _lines: _line = _line.rstrip() # 右侧空格无用 _lineNum = _lineNum + 1 if _commentLines: # 多行注释中 _multipleLineReg = re.search(r"(.*?\*/)", _line) # 当前行是不是多行注释的结尾 if not _multipleLineReg: # 找不到多行注释的结尾,就直接越到下一行,当前行直接删除 # print(str(_lineNum) + " : ") # + " -> " + str(_doubleQuotes) _newLines.append("") # 舍弃当前行 continue else: # 不在多行注释中,一行只有一个花括号,直接合并到上一行就行了 if _line.strip() == "{": _currentLastIdx = len(_newLines) - 1 while (_newLines[_currentLastIdx].strip() == ""): _currentLastIdx = _currentLastIdx - 1 # 如果上一行也是空行的话,就继续向上找 if not _newLines[_currentLastIdx].endswith("{") and \ not _newLines[_currentLastIdx].startswith("#"): # 不是 { 结尾,不是 # 开头(宏) _newLines[_currentLastIdx] = _newLines[_currentLastIdx] + " {" # 添加到最后一个非空行的最后 _newLines.append("") # 当前行置为空 continue else: # 是一个宏 if _line.lstrip().startswith("#"): _newLines.append(_line) continue else: # 同一行内的多行注释去掉/*...*/,这样剩下的就是纯多行注释了 regex = r"(/\*.*?\*/)" matches = re.finditer(regex, _line, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): _line = _line.replace(match.group(1), "") # 挨个字符遍历 _charIdx = 0 _length = len(_line) while _charIdx < _length: if _charIdx >= len(_line): self.raiseError(pyUtils.getCurrentRunningFunctionName(), "解析格式错误") _char = _line[_charIdx] # if _lineNum == 147 and _charIdx == 28: # print("x") if not _doubleQuotes and not _singleQuotes and not _specialQuotes: # 双引号起始的话,寻找结束 if _char == '"': _doubleQuotes = True _charIdx = _charIdx + 1 continue # 单引号起始的话,寻找结束 if _char == "'": _singleQuotes = True _charIdx = _charIdx + 1 continue # 特殊引号 _resultTuple = strUtils.checkStr(_line, _charIdx, '@"') if _resultTuple[0]: _specialQuotes = True _charIdx = _resultTuple[1] continue # 不在注释中的话 if not _commentLines: _resultTuple = strUtils.checkStr(_line, _charIdx, "//") if _resultTuple[0]: _line = _line[0:_charIdx] # 当前行已经完犊子了 break else: _resultTuple = strUtils.checkStr(_line, _charIdx, "/*") if _resultTuple[0]: _commentLines = True # /* _line = _line[0:_charIdx] # 当前行已经完犊子,因为,之前已经去掉了同一行结束的多行注释。 break else: # 多行注释中的话 _resultTuple = strUtils.checkStr(_line, _charIdx, "*/") if _resultTuple[0]: _charIdx = _resultTuple[1] # 向下推荐一个字符 _line = _line[_charIdx: _length] # 变更总长 _length = _length - _charIdx - 1 # 前面的都删了,剩余多少个字符 _charIdx = 0 # 变更当前序号,下一个循环从头开始 _commentLines = False _charIdx = _charIdx + 1 continue # 下一个字符 else: if _doubleQuotes or _singleQuotes: if _char == '\\' and _charIdx < (_length - 1): # 转意字符 _charIdx = _charIdx + 2 continue else: if _doubleQuotes: if _char == '"': _doubleQuotes = False if _singleQuotes: if _char == "'": _singleQuotes = False if _specialQuotes: if _char == '"': _specialQuotes = False _charIdx = _charIdx + 1 # print(str(_lineNum) + " : " + _line.rstrip()) # + " -> " + str(_doubleQuotes) _newLines.append(_line.rstrip()) # 右侧空格无用 return "\n".join(_newLines)
def analyseSingle(self, filePath_: str, folderName_: str): _funcLineCount = 0 # 做判断的,用来显示的路径 fileShowName = filePath_.split(folderName_)[1] # 实质内容的行数 currentLine = 0 # 行数 lineCount = 0 # 多行注释堆栈 multiCommon = False # 记录判断修改后的行 jsLines = [] # 读每一行 jsCodes = fileUtils.linesFromFile(filePath_) # 逐行循环 for jsLine in jsCodes: # 记录行号 lineCount = lineCount + 1 # 临时的行副本 tempJsLine = jsLine # 多行注释/*... if multiCommon == False: commonMatchBegin = re.search(r'/\*.*', tempJsLine) if commonMatchBegin: # /* ... */ commonMatchOneLine = re.search(r'/\*.*\*/(.*)', tempJsLine) if commonMatchOneLine: multiCommon = False tempJsLine = commonMatchOneLine.group(1) else: multiCommon = True tempJsLine = '' else: # ...*/ commonMatchEnd = re.search(r'.*\*/(.*)', tempJsLine) if commonMatchEnd: multiCommon = False tempJsLine = commonMatchEnd.group(1) if multiCommon: # 处于多行注释,相当于代码是空行 jsLine = "\n" jsLines.append(jsLine) continue # 单行注释 commonMatch = re.search(r'(.*)//.*', tempJsLine) if commonMatch: # 排除网址,最后一个字符为\这样的字符串拼接.字符串拼接里面的//不是代码的注释,是字符串的一部分. if tempJsLine.find("http://") < 0 and \ tempJsLine.find("https://") < 0 and \ tempJsLine.strip()[len(tempJsLine.strip()) - 1] != "\\" and \ tempJsLine.find("wss://") < 0 and \ tempJsLine.find("ws://") < 0: tempJsLine = commonMatch.group(1) # 分割注释,提取非注释部分 jsLine = tempJsLine + "\n" # 去空格及特殊符号 tempJsLine = tempJsLine.strip() # 去掉空白行 if tempJsLine.strip() == '': jsLines.append("\n") continue # 不考虑 '...function...' 以及 "...function..." 的情况。。。 # 有实质内容,行数叠加 currentLine = currentLine + 1 # log取得 logMatch = re.search(r'(.*)cc\.log\(.*\);?(.*)', tempJsLine) # 含log if logMatch: # 去掉原有log jsLine = logMatch.group(1) + logMatch.group(2) # log取得 logMatch = re.search(r'(.*)console\.log\(.*\);?(.*)', tempJsLine) # 含log if logMatch: # 去掉原有log jsLine = logMatch.group(1) + logMatch.group(2) # log取得 logMatch = re.search(r'(.*)window\.debug\.log\(.*\);?(.*)', tempJsLine) # 含log if logMatch: # 去掉原有log jsLine = logMatch.group(1) + logMatch.group(2) else: logMatch = re.search(r'(.*)debug\.log\(.*\);?(.*)', tempJsLine) # 含log if logMatch: # 去掉原有log jsLine = logMatch.group(1) + logMatch.group(2) # 用于 new Function 的方式。工程内没有 # functionBigMatch=re.search(r'(.*)Function(.*)',tempJsLine) # if functionBigMatch: # print tempJsLine functionMatch = re.search(r'.*function.*\(.*\).*', tempJsLine) if functionMatch: # 工程里有/function\s*([^(]*)\(/ 这个正则表达式 规避掉这个情况 if tempJsLine.find('match(/function\s*([^(]*)\(/)') < 0: # 记录Function数量 _funcLineCount = _funcLineCount + 1 functionName = '' arguments = '' functionOneLine_1 = re.search( r'.*function\s*(.*)\s*\(\s*.*\s*\)\s*\{.*', tempJsLine) if functionOneLine_1: # xx:function(xx){ functionOneLine_2 = re.search( r'\s*(.*)\s*:\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_2: functionName = functionOneLine_2.group(1) arguments = functionOneLine_2.group(2) else: # * (function(){ functionOneLine_3 = re.search( r'.*\(\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_3: if tempJsLine.strip() == '(function(){': # 就是初始化用的 functionName = "__init__" arguments = functionOneLine_3.group(1) else: # ty.PublishersManager = (function(){ functionOneLine_4 = re.search( r'\s*(.*)\s*=\s*\(\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_4: functionName = functionOneLine_4.group( 1) arguments = functionOneLine_4.group(2) else: # this.view.ccbRootNode.setOnExitCallback(function(){ functionOneLine_5 = re.search( r'\s*(.*)\(\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_5: functionName = functionOneLine_5.group( 1) arguments = functionOneLine_5.group( 2) else: # ty.PublishersManager = function(){ functionOneLine_6 = re.search( r'\s*(.*)\s*=\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_6: # var sss = function(){ functionOneLine_7 = re.search( r'\s*var\s*(.*)\s*=\s*function\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_7: functionName = "var_" + functionOneLine_7.group( 1) arguments = functionOneLine_7.group(2) else: functionName = functionOneLine_6.group( 1) arguments = functionOneLine_6.group(2) else: functionOneLine_11 = re.search( r'\s*function\s*(.*)\s*\(\s*(.*)\s*\)\s*\{.*', tempJsLine) if functionOneLine_11: functionName = functionOneLine_11.group(1) arguments = functionOneLine_11.group(2) if functionName.strip() == '': # function xx(){ 这样的全局函数。 jsLines.append(jsLine) print("{0} _ {1} _ 类似全局函数,或者特殊写法,未添加log".format( fileShowName, jsLine.split('\n'[0]))) else: # 去掉没过滤好的空格 functionName = functionName.strip() # 拆分模式 replace = re.search(r'(.*function.*\(.*\).*\{)(.*)', jsLine) # 整理参数-让参数 得以在运行时输出成对应值 argumentsStr = '{' argumentsList = arguments.split(",") for arg_i in range(len(argumentsList)): argument = argumentsList[arg_i].strip() if argument == '': continue else: argumentsStr += '"' + argument + '":' + argument + ',' argumentsStr += '}' # 折射全局对象名-全局函数的文件不输出js路径,而是输出自己的全局类名 className = '' if self._fileNameToObjNameBoo: # 存在对应关系就转换 if fileShowName in self._fileNameToObjName: className = self._fileNameToObjName[ fileShowName] # 函数名有 ' 的话,把 ' 转意了. if functionName.find("\'") > 0: functionName = "\\\'".join(functionName.split("'")) # 是否需要过滤-运行时,取得这个作为参数,决定是否显示log passBooStr = 'false' if self._justFilterLogsBoo: # 只过滤 passBooStr = 'true' if fileShowName in self._filterLogsDict: passBooStr = 'false' print("{0} _ {1} _会输出".format( fileShowName, functionName)) else: # 只显示 if passBooStr == 'false': changeBoo = True if fileShowName in self._filterLogsDict: if functionName in self._filterLogsDict[ fileShowName]: changeBoo = False if changeBoo == False: passBooStr = 'true' print("{0} _ {1} _输出被过滤".format( fileShowName, functionName)) if (fileShowName + ' -> ' + functionName) in self._specialFuncLogs: functionName = self._specialFuncLogs[ fileShowName + ' -> ' + functionName] newJsLine = replace.group( 1 ) + '\n' + '_global.log(' + passBooStr + ',\'' + fileShowName.ljust( 45 ) + '\',\'' + className.rjust( 25 ) + '\',\'' + functionName + '\',' + argumentsStr + ');\n' + replace.group( 2) # 修改后的 jsLines.append(newJsLine) else: # 使用Function做正则表达式的行 jsLines.append(jsLine) else: # 非function行 jsLines.append(jsLine) if (len(jsLines) != lineCount): print('识别行数与给定行数不一致 : {0} {1} - {2}'.format( fileShowName, len(jsLines), lineCount)) else: # 如果存在任意一行的话 if len(jsLines) > 0: jsLines[0] = jsLines[0].split('\n')[0] + '\n' jsLines.insert( 0, addLogFun(self._startLogCount, self._endLogCount)) jsCodeStr = "".join(jsLines) # 写一个新的 fileUtils.writeFileWithStr(filePath_, jsCodeStr) return _funcLineCount
def removeFirstCharsInEveryLine(self, filePath_: str, charCount_: int): _lines = fileUtils.linesFromFile(filePath_) for _i in range(len(_lines)): _lines[_i] = _lines[_i][charCount_:] fileUtils.writeFileWithStr(filePath_, "".join(_lines)) return
def luaRemoveComment(self, filePath_: str): _lines = fileUtils.linesFromFile(filePath_) _singleQuotes: bool = False # 单双引号 _doubleQuotes: bool = False _specialQuotes: bool = False # 特殊引号 [[ 起始,]] 结束 之间 _commentLines: bool = False # 多行注释 --[[ 起始,--]] 结束 之间 _newLines = [] # 新行 _lineNum = 0 for _line in _lines: _line = _line.rstrip() # 右侧空格无用 _lineNum = _lineNum + 1 if _commentLines: # 多行注释中 _multipleLineReg = re.search(r'(.*?\]\])', _line) # 当前行是不是多行注释的结尾 if not _multipleLineReg: # 找不到多行注释的结尾,就直接越到下一行,当前行直接删除 # print(str(_lineNum) + " : ") # + " -> " + str(_doubleQuotes) _newLines.append("") # 舍弃当前行 continue else: # 同一行内的多行注释去掉/*...*/,这样剩下的就是纯多行注释了 regex = r'(\-\-\[\[.*?\]\])' matches = re.finditer(regex, _line, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): _line = _line.replace(match.group(1), "") # 挨个字符遍历 _charIdx = 0 _length = len(_line) while _charIdx < _length: if _charIdx >= len(_line): self.raiseError(pyUtils.getCurrentRunningFunctionName(), "解析格式错误") _char = _line[_charIdx] # if _lineNum == 147 and _charIdx == 28: # print("x") if not _doubleQuotes and not _singleQuotes and not _specialQuotes: # 双引号起始的话,寻找结束 if _char == '"': _doubleQuotes = True _charIdx = _charIdx + 1 continue # 单引号起始的话,寻找结束 if _char == "'": _singleQuotes = True _charIdx = _charIdx + 1 continue # 特殊引号 _resultTuple = strUtils.checkStr(_line, _charIdx, '[[') if _resultTuple[0]: _specialQuotes = True _charIdx = _resultTuple[1] continue # 不在注释中的话 if not _commentLines: _resultTuple = strUtils.checkStr(_line, _charIdx, '--[[') if _resultTuple[0]: _commentLines = True # --[[ _line = _line[0:_charIdx] # 当前行已经完犊子,因为,之前已经去掉了同一行结束的多行注释。 break else: _resultTuple = strUtils.checkStr(_line, _charIdx, '--') if _resultTuple[0]: _line = _line[0:_charIdx] # 当前行已经完犊子了 break else: # 多行注释中的话 _resultTuple = strUtils.checkStr(_line, _charIdx, "]]") if _resultTuple[0]: _charIdx = _resultTuple[1] # 向下推荐一个字符 _line = _line[_charIdx: _length] # 变更总长 _length = _length - _charIdx - 1 # 前面的都删了,剩余多少个字符 _charIdx = 0 # 变更当前序号,下一个循环从头开始 _commentLines = False _charIdx = _charIdx + 1 continue # 下一个字符 else: if _doubleQuotes or _singleQuotes: if _char == '\\' and _charIdx < (_length - 1): # 转意字符 _charIdx = _charIdx + 2 continue else: if _doubleQuotes: if _char == '"': _doubleQuotes = False if _singleQuotes: if _char == "'": _singleQuotes = False if _specialQuotes: _resultTuple = strUtils.checkStr(_line, _charIdx, "]]") if _resultTuple[0]: _specialQuotes = False _charIdx = _resultTuple[1] continue _charIdx = _charIdx + 1 # print(str(_lineNum) + " : " + _line.rstrip() + " -> " + # str(_doubleQuotes) + "," + # str(_singleQuotes) + "," + # str(_specialQuotes) # ) _newLines.append(_line.rstrip()) # 右侧空格无用 return "\n".join(_newLines)