<s> from operator import attrgetter <EOL> from django . template import Node <EOL> from classytags . blocks import BlockDefinition <EOL> from classytags . compat import compat_basestring <EOL> from classytags . parser import Parser <EOL> from classytags . utils import StructuredOptions <EOL> from classytags . utils import get_default_name <EOL> class Options ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * options , ** kwargs ) : <EOL> self . options = { } <EOL> self . raw_options = options <EOL> self . breakpoints = [ ] <EOL> self . combined_breakpoints = { } <EOL> current_breakpoint = None <EOL> last = None <EOL> self . options [ current_breakpoint ] = [ ] <EOL> self . all_argument_names = [ ] <EOL> for value in options : <EOL> if isinstance ( value , compat_basestring ) : <EOL> if isinstance ( last , compat_basestring ) : <EOL> self . combined_breakpoints [ last ] = value <EOL> self . breakpoints . append ( value ) <EOL> current_breakpoint = value <EOL> self . options [ current_breakpoint ] = [ ] <EOL> else : <EOL> self . options [ current_breakpoint ] . append ( value ) <EOL> self . all_argument_names . append ( value . name ) <EOL> last = value <EOL> self . blocks = [ ] <EOL> for block in kwargs . get ( '<STR_LIT>' , [ ] ) : <EOL> if isinstance ( block , BlockDefinition ) : <EOL> block_definition = block <EOL> elif isinstance ( block , compat_basestring ) : <EOL> block_definition = BlockDefinition ( block , block ) <EOL> else : <EOL> block_definition = BlockDefinition ( block [ <NUM_LIT:1> ] , block [ <NUM_LIT:0> ] ) <EOL> block_definition . validate ( self ) <EOL> self . blocks . append ( block_definition ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . parser_class = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> self . parser_class = Parser <EOL> def __repr__ ( self ) : <EOL> bits = list ( map ( repr , self . options [ None ] ) ) <EOL> for breakpoint in self . breakpoints : <EOL> bits . append ( breakpoint ) <EOL> for option in self . options [ breakpoint ] : <EOL> bits . append ( repr ( option ) ) <EOL> options = '<STR_LIT:U+002C>' . join ( bits ) <EOL> if self . blocks : <EOL> blocks = '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( map ( attrgetter ( '<STR_LIT>' ) , self . blocks ) ) <EOL> else : <EOL> blocks = '<STR_LIT>' <EOL> return '<STR_LIT>' % ( options , blocks ) <EOL> def __add__ ( self , other ) : <EOL> if not isinstance ( other , Options ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if all ( ( self . blocks , other . blocks ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if self . parser_class != other . parser_class : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> kwargs = { <EOL> '<STR_LIT>' : self . parser_class , <EOL> '<STR_LIT>' : self . blocks or other . blocks <EOL> } <EOL> args = list ( self . raw_options ) + list ( other . raw_options ) <EOL> return Options ( * args , ** kwargs ) <EOL> def get_parser_class ( self ) : <EOL> return self . parser_class <EOL> def bootstrap ( self ) : <EOL> """<STR_LIT>""" <EOL> return StructuredOptions ( <EOL> self . options , <EOL> self . breakpoints , <EOL> self . blocks , <EOL> self . combined_breakpoints <EOL> ) <EOL> def parse ( self , parser , tokens ) : <EOL> """<STR_LIT>""" <EOL> argument_parser_class = self . get_parser_class ( ) <EOL> argument_parser = argument_parser_class ( self ) <EOL> return argument_parser . parse ( parser , tokens ) <EOL> class TagMeta ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> parents = [ base for base in bases if isinstance ( base , TagMeta ) ] <EOL> if not parents : <EOL> return super ( TagMeta , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> tag_name = str ( attrs . get ( '<STR_LIT:name>' , get_default_name ( name ) ) ) <EOL> def fake_func ( ) : <EOL> pass <EOL> fake_func . __name__ = tag_name <EOL> attrs [ '<STR_LIT>' ] = fake_func <EOL> attrs [ '<STR_LIT:name>' ] = str ( tag_name ) <EOL> return super ( TagMeta , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> class Tag ( TagMeta ( '<STR_LIT>' , ( Node , ) , { } ) ) : <EOL> """<STR_LIT>""" <EOL> options = Options ( ) <EOL> name = None <EOL> def __init__ ( self , parser , tokens ) : <EOL> self . kwargs , self . blocks = self . options . parse ( parser , tokens ) <EOL> self . child_nodelists = [ ] <EOL> for key , value in self . blocks . items ( ) : <EOL> setattr ( self , key , value ) <EOL> self . child_nodelists . append ( key ) <EOL> def render ( self , context ) : <EOL> """<STR_LIT>""" <EOL> items = self . kwargs . items ( ) <EOL> kwargs = dict ( [ ( key , value . resolve ( context ) ) for key , value in items ] ) <EOL> kwargs . update ( self . blocks ) <EOL> return self . render_tag ( context , ** kwargs ) <EOL> def render_tag ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name </s>
