<s> """<STR_LIT>""" <EOL> from _ast import * <EOL> from _ast import __version__ <EOL> def parse ( expr , filename = '<STR_LIT>' , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return compile ( expr , filename , mode , PyCF_ONLY_AST ) <EOL> def literal_eval ( node_or_string ) : <EOL> """<STR_LIT>""" <EOL> _safe_names = { '<STR_LIT:None>' : None , '<STR_LIT:True>' : True , '<STR_LIT:False>' : False } <EOL> if isinstance ( node_or_string , basestring ) : <EOL> node_or_string = parse ( node_or_string , mode = '<STR_LIT>' ) <EOL> if isinstance ( node_or_string , Expression ) : <EOL> node_or_string = node_or_string . body <EOL> def _convert ( node ) : <EOL> if isinstance ( node , Str ) : <EOL> return node . s <EOL> elif isinstance ( node , Num ) : <EOL> return node . n <EOL> elif isinstance ( node , Tuple ) : <EOL> return tuple ( map ( _convert , node . elts ) ) <EOL> elif isinstance ( node , List ) : <EOL> return list ( map ( _convert , node . elts ) ) <EOL> elif isinstance ( node , Dict ) : <EOL> return dict ( ( _convert ( k ) , _convert ( v ) ) for k , v <EOL> in zip ( node . keys , node . values ) ) <EOL> elif isinstance ( node , Name ) : <EOL> if node . id in _safe_names : <EOL> return _safe_names [ node . id ] <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return _convert ( node_or_string ) <EOL> def dump ( node , annotate_fields = True , include_attributes = False ) : <EOL> """<STR_LIT>""" <EOL> def _format ( node ) : <EOL> if isinstance ( node , AST ) : <EOL> fields = [ ( a , _format ( b ) ) for a , b in iter_fields ( node ) ] <EOL> rv = '<STR_LIT>' % ( node . __class__ . __name__ , '<STR_LIT:U+002CU+0020>' . join ( <EOL> ( '<STR_LIT>' % field for field in fields ) <EOL> if annotate_fields else <EOL> ( b for a , b in fields ) <EOL> ) ) <EOL> if include_attributes and node . _attributes : <EOL> rv += fields and '<STR_LIT:U+002CU+0020>' or '<STR_LIT:U+0020>' <EOL> rv += '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % ( a , _format ( getattr ( node , a ) ) ) <EOL> for a in node . _attributes ) <EOL> return rv + '<STR_LIT:)>' <EOL> elif isinstance ( node , list ) : <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( _format ( x ) for x in node ) <EOL> return repr ( node ) <EOL> if not isinstance ( node , AST ) : <EOL> raise TypeError ( '<STR_LIT>' % node . __class__ . __name__ ) <EOL> return _format ( node ) <EOL> def copy_location ( new_node , old_node ) : <EOL> """<STR_LIT>""" <EOL> for attr in '<STR_LIT>' , '<STR_LIT>' : <EOL> if attr in old_node . _attributes and attr in new_node . _attributes and hasattr ( old_node , attr ) : <EOL> setattr ( new_node , attr , getattr ( old_node , attr ) ) <EOL> return new_node <EOL> def fix_missing_locations ( node ) : <EOL> """<STR_LIT>""" <EOL> def _fix ( node , lineno , col_offset ) : <EOL> if '<STR_LIT>' in node . _attributes : <EOL> if not hasattr ( node , '<STR_LIT>' ) : <EOL> node . lineno = lineno <EOL> else : <EOL> lineno = node . lineno <EOL> if '<STR_LIT>' in node . _attributes : <EOL> if not hasattr ( node , '<STR_LIT>' ) : <EOL> node . col_offset = col_offset <EOL> else : <EOL> col_offset = node . col_offset <EOL> for child in iter_child_nodes ( node ) : <EOL> _fix ( child , lineno , col_offset ) <EOL> _fix ( node , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> return node <EOL> def increment_lineno ( node , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in node . _attributes : <EOL> node . lineno = getattr ( node , '<STR_LIT>' , <NUM_LIT:0> ) + n <EOL> for child in walk ( node ) : <EOL> if '<STR_LIT>' in child . _attributes : <EOL> child . lineno = getattr ( child , '<STR_LIT>' , <NUM_LIT:0> ) + n <EOL> return node <EOL> def iter_fields ( node ) : <EOL> """<STR_LIT>""" <EOL> for field in node . _fields : <EOL> try : <EOL> yield field , getattr ( node , field ) <EOL> except AttributeError : <EOL> pass <EOL> def iter_child_nodes ( node ) : <EOL> """<STR_LIT>""" <EOL> for name , field in iter_fields ( node ) : <EOL> if isinstance ( field , AST ) : <EOL> yield field <EOL> elif isinstance ( field , list ) : <EOL> for item in field : <EOL> if isinstance ( item , AST ) : <EOL> yield item <EOL> def get_docstring ( node , clean = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( node , ( FunctionDef , ClassDef , Module ) ) : <EOL> raise TypeError ( "<STR_LIT>" % node . __class__ . __name__ ) <EOL> if node . body and isinstance ( node . body [ <NUM_LIT:0> ] , Expr ) and isinstance ( node . body [ <NUM_LIT:0> ] . value , Str ) : <EOL> if clean : <EOL> import inspect <EOL> return inspect . cleandoc ( node . body [ <NUM_LIT:0> ] . value . s ) <EOL> return node . body [ <NUM_LIT:0> ] . value . s <EOL> def walk ( node ) : <EOL> """<STR_LIT>""" <EOL> from collections import deque <EOL> todo = deque ( [ node ] ) <EOL> while todo : <EOL> node = todo . popleft ( ) <EOL> todo . extend ( iter_child_nodes ( node ) ) <EOL> yield node <EOL> class NodeVisitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT>' + node . __class__ . __name__ <EOL> visitor = getattr ( self , method , self . generic_visit ) <EOL> return visitor ( node ) <EOL> def generic_visit ( self , node ) : <EOL> """<STR_LIT>""" <EOL> for field , value in iter_fields ( node ) : <EOL> if isinstance ( value , list ) : <EOL> for item in value : <EOL> if isinstance ( item , AST ) : <EOL> self . visit ( item ) <EOL> elif isinstance ( value , AST ) : <EOL> self . visit ( value ) <EOL> class NodeTransformer ( NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def generic_visit ( self , node ) : <EOL> for field , old_value in iter_fields ( node ) : <EOL> old_value = getattr ( node , field , None ) <EOL> if isinstance ( old_value , list ) : <EOL> new_values = [ ] <EOL> for value in old_value : <EOL> if isinstance ( value , AST ) : <EOL> value = self . visit ( value ) <EOL> if value is None : <EOL> continue <EOL> elif not isinstance ( value , AST ) : <EOL> new_values . extend ( value ) <EOL> continue <EOL> new_values . append ( value ) <EOL> old_value [ : ] = new_values <EOL> elif isinstance ( old_value , AST ) : <EOL> new_node = self . visit ( old_value ) <EOL> if new_node is None : <EOL> delattr ( node , field ) <EOL> else : <EOL> setattr ( node , field , new_node ) <EOL> return node </s>
