<s> """<STR_LIT>""" <EOL> import builtins <EOL> from textwrap import dedent <EOL> from types import CodeType <EOL> from genshi . core import Markup <EOL> from genshi . template . astutil import ASTTransformer , ASTCodeGenerator , _ast , parse <EOL> from genshi . template . base import TemplateRuntimeError <EOL> from genshi . util import flatten <EOL> from genshi . compat import get_code_params , build_code_chunk , IS_PYTHON2 <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> __docformat__ = '<STR_LIT>' <EOL> has_star_import_bug = False <EOL> try : <EOL> class _FakeMapping ( object ) : <EOL> __getitem__ = __setitem__ = lambda * a : None <EOL> exec ( '<STR_LIT>' , { } , _FakeMapping ( ) ) <EOL> except SystemError : <EOL> has_star_import_bug = True <EOL> del _FakeMapping <EOL> def _star_import_patch ( mapping , modname ) : <EOL> """<STR_LIT>""" <EOL> module = __import__ ( modname , None , None , [ '<STR_LIT>' ] ) <EOL> if hasattr ( module , '<STR_LIT>' ) : <EOL> members = module . __all__ <EOL> else : <EOL> members = [ x for x in module . __dict__ if not x . startswith ( '<STR_LIT:_>' ) ] <EOL> mapping . update ( [ ( name , getattr ( module , name ) ) for name in members ] ) <EOL> class Code ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT:source>' , '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , source , filename = None , lineno = - <NUM_LIT:1> , lookup = '<STR_LIT:strict>' , <EOL> xform = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( source , str ) : <EOL> self . source = source <EOL> node = _parse ( source , mode = self . mode ) <EOL> else : <EOL> assert isinstance ( source , _ast . AST ) , '<STR_LIT>' % source <EOL> self . source = '<STR_LIT:?>' <EOL> if self . mode == '<STR_LIT>' : <EOL> node = _ast . Expression ( ) <EOL> node . body = source <EOL> else : <EOL> node = _ast . Module ( ) <EOL> node . body = [ source ] <EOL> self . ast = node <EOL> self . code = _compile ( node , self . source , mode = self . mode , <EOL> filename = filename , lineno = lineno , xform = xform ) <EOL> if lookup is None : <EOL> lookup = LenientLookup <EOL> elif isinstance ( lookup , str ) : <EOL> lookup = { '<STR_LIT>' : LenientLookup , '<STR_LIT:strict>' : StrictLookup } [ lookup ] <EOL> self . _globals = lookup . globals <EOL> def __getstate__ ( self ) : <EOL> state = { '<STR_LIT:source>' : self . source , '<STR_LIT>' : self . ast , <EOL> '<STR_LIT>' : self . _globals . __self__ } <EOL> state [ '<STR_LIT:code>' ] = get_code_params ( self . code ) <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> self . source = state [ '<STR_LIT:source>' ] <EOL> self . ast = state [ '<STR_LIT>' ] <EOL> self . code = CodeType ( <NUM_LIT:0> , * state [ '<STR_LIT:code>' ] ) <EOL> self . _globals = state [ '<STR_LIT>' ] . globals <EOL> def __eq__ ( self , other ) : <EOL> return ( type ( other ) == type ( self ) ) and ( self . code == other . code ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . code ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . source ) <EOL> class Expression ( Code ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> mode = '<STR_LIT>' <EOL> def evaluate ( self , data ) : <EOL> """<STR_LIT>""" <EOL> __traceback_hide__ = '<STR_LIT>' <EOL> _globals = self . _globals ( data ) <EOL> return eval ( self . code , _globals , { '<STR_LIT>' : data } ) <EOL> class Suite ( Code ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> mode = '<STR_LIT>' <EOL> def execute ( self , data ) : <EOL> """<STR_LIT>""" <EOL> __traceback_hide__ = '<STR_LIT>' <EOL> _globals = self . _globals ( data ) <EOL> exec ( self . code , _globals , data ) <EOL> UNDEFINED = object ( ) <EOL> class UndefinedError ( TemplateRuntimeError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , owner = UNDEFINED ) : <EOL> if owner is not UNDEFINED : <EOL> message = '<STR_LIT>' % ( repr ( owner ) , name ) <EOL> else : <EOL> message = '<STR_LIT>' % name <EOL> TemplateRuntimeError . __init__ ( self , message ) <EOL> class Undefined ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , name , owner = UNDEFINED ) : <EOL> """<STR_LIT>""" <EOL> self . _name = name <EOL> self . _owner = owner <EOL> def __iter__ ( self ) : <EOL> return iter ( [ ] ) <EOL> def __bool__ ( self ) : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . _name ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def _die ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> __traceback_hide__ = True <EOL> raise UndefinedError ( self . _name , self . _owner ) <EOL> __call__ = __getattr__ = __getitem__ = _die <EOL> __length_hint__ = None <EOL> class LookupBase ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def globals ( cls , data ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : data , <EOL> '<STR_LIT>' : cls . lookup_name , <EOL> '<STR_LIT>' : cls . lookup_attr , <EOL> '<STR_LIT>' : cls . lookup_item , <EOL> '<STR_LIT>' : _star_import_patch , <EOL> '<STR_LIT>' : UndefinedError , <EOL> } <EOL> @ classmethod <EOL> def lookup_name ( cls , data , name ) : <EOL> __traceback_hide__ = True <EOL> val = data . get ( name , UNDEFINED ) <EOL> if val is UNDEFINED : <EOL> val = BUILTINS . get ( name , val ) <EOL> if val is UNDEFINED : <EOL> val = cls . undefined ( name ) <EOL> return val <EOL> @ classmethod <EOL> def lookup_attr ( cls , obj , key ) : <EOL> __traceback_hide__ = True <EOL> try : <EOL> val = getattr ( obj , key ) <EOL> except AttributeError : <EOL> if hasattr ( obj . __class__ , key ) : <EOL> raise <EOL> else : <EOL> try : <EOL> val = obj [ key ] <EOL> except ( KeyError , TypeError ) : <EOL> val = cls . undefined ( key , owner = obj ) <EOL> return val <EOL> @ classmethod <EOL> def lookup_item ( cls , obj , key ) : <EOL> __traceback_hide__ = True <EOL> if len ( key ) == <NUM_LIT:1> : <EOL> key = key [ <NUM_LIT:0> ] <EOL> try : <EOL> return obj [ key ] <EOL> except ( AttributeError , KeyError , IndexError , TypeError ) as e : <EOL> if isinstance ( key , str ) : <EOL> val = getattr ( obj , key , UNDEFINED ) <EOL> if val is UNDEFINED : <EOL> val = cls . undefined ( key , owner = obj ) <EOL> return val <EOL> raise <EOL> @ classmethod <EOL> def undefined ( cls , key , owner = UNDEFINED ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class LenientLookup ( LookupBase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def undefined ( cls , key , owner = UNDEFINED ) : <EOL> """<STR_LIT>""" <EOL> __traceback_hide__ = True <EOL> return Undefined ( key , owner = owner ) <EOL> class StrictLookup ( LookupBase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def undefined ( cls , key , owner = UNDEFINED ) : <EOL> """<STR_LIT>""" <EOL> __traceback_hide__ = True <EOL> raise UndefinedError ( key , owner = owner ) <EOL> def _parse ( source , mode = '<STR_LIT>' ) : <EOL> source = source . strip ( ) <EOL> if mode == '<STR_LIT>' : <EOL> lines = [ line . expandtabs ( ) for line in source . splitlines ( ) ] <EOL> if lines : <EOL> first = lines [ <NUM_LIT:0> ] <EOL> rest = dedent ( '<STR_LIT:\n>' . join ( lines [ <NUM_LIT:1> : ] ) ) . rstrip ( ) <EOL> if first . rstrip ( ) . endswith ( '<STR_LIT::>' ) and not rest [ <NUM_LIT:0> ] . isspace ( ) : <EOL> rest = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' % line for line in rest . splitlines ( ) ] ) <EOL> source = '<STR_LIT:\n>' . join ( [ first , rest ] ) <EOL> if isinstance ( source , str ) : <EOL> source = ( '<STR_LIT>' + source ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return parse ( source , mode ) <EOL> def _compile ( node , source = None , mode = '<STR_LIT>' , filename = None , lineno = - <NUM_LIT:1> , <EOL> xform = None ) : <EOL> if not filename : <EOL> filename = '<STR_LIT>' <EOL> if IS_PYTHON2 : <EOL> if isinstance ( filename , str ) : <EOL> filename = filename . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> else : <EOL> if not isinstance ( filename , str ) : <EOL> filename = filename . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> if lineno <= <NUM_LIT:0> : <EOL> lineno = <NUM_LIT:1> <EOL> if xform is None : <EOL> xform = { <EOL> '<STR_LIT>' : ExpressionASTTransformer <EOL> } . get ( mode , TemplateASTTransformer ) <EOL> tree = xform ( ) . visit ( node ) <EOL> if mode == '<STR_LIT>' : <EOL> name = '<STR_LIT>' % ( source or '<STR_LIT:?>' ) <EOL> else : <EOL> lines = source . splitlines ( ) <EOL> if not lines : <EOL> extract = '<STR_LIT>' <EOL> else : <EOL> extract = lines [ <NUM_LIT:0> ] <EOL> if len ( lines ) > <NUM_LIT:1> : <EOL> extract += '<STR_LIT>' <EOL> name = '<STR_LIT>' % ( extract ) <EOL> new_source = ASTCodeGenerator ( tree ) . code <EOL> code = compile ( new_source , filename , mode ) <EOL> try : <EOL> return build_code_chunk ( code , filename , name , lineno ) <EOL> except RuntimeError : <EOL> return code <EOL> def _new ( class_ , * args , ** kwargs ) : <EOL> ret = class_ ( ) <EOL> for attr , value in zip ( ret . _fields , args ) : <EOL> if attr in kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> setattr ( ret , attr , value ) <EOL> for attr , value in kwargs : <EOL> setattr ( ret , attr , value ) <EOL> return ret <EOL> BUILTINS = builtins . __dict__ . copy ( ) <EOL> BUILTINS . update ( { '<STR_LIT>' : Markup , '<STR_LIT>' : Undefined } ) <EOL> CONSTANTS = frozenset ( [ '<STR_LIT:False>' , '<STR_LIT:True>' , '<STR_LIT:None>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class TemplateASTTransformer ( ASTTransformer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . locals = [ CONSTANTS ] <EOL> def _extract_names ( self , node ) : <EOL> names = set ( ) <EOL> def _process ( node ) : <EOL> if not IS_PYTHON2 and isinstance ( node , _ast . arg ) : <EOL> names . add ( node . arg ) <EOL> if isinstance ( node , _ast . Name ) : <EOL> names . add ( node . id ) <EOL> elif isinstance ( node , _ast . alias ) : <EOL> names . add ( node . asname or node . name ) <EOL> elif isinstance ( node , _ast . Tuple ) : <EOL> for elt in node . elts : <EOL> _process ( elt ) <EOL> if hasattr ( node , '<STR_LIT:args>' ) : <EOL> for arg in node . args : <EOL> _process ( arg ) <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> names . add ( node . vararg ) <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> names . add ( node . kwarg ) <EOL> elif hasattr ( node , '<STR_LIT>' ) : <EOL> for elt in node . names : <EOL> _process ( elt ) <EOL> return names <EOL> def visit_Str ( self , node ) : <EOL> if not isinstance ( node . s , str ) : <EOL> try : <EOL> node . s . decode ( '<STR_LIT:ascii>' ) <EOL> except ValueError : <EOL> return _new ( _ast . Str , node . s . decode ( '<STR_LIT:utf-8>' ) ) <EOL> return node <EOL> def visit_ClassDef ( self , node ) : <EOL> if len ( self . locals ) > <NUM_LIT:1> : <EOL> self . locals [ - <NUM_LIT:1> ] . add ( node . name ) <EOL> self . locals . append ( set ( ) ) <EOL> try : <EOL> return ASTTransformer . visit_ClassDef ( self , node ) <EOL> finally : <EOL> self . locals . pop ( ) <EOL> def visit_Import ( self , node ) : <EOL> if len ( self . locals ) > <NUM_LIT:1> : <EOL> self . locals [ - <NUM_LIT:1> ] . update ( self . _extract_names ( node ) ) <EOL> return ASTTransformer . visit_Import ( self , node ) <EOL> def visit_ImportFrom ( self , node ) : <EOL> if [ a . name for a in node . names ] == [ '<STR_LIT:*>' ] : <EOL> if has_star_import_bug : <EOL> node = _new ( _ast . Expr , _new ( _ast . Call , <EOL> _new ( _ast . Name , '<STR_LIT>' ) , [ <EOL> _new ( _ast . Name , '<STR_LIT>' ) , <EOL> _new ( _ast . Str , node . module ) <EOL> ] , ( ) , ( ) ) ) <EOL> return node <EOL> if len ( self . locals ) > <NUM_LIT:1> : <EOL> self . locals [ - <NUM_LIT:1> ] . update ( self . _extract_names ( node ) ) <EOL> return ASTTransformer . visit_ImportFrom ( self , node ) <EOL> def visit_FunctionDef ( self , node ) : <EOL> if len ( self . locals ) > <NUM_LIT:1> : <EOL> self . locals [ - <NUM_LIT:1> ] . add ( node . name ) <EOL> self . locals . append ( self . _extract_names ( node . args ) ) <EOL> try : <EOL> return ASTTransformer . visit_FunctionDef ( self , node ) <EOL> finally : <EOL> self . locals . pop ( ) <EOL> def visit_GeneratorExp ( self , node ) : <EOL> gens = [ ] <EOL> for generator in node . generators : <EOL> self . locals . append ( set ( ) ) <EOL> gen = _new ( _ast . comprehension , self . visit ( generator . target ) , <EOL> self . visit ( generator . iter ) , <EOL> [ self . visit ( if_ ) for if_ in generator . ifs ] ) <EOL> gens . append ( gen ) <EOL> ret = _new ( node . __class__ , self . visit ( node . elt ) , gens ) <EOL> del self . locals [ - len ( node . generators ) : ] <EOL> return ret <EOL> visit_ListComp = visit_GeneratorExp <EOL> def visit_Lambda ( self , node ) : <EOL> self . locals . append ( self . _extract_names ( node . args ) ) <EOL> try : <EOL> return ASTTransformer . visit_Lambda ( self , node ) <EOL> finally : <EOL> self . locals . pop ( ) <EOL> def visit_Name ( self , node ) : <EOL> if isinstance ( node . ctx , _ast . Load ) and node . id not in flatten ( self . locals ) : <EOL> name = _new ( _ast . Name , '<STR_LIT>' , _ast . Load ( ) ) <EOL> namearg = _new ( _ast . Name , '<STR_LIT>' , _ast . Load ( ) ) <EOL> strarg = _new ( _ast . Str , node . id ) <EOL> node = _new ( _ast . Call , name , [ namearg , strarg ] , [ ] ) <EOL> elif isinstance ( node . ctx , _ast . Store ) : <EOL> if len ( self . locals ) > <NUM_LIT:1> : <EOL> self . locals [ - <NUM_LIT:1> ] . add ( node . id ) <EOL> return node <EOL> class ExpressionASTTransformer ( TemplateASTTransformer ) : <EOL> """<STR_LIT>""" <EOL> def visit_Attribute ( self , node ) : <EOL> if not isinstance ( node . ctx , _ast . Load ) : <EOL> return ASTTransformer . visit_Attribute ( self , node ) <EOL> func = _new ( _ast . Name , '<STR_LIT>' , _ast . Load ( ) ) <EOL> args = [ self . visit ( node . value ) , _new ( _ast . Str , node . attr ) ] <EOL> return _new ( _ast . Call , func , args , [ ] ) <EOL> def visit_Subscript ( self , node ) : <EOL> if not isinstance ( node . ctx , _ast . Load ) or not isinstance ( node . slice , _ast . Index ) : <EOL> return ASTTransformer . visit_Subscript ( self , node ) <EOL> func = _new ( _ast . Name , '<STR_LIT>' , _ast . Load ( ) ) <EOL> args = [ <EOL> self . visit ( node . value ) , <EOL> _new ( _ast . Tuple , ( self . visit ( node . slice . value ) , ) , _ast . Load ( ) ) <EOL> ] <EOL> return _new ( _ast . Call , func , args , [ ] ) </s>
