コード例 #1
0
 def unparseStmt(tree):
     sio = io.StringIO()
     astunparse.Unparser(tree, sio)
     
     s = sio.getvalue().split("\n")[1]
                                   
     return s
コード例 #2
0
 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
コード例 #3
0
ファイル: cmd.py プロジェクト: engeg/recipes-py
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()
コード例 #4
0
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
コード例 #5
0
 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)
コード例 #6
0
def codegen(node):
    astunparse.Unparser(node, sys.stdout)
コード例 #7
0
ファイル: doc.py プロジェクト: Kryndex/recipes-py
def _unparse(node):
    buf = StringIO()
    astunparse.Unparser(node, buf)
    return buf.getvalue()