<s> from __future__ import unicode_literals <EOL> from django import template <EOL> from django . template . base import VariableDoesNotExist <EOL> from waffle import flag_is_active , sample_is_active , switch_is_active <EOL> from waffle . views import _generate_waffle_js <EOL> register = template . Library ( ) <EOL> class WaffleNode ( template . Node ) : <EOL> child_nodelists = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , nodelist_true , nodelist_false , condition , name , <EOL> compiled_name ) : <EOL> self . nodelist_true = nodelist_true <EOL> self . nodelist_false = nodelist_false <EOL> self . condition = condition <EOL> self . name = name <EOL> self . compiled_name = compiled_name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def __iter__ ( self ) : <EOL> for node in self . nodelist_true : <EOL> yield node <EOL> for node in self . nodelist_false : <EOL> yield node <EOL> def render ( self , context ) : <EOL> try : <EOL> name = self . compiled_name . resolve ( context ) <EOL> except VariableDoesNotExist : <EOL> name = self . name <EOL> if not name : <EOL> name = self . name <EOL> if self . condition ( context . get ( '<STR_LIT>' , None ) , name ) : <EOL> return self . nodelist_true . render ( context ) <EOL> return self . nodelist_false . render ( context ) <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token , kind , condition ) : <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) < <NUM_LIT:2> : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % <EOL> bits [ <NUM_LIT:0> ] ) <EOL> name = bits [ <NUM_LIT:1> ] <EOL> compiled_name = parser . compile_filter ( name ) <EOL> nodelist_true = parser . parse ( ( '<STR_LIT>' , '<STR_LIT>' % kind ) ) <EOL> token = parser . next_token ( ) <EOL> if token . contents == '<STR_LIT>' : <EOL> nodelist_false = parser . parse ( ( '<STR_LIT>' % kind , ) ) <EOL> parser . delete_first_token ( ) <EOL> else : <EOL> nodelist_false = template . NodeList ( ) <EOL> return cls ( nodelist_true , nodelist_false , condition , <EOL> name , compiled_name ) <EOL> @ register . tag <EOL> def flag ( parser , token ) : <EOL> return WaffleNode . handle_token ( parser , token , '<STR_LIT>' , flag_is_active ) <EOL> @ register . tag <EOL> def switch ( parser , token ) : <EOL> condition = lambda request , name : switch_is_active ( name ) <EOL> return WaffleNode . handle_token ( parser , token , '<STR_LIT>' , condition ) <EOL> @ register . tag <EOL> def sample ( parser , token ) : <EOL> condition = lambda request , name : sample_is_active ( name ) <EOL> return WaffleNode . handle_token ( parser , token , '<STR_LIT>' , condition ) <EOL> class InlineWaffleJSNode ( template . Node ) : <EOL> def render ( self , context ) : <EOL> return _generate_waffle_js ( context [ '<STR_LIT>' ] ) <EOL> @ register . tag <EOL> def wafflejs ( parser , token ) : <EOL> return InlineWaffleJSNode ( ) </s>
