def unparseStmt(tree): sio = io.StringIO() astunparse.Unparser(tree, sio) s = sio.getvalue().split("\n")[1] return s
def unparseExpr(tree): if type(tree).__name__ == 'Str': return tree.s else: sio = io.StringIO() astunparse.Unparser(tree, sio) s = sio.getvalue().split("\n")[0] return s
def _unparse(node): """Prints Python code which could parse to `node`. Args: * node (ast.AST) - The AST to produce Python code for. Returns a str with the formatted code. """ assert isinstance(node, ast.AST), type(node) buf = StringIO() astunparse.Unparser(node, buf) return buf.getvalue()
def make_metal(*, path: Path, content: str = None) -> str: """Autoformat the given file in place. """ if content is None: content = path.read_text(encoding=ENCODING) tree = compile(content, str(path), 'exec', ast.PyCF_ONLY_AST) with StringIO() as buffer: astunparse.Unparser(tree=tree, file=buffer) buffer.seek(0) content = buffer.read() content = autopep8.fix_code(source=content) return content
def generic_visit(self, node): if type(node).__name__ == 'ClassDef': self.isDef = True self.className = node.name else: self.isDef = False # 클래스 선언이 시작되면 클래스 별 저장 if self.isDef == True: fileName = self.filePath if not os.path.exists(self.filePath): os.mkdir(self.filePath) fileName += "/" + self.className fileName += ".py" f = open(fileName, 'w+t') sio = io.StringIO() astunparse.Unparser(node.body, sio) f.write(sio.getvalue()) f.close() ast.NodeVisitor.generic_visit(self, node)
def codegen(node): astunparse.Unparser(node, sys.stdout)
def _unparse(node): buf = StringIO() astunparse.Unparser(node, buf) return buf.getvalue()