def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(PyforaInspect.getlines(sourceFile)) return sourceFileCache_[sourceFile], sourceFile
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join( PyforaInspect.getlines(sourceFile)) return sourceFileCache_[sourceFile], sourceFile
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) if sourceFile in sourceFileCache_: return sourceFileCache_[sourceFile], sourceFile with open(sourceFile, "r") as f: tr = f.read() sourceFileCache_[sourceFile] = tr return tr, sourceFile
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) linesOrNone = PyforaInspect.getlines(sourceFile) if linesOrNone is None: raise Exceptions.CantGetSourceTextError( "can't get source lines for file %s" % sourceFile) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(linesOrNone) return sourceFileCache_[sourceFile], sourceFile
def populateModuleMembers(self, path): if path in self.moduleClassesAndFunctionsByPath: return res = self.moduleClassesAndFunctionsByPath[path] = {} module = self.moduleForFile(path) if module is not None and self.canPopulateForPath(path): for leafItemName in module.__dict__: leafItemValue = module.__dict__[leafItemName] if PyforaInspect.isclass( leafItemValue) or PyforaInspect.isfunction( leafItemValue): try: sourcePath = PyforaInspect.getsourcefile(leafItemValue) if sourcePath is not None: if os.path.samefile(path, sourcePath): _, lineNumber = PyforaInspect.findsource( leafItemValue) lineNumberToUse = lineNumber + 1 if lineNumberToUse in res and res[ lineNumberToUse] is not leafItemValue: raise Exceptions.ForaToPythonConversionError( ("PythonObjectRehydratorHelpers got a line number collision at lineNumber %s" ", between %s and %s"), lineNumberToUse, leafItemValue, res[lineNumber + 1]) res[lineNumberToUse] = leafItemValue else: self.populateModuleMembers(sourcePath) except Exceptions.ForaToPythonConversionError: raise except PyforaInspect.PyforaInspectError: pass except IOError: #this gets raised when PyforaInspect can't find a file it needs pass except Exception as e: logging.critical( "PyforaInspect threw an exception: %s. tb = %s", e, traceback.format_exc())
def getSourceFilenameAndText(pyObject): try: sourceFile = PyforaInspect.getsourcefile(pyObject) except TypeError as e: raise Exceptions.CantGetSourceTextError(e.message) linesOrNone = PyforaInspect.getlines(sourceFile) if linesOrNone is None: raise Exceptions.CantGetSourceTextError( "can't get source lines for file %s" % sourceFile ) if sourceFile not in sourceFileCache_: sourceFileCache_[sourceFile] = "".join(linesOrNone) return sourceFileCache_[sourceFile], sourceFile
def populateModuleMembers(self, path): res = {} module = self.moduleForFile(path) if module is not None: for leafItemName in module.__dict__: leafItemValue = module.__dict__[leafItemName] if PyforaInspect.isclass(leafItemValue) or PyforaInspect.isfunction(leafItemValue): try: sourcePath = PyforaInspect.getsourcefile(leafItemValue) if os.path.samefile(path, sourcePath): _, lineNumber = PyforaInspect.findsource(leafItemValue) lineNumberToUse = lineNumber + 1 if lineNumberToUse in res: raise Exceptions.ForaToPythonConversionError( ("PythonObjectRehydrator got a line number collision at lineNumber %s" ", between %s and %s"), lineNumberToUse, leafItemValue, res[lineNumber + 1] ) res[lineNumberToUse] = leafItemValue else: self.populateModuleMembers(sourcePath) except Exceptions.ForaToPythonConversionError: raise except Exception as e: logging.critical("PyforaInspect threw an exception: %s. tb = %s", e, traceback.format_exc()) self.moduleClassesAndFunctionsByPath[path] = res
def getSourceFileAst(pyObject): filename = PyforaInspect.getsourcefile(pyObject) return getAstFromFilePath(filename)