<s> from __future__ import unicode_literals <EOL> from functools import wraps <EOL> from django import template <EOL> from django . template . context import Context <EOL> from django . template . loader import get_template , select_template <EOL> from mezzanine . utils . device import templates_for_device <EOL> class Library ( template . Library ) : <EOL> """<STR_LIT>""" <EOL> def as_tag ( self , tag_func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( tag_func ) <EOL> def tag_wrapper ( parser , token ) : <EOL> class AsTagNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> parts = token . split_contents ( ) <EOL> def resolve ( arg ) : <EOL> try : <EOL> return template . Variable ( arg ) . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return arg <EOL> args , kwargs = [ ] , { } <EOL> for arg in parts [ <NUM_LIT:1> : - <NUM_LIT:2> ] : <EOL> if "<STR_LIT:=>" in arg : <EOL> name , val = arg . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> if name in tag_func . __code__ . co_varnames : <EOL> kwargs [ name ] = resolve ( val ) <EOL> continue <EOL> args . append ( resolve ( arg ) ) <EOL> context [ parts [ - <NUM_LIT:1> ] ] = tag_func ( * args , ** kwargs ) <EOL> return "<STR_LIT>" <EOL> return AsTagNode ( ) <EOL> return self . tag ( tag_wrapper ) <EOL> def render_tag ( self , tag_func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( tag_func ) <EOL> def tag_wrapper ( parser , token ) : <EOL> class RenderTagNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> return tag_func ( context , token ) <EOL> return RenderTagNode ( ) <EOL> return self . tag ( tag_wrapper ) <EOL> def to_end_tag ( self , tag_func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( tag_func ) <EOL> def tag_wrapper ( parser , token ) : <EOL> class ToEndTagNode ( template . Node ) : <EOL> def __init__ ( self ) : <EOL> end_name = "<STR_LIT>" % tag_func . __name__ <EOL> self . nodelist = parser . parse ( ( end_name , ) ) <EOL> parser . delete_first_token ( ) <EOL> def render ( self , context ) : <EOL> args = ( self . nodelist . render ( context ) , context , token ) <EOL> return tag_func ( * args [ : tag_func . __code__ . co_argcount ] ) <EOL> return ToEndTagNode ( ) <EOL> return self . tag ( tag_wrapper ) <EOL> def inclusion_tag ( self , name , context_class = Context , takes_context = False ) : <EOL> """<STR_LIT>""" <EOL> def tag_decorator ( tag_func ) : <EOL> @ wraps ( tag_func ) <EOL> def tag_wrapper ( parser , token ) : <EOL> class InclusionTagNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> if not getattr ( self , "<STR_LIT>" , False ) : <EOL> try : <EOL> request = context [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> t = get_template ( name ) <EOL> else : <EOL> ts = templates_for_device ( request , name ) <EOL> t = select_template ( ts ) <EOL> self . template = t <EOL> parts = [ template . Variable ( part ) . resolve ( context ) <EOL> for part in token . split_contents ( ) [ <NUM_LIT:1> : ] ] <EOL> if takes_context : <EOL> parts . insert ( <NUM_LIT:0> , context ) <EOL> result = tag_func ( * parts ) <EOL> autoescape = context . autoescape <EOL> context = context_class ( result , autoescape = autoescape ) <EOL> return self . template . render ( context ) <EOL> return InclusionTagNode ( ) <EOL> return self . tag ( tag_wrapper ) <EOL> return tag_decorator </s>
