<s> """<STR_LIT>""" <EOL> from mako import exceptions , util , compat <EOL> from mako . compat import StringIO <EOL> import operator <EOL> if compat . py3k : <EOL> reserved = set ( [ '<STR_LIT:True>' , '<STR_LIT:False>' , '<STR_LIT:None>' , '<STR_LIT>' ] ) <EOL> arg_id = operator . attrgetter ( '<STR_LIT>' ) <EOL> else : <EOL> reserved = set ( [ '<STR_LIT:True>' , '<STR_LIT:False>' , '<STR_LIT:None>' ] ) <EOL> arg_id = operator . attrgetter ( '<STR_LIT:id>' ) <EOL> try : <EOL> import _ast <EOL> util . restore__ast ( _ast ) <EOL> from mako import _ast_util <EOL> except ImportError : <EOL> _ast = None <EOL> from compiler import parse as compiler_parse <EOL> from compiler import visitor <EOL> def parse ( code , mode = '<STR_LIT>' , ** exception_kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if _ast : <EOL> return _ast_util . parse ( code , '<STR_LIT>' , mode ) <EOL> else : <EOL> if isinstance ( code , compat . text_type ) : <EOL> code = code . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> return compiler_parse ( code , mode ) <EOL> except Exception : <EOL> raise exceptions . SyntaxException ( <EOL> "<STR_LIT>" % ( <EOL> compat . exception_as ( ) . __class__ . __name__ , <EOL> compat . exception_as ( ) , <EOL> code [ <NUM_LIT:0> : <NUM_LIT:50> ] <EOL> ) , ** exception_kwargs ) <EOL> if _ast : <EOL> class FindIdentifiers ( _ast_util . NodeVisitor ) : <EOL> def __init__ ( self , listener , ** exception_kwargs ) : <EOL> self . in_function = False <EOL> self . in_assign_targets = False <EOL> self . local_ident_stack = set ( ) <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> def _add_declared ( self , name ) : <EOL> if not self . in_function : <EOL> self . listener . declared_identifiers . add ( name ) <EOL> else : <EOL> self . local_ident_stack . add ( name ) <EOL> def visit_ClassDef ( self , node ) : <EOL> self . _add_declared ( node . name ) <EOL> def visit_Assign ( self , node ) : <EOL> self . visit ( node . value ) <EOL> in_a = self . in_assign_targets <EOL> self . in_assign_targets = True <EOL> for n in node . targets : <EOL> self . visit ( n ) <EOL> self . in_assign_targets = in_a <EOL> if compat . py3k : <EOL> def visit_ExceptHandler ( self , node ) : <EOL> if node . name is not None : <EOL> self . _add_declared ( node . name ) <EOL> if node . type is not None : <EOL> self . listener . undeclared_identifiers . add ( node . type . id ) <EOL> for statement in node . body : <EOL> self . visit ( statement ) <EOL> def visit_Lambda ( self , node , * args ) : <EOL> self . _visit_function ( node , True ) <EOL> def visit_FunctionDef ( self , node ) : <EOL> self . _add_declared ( node . name ) <EOL> self . _visit_function ( node , False ) <EOL> def _expand_tuples ( self , args ) : <EOL> for arg in args : <EOL> if isinstance ( arg , _ast . Tuple ) : <EOL> for n in arg . elts : <EOL> yield n <EOL> else : <EOL> yield arg <EOL> def _visit_function ( self , node , islambda ) : <EOL> inf = self . in_function <EOL> self . in_function = True <EOL> local_ident_stack = self . local_ident_stack <EOL> self . local_ident_stack = local_ident_stack . union ( [ <EOL> arg_id ( arg ) for arg in self . _expand_tuples ( node . args . args ) <EOL> ] ) <EOL> if islambda : <EOL> self . visit ( node . body ) <EOL> else : <EOL> for n in node . body : <EOL> self . visit ( n ) <EOL> self . in_function = inf <EOL> self . local_ident_stack = local_ident_stack <EOL> def visit_For ( self , node ) : <EOL> self . visit ( node . iter ) <EOL> self . visit ( node . target ) <EOL> for statement in node . body : <EOL> self . visit ( statement ) <EOL> for statement in node . orelse : <EOL> self . visit ( statement ) <EOL> def visit_Name ( self , node ) : <EOL> if isinstance ( node . ctx , _ast . Store ) : <EOL> self . _add_declared ( node . id ) <EOL> elif node . id not in reserved and node . id not in self . listener . declared_identifiers and node . id not in self . local_ident_stack : <EOL> self . listener . undeclared_identifiers . add ( node . id ) <EOL> def visit_Import ( self , node ) : <EOL> for name in node . names : <EOL> if name . asname is not None : <EOL> self . _add_declared ( name . asname ) <EOL> else : <EOL> self . _add_declared ( name . name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) <EOL> def visit_ImportFrom ( self , node ) : <EOL> for name in node . names : <EOL> if name . asname is not None : <EOL> self . _add_declared ( name . asname ) <EOL> else : <EOL> if name . name == '<STR_LIT:*>' : <EOL> raise exceptions . CompileException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , ** self . exception_kwargs ) <EOL> self . _add_declared ( name . name ) <EOL> class FindTuple ( _ast_util . NodeVisitor ) : <EOL> def __init__ ( self , listener , code_factory , ** exception_kwargs ) : <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> self . code_factory = code_factory <EOL> def visit_Tuple ( self , node ) : <EOL> for n in node . elts : <EOL> p = self . code_factory ( n , ** self . exception_kwargs ) <EOL> self . listener . codeargs . append ( p ) <EOL> self . listener . args . append ( ExpressionGenerator ( n ) . value ( ) ) <EOL> self . listener . declared_identifiers = self . listener . declared_identifiers . union ( <EOL> p . declared_identifiers ) <EOL> self . listener . undeclared_identifiers = self . listener . undeclared_identifiers . union ( <EOL> p . undeclared_identifiers ) <EOL> class ParseFunc ( _ast_util . NodeVisitor ) : <EOL> def __init__ ( self , listener , ** exception_kwargs ) : <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> def visit_FunctionDef ( self , node ) : <EOL> self . listener . funcname = node . name <EOL> argnames = [ arg_id ( arg ) for arg in node . args . args ] <EOL> if node . args . vararg : <EOL> argnames . append ( node . args . vararg ) <EOL> if node . args . kwarg : <EOL> argnames . append ( node . args . kwarg ) <EOL> self . listener . argnames = argnames <EOL> self . listener . defaults = node . args . defaults <EOL> self . listener . varargs = node . args . vararg <EOL> self . listener . kwargs = node . args . kwarg <EOL> class ExpressionGenerator ( object ) : <EOL> def __init__ ( self , astnode ) : <EOL> self . generator = _ast_util . SourceGenerator ( '<STR_LIT:U+0020>' * <NUM_LIT:4> ) <EOL> self . generator . visit ( astnode ) <EOL> def value ( self ) : <EOL> return '<STR_LIT>' . join ( self . generator . result ) <EOL> else : <EOL> class FindIdentifiers ( object ) : <EOL> def __init__ ( self , listener , ** exception_kwargs ) : <EOL> self . in_function = False <EOL> self . local_ident_stack = set ( ) <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> def _add_declared ( self , name ) : <EOL> if not self . in_function : <EOL> self . listener . declared_identifiers . add ( name ) <EOL> else : <EOL> self . local_ident_stack . add ( name ) <EOL> def visitClass ( self , node , * args ) : <EOL> self . _add_declared ( node . name ) <EOL> def visitAssName ( self , node , * args ) : <EOL> self . _add_declared ( node . name ) <EOL> def visitAssign ( self , node , * args ) : <EOL> self . visit ( node . expr , * args ) <EOL> for n in node . nodes : <EOL> self . visit ( n , * args ) <EOL> def visitLambda ( self , node , * args ) : <EOL> self . _visit_function ( node , args ) <EOL> def visitFunction ( self , node , * args ) : <EOL> self . _add_declared ( node . name ) <EOL> self . _visit_function ( node , args ) <EOL> def _expand_tuples ( self , args ) : <EOL> for arg in args : <EOL> if isinstance ( arg , tuple ) : <EOL> for n in arg : <EOL> yield n <EOL> else : <EOL> yield arg <EOL> def _visit_function ( self , node , args ) : <EOL> inf = self . in_function <EOL> self . in_function = True <EOL> local_ident_stack = self . local_ident_stack <EOL> self . local_ident_stack = local_ident_stack . union ( [ <EOL> arg for arg in self . _expand_tuples ( node . argnames ) <EOL> ] ) <EOL> for n in node . getChildNodes ( ) : <EOL> self . visit ( n , * args ) <EOL> self . in_function = inf <EOL> self . local_ident_stack = local_ident_stack <EOL> def visitFor ( self , node , * args ) : <EOL> self . visit ( node . list , * args ) <EOL> self . visit ( node . assign , * args ) <EOL> self . visit ( node . body , * args ) <EOL> def visitName ( self , node , * args ) : <EOL> if node . name not in reserved and node . name not in self . listener . declared_identifiers and node . name not in self . local_ident_stack : <EOL> self . listener . undeclared_identifiers . add ( node . name ) <EOL> def visitImport ( self , node , * args ) : <EOL> for mod , alias in node . names : <EOL> if alias is not None : <EOL> self . _add_declared ( alias ) <EOL> else : <EOL> self . _add_declared ( mod . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) <EOL> def visitFrom ( self , node , * args ) : <EOL> for mod , alias in node . names : <EOL> if alias is not None : <EOL> self . _add_declared ( alias ) <EOL> else : <EOL> if mod == '<STR_LIT:*>' : <EOL> raise exceptions . CompileException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , ** self . exception_kwargs ) <EOL> self . _add_declared ( mod ) <EOL> def visit ( self , expr ) : <EOL> visitor . walk ( expr , self ) <EOL> class FindTuple ( object ) : <EOL> def __init__ ( self , listener , code_factory , ** exception_kwargs ) : <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> self . code_factory = code_factory <EOL> def visitTuple ( self , node , * args ) : <EOL> for n in node . nodes : <EOL> p = self . code_factory ( n , ** self . exception_kwargs ) <EOL> self . listener . codeargs . append ( p ) <EOL> self . listener . args . append ( ExpressionGenerator ( n ) . value ( ) ) <EOL> self . listener . declared_identifiers = self . listener . declared_identifiers . union ( <EOL> p . declared_identifiers ) <EOL> self . listener . undeclared_identifiers = self . listener . undeclared_identifiers . union ( <EOL> p . undeclared_identifiers ) <EOL> def visit ( self , expr ) : <EOL> visitor . walk ( expr , self ) <EOL> class ParseFunc ( object ) : <EOL> def __init__ ( self , listener , ** exception_kwargs ) : <EOL> self . listener = listener <EOL> self . exception_kwargs = exception_kwargs <EOL> def visitFunction ( self , node , * args ) : <EOL> self . listener . funcname = node . name <EOL> self . listener . argnames = node . argnames <EOL> self . listener . defaults = node . defaults <EOL> self . listener . varargs = node . varargs <EOL> self . listener . kwargs = node . kwargs <EOL> def visit ( self , expr ) : <EOL> visitor . walk ( expr , self ) <EOL> class ExpressionGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , astnode ) : <EOL> self . buf = StringIO ( ) <EOL> visitor . walk ( astnode , self ) <EOL> def value ( self ) : <EOL> return self . buf . getvalue ( ) <EOL> def operator ( self , op , node , * args ) : <EOL> self . buf . write ( '<STR_LIT:(>' ) <EOL> self . visit ( node . left , * args ) <EOL> self . buf . write ( '<STR_LIT>' % op ) <EOL> self . visit ( node . right , * args ) <EOL> self . buf . write ( '<STR_LIT:)>' ) <EOL> def booleanop ( self , op , node , * args ) : <EOL> self . visit ( node . nodes [ <NUM_LIT:0> ] ) <EOL> for n in node . nodes [ <NUM_LIT:1> : ] : <EOL> self . buf . write ( '<STR_LIT:U+0020>' + op + '<STR_LIT:U+0020>' ) <EOL> self . visit ( n , * args ) <EOL> def visitConst ( self , node , * args ) : <EOL> self . buf . write ( repr ( node . value ) ) <EOL> def visitAssName ( self , node , * args ) : <EOL> self . buf . write ( node . name ) <EOL> def visitName ( self , node , * args ) : <EOL> self . buf . write ( node . name ) <EOL> def visitMul ( self , node , * args ) : <EOL> self . operator ( '<STR_LIT:*>' , node , * args ) <EOL> def visitAnd ( self , node , * args ) : <EOL> self . booleanop ( '<STR_LIT>' , node , * args ) <EOL> def visitOr ( self , node , * args ) : <EOL> self . booleanop ( '<STR_LIT>' , node , * args ) <EOL> def visitBitand ( self , node , * args ) : <EOL> self . booleanop ( '<STR_LIT:&>' , node , * args ) <EOL> def visitBitor ( self , node , * args ) : <EOL> self . booleanop ( '<STR_LIT:|>' , node , * args ) <EOL> def visitBitxor ( self , node , * args ) : <EOL> self . booleanop ( '<STR_LIT>' , node , * args ) <EOL> def visitAdd ( self , node , * args ) : <EOL> self . operator ( '<STR_LIT:+>' , node , * args ) <EOL> def visitGetattr ( self , node , * args ) : <EOL> self . visit ( node . expr , * args ) <EOL> self . buf . write ( '<STR_LIT>' % node . attrname ) <EOL> def visitSub ( self , node , * args ) : <EOL> self . operator ( '<STR_LIT:->' , node , * args ) <EOL> def visitNot ( self , node , * args ) : <EOL> self . buf . write ( '<STR_LIT>' ) <EOL> self . visit ( node . expr ) <EOL> def visitDiv ( self , node , * args ) : <EOL> self . operator ( '<STR_LIT:/>' , node , * args ) <EOL> def visitFloorDiv ( self , node , * args ) : <EOL> self . operator ( '<STR_LIT>' , node , * args ) <EOL> def visitSubscript ( self , node , * args ) : <EOL> self . visit ( node . expr ) <EOL> self . buf . write ( '<STR_LIT:[>' ) <EOL> [ self . visit ( x ) for x in node . subs ] <EOL> self . buf . write ( '<STR_LIT:]>' ) <EOL> def visitUnarySub ( self , node , * args ) : <EOL> self . buf . write ( '<STR_LIT:->' ) <EOL> self . visit ( node . expr ) <EOL> def visitUnaryAdd ( self , node , * args ) : <EOL> self . buf . write ( '<STR_LIT:->' ) <EOL> self . visit ( node . expr ) <EOL> def visitSlice ( self , node , * args ) : <EOL> self . visit ( node . expr ) <EOL> self . buf . write ( '<STR_LIT:[>' ) <EOL> if node . lower is not None : <EOL> self . visit ( node . lower ) <EOL> self . buf . write ( '<STR_LIT::>' ) <EOL> if node . upper is not None : <EOL> self . visit ( node . upper ) <EOL> self . buf . write ( '<STR_LIT:]>' ) <EOL> def visitDict ( self , node ) : <EOL> self . buf . write ( '<STR_LIT:{>' ) <EOL> c = node . getChildren ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( c ) , <NUM_LIT:2> ) : <EOL> self . visit ( c [ i ] ) <EOL> self . buf . write ( '<STR_LIT>' ) <EOL> self . visit ( c [ i + <NUM_LIT:1> ] ) <EOL> if i < len ( c ) - <NUM_LIT:2> : <EOL> self . buf . write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . buf . write ( '<STR_LIT:}>' ) <EOL> def visitTuple ( self , node ) : <EOL> self . buf . write ( '<STR_LIT:(>' ) <EOL> c = node . getChildren ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( c ) ) : <EOL> self . visit ( c [ i ] ) <EOL> if i < len ( c ) - <NUM_LIT:1> : <EOL> self . buf . write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . buf . write ( '<STR_LIT:)>' ) <EOL> def visitList ( self , node ) : <EOL> self . buf . write ( '<STR_LIT:[>' ) <EOL> c = node . getChildren ( ) <EOL> for i in range ( <NUM_LIT:0> , len ( c ) ) : <EOL> self . visit ( c [ i ] ) <EOL> if i < len ( c ) - <NUM_LIT:1> : <EOL> self . buf . write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . buf . write ( '<STR_LIT:]>' ) <EOL> def visitListComp ( self , node ) : <EOL> self . buf . write ( '<STR_LIT:[>' ) <EOL> self . visit ( node . expr ) <EOL> self . buf . write ( '<STR_LIT:U+0020>' ) <EOL> for n in node . quals : <EOL> self . visit ( n ) <EOL> self . buf . write ( '<STR_LIT:]>' ) <EOL> def visitListCompFor ( self , node ) : <EOL> self . buf . write ( '<STR_LIT>' ) <EOL> self . visit ( node . assign ) <EOL> self . buf . write ( '<STR_LIT>' ) <EOL> self . visit ( node . list ) <EOL> for n in node . ifs : <EOL> self . visit ( n ) <EOL> def visitListCompIf ( self , node ) : <EOL> self . buf . write ( '<STR_LIT>' ) <EOL> self . visit ( node . test ) <EOL> def visitCompare ( self , node ) : <EOL> self . visit ( node . expr ) <EOL> for tup in node . ops : <EOL> self . buf . write ( tup [ <NUM_LIT:0> ] ) <EOL> self . visit ( tup [ <NUM_LIT:1> ] ) <EOL> def visitCallFunc ( self , node , * args ) : <EOL> self . visit ( node . node ) <EOL> self . buf . write ( '<STR_LIT:(>' ) <EOL> if len ( node . args ) : <EOL> self . visit ( node . args [ <NUM_LIT:0> ] ) <EOL> for a in node . args [ <NUM_LIT:1> : ] : <EOL> self . buf . write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . visit ( a ) <EOL> self . buf . write ( '<STR_LIT:)>' ) <EOL> class walker ( visitor . ASTVisitor ) : <EOL> def dispatch ( self , node , * args ) : <EOL> print ( '<STR_LIT>' , str ( node ) ) <EOL> return visitor . ASTVisitor . dispatch ( self , node , * args ) </s>
