관리-도구
편집 파일: _ast_util.cpython-37.pyc
B �C]�O � @ sr d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd lm Z dd lm Z ddlmZ ddlmZ dd lm Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd lm Z dd!l!m"Z" ed"ed#iZ#ed$ed%ed&ed'e d(ed)ed*ed+ed,ed-ed.iZ$e d/ed0ed1ed2ed3ed4ed5ed6ed7ed8i Z%ed9ed:ed$e d%iZ&i Z'e'�(e#� e'�(e$� e'�(e%� e'�(e&� dHd=d>�Z)d?d@� Z*G dAdB� dBe+�Z,G dCdD� dDe,�Z-G dEdF� dFe,�Z.dGS )Iz� ast ~~~ This is a stripped down version of Armin Ronacher's ast module. :copyright: Copyright 2008 by Armin Ronacher. :license: Python License. � )�Add)�And)�AST)�BitAnd)�BitOr)�BitXor)�Div)�Eq)�FloorDiv)�Gt)�GtE)�If)�In)�Invert)�Is)�IsNot)�LShift)�Lt)�LtE)�Mod)�Mult)�Name)�Not)�NotEq)�NotIn)�Or)� PyCF_ONLY_AST)�RShift)�Sub)�UAdd)�USub)�arg_stringname�and�or�+�-�*�/z//�%z<<z>>�|�&�^z==�>z>=�in�iszis not�<z<=z!=znot in�~�not� <unknown>�execc C s t | ||t�S )z%Parse an expression into an AST node.)�compiler )�expr�filename�mode� r8 �?/opt/alt/python37/lib/python3.7/site-packages/mako/_ast_util.py�parseZ s r: c c sR t | d�r| jsdS x8| jD ].}y|t| |�fV W q tk rH Y qX qW dS )zAIterate over all fields of a node, only yielding existing fields.�_fieldsN)�hasattrr; �getattr�AttributeError)�node�fieldr8 r8 r9 �iter_fields_ s rA c @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �NodeVisitora� Walks the abstract syntax tree and call visitor functions for every node found. The visitor functions may return values which will be forwarded by the `visit` method. Per default the visitor functions for the nodes are ``'visit_'`` + class name of the node. So a `TryFinally` node visit function would be `visit_TryFinally`. This behavior can be changed by overriding the `get_visitor` function. If no visitor function exists for a node (return value `None`) the `generic_visit` visitor is used instead. Don't use the `NodeVisitor` if you want to apply changes to nodes during traversing. For this a special visitor exists (`NodeTransformer`) that allows modifications. c C s d|j j }t| |d�S )z� Return the visitor function for this node or `None` if no visitor exists for this node. In that case the generic visit function is used instead. �visit_N)� __class__�__name__r= )�selfr? �methodr8 r8 r9 �get_visitor} s zNodeVisitor.get_visitorc C s$ | � |�}|dk r||�S | �|�S )z Visit a node.N)rH � generic_visit)rF r? �fr8 r8 r9 �visit� s zNodeVisitor.visitc C s\ xVt |�D ]J\}}t|t�r@x6|D ]}t|t�r"| �|� q"W q t|t�r | �|� q W dS )z9Called if no explicit visitor function exists for a node.N)rA � isinstance�listr rK )rF r? r@ �value�itemr8 r8 r9 rI � s zNodeVisitor.generic_visitN)rE � __module__�__qualname__�__doc__rH rK rI r8 r8 r8 r9 rB k s rB c @ s e Zd ZdZdd� ZdS )�NodeTransformera Walks the abstract syntax tree and allows modifications of nodes. The `NodeTransformer` will walk the AST and use the return value of the visitor functions to replace or remove the old node. If the return value of the visitor function is `None` the node will be removed from the previous location otherwise it's replaced with the return value. The return value may be the original node in which case no replacement takes place. Here an example transformer that rewrites all `foo` to `data['foo']`:: class RewriteName(NodeTransformer): def visit_Name(self, node): return copy_location(Subscript( value=Name(id='data', ctx=Load()), slice=Index(value=Str(s=node.id)), ctx=node.ctx ), node) Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the generic visit function for the node first. Nodes that were part of a collection of statements (that applies to all statement nodes) may also return a list of nodes rather than just a single node. Usually you use the transformer like this:: node = YourTransformer().visit(node) c C s� x�t |�D ]�\}}t||d �}t|t�r�g }xL|D ]D}t|t�rl| �|�}|d krVq2nt|t�sl|�|� q2|�|� q2W ||d d �<