<s> """<STR_LIT>""" <EOL> from django . core . urlresolvers import NoReverseMatch <EOL> from django . template import Node , TemplateSyntaxError , Library , Variable <EOL> from django . utils . encoding import smart_str <EOL> from ella . core import custom_urls <EOL> register = Library ( ) <EOL> class CustomURLNode ( Node ) : <EOL> def __init__ ( self , obj , view_name , args , kwargs , asvar , resolver = None ) : <EOL> self . obj = obj <EOL> self . view_name = view_name <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . asvar = asvar <EOL> self . resolver = resolver or custom_urls . resolver <EOL> def render ( self , context ) : <EOL> args = [ arg . resolve ( context ) for arg in self . args ] <EOL> kwargs = dict ( [ ( smart_str ( k , '<STR_LIT:ascii>' ) , v . resolve ( context ) ) <EOL> for k , v in self . kwargs . items ( ) ] ) <EOL> obj = self . obj . resolve ( context ) <EOL> if not obj : <EOL> return <EOL> url = '<STR_LIT>' <EOL> try : <EOL> url = self . resolver . reverse ( obj , self . view_name , * args , ** kwargs ) <EOL> except NoReverseMatch , e : <EOL> if self . asvar is None : <EOL> raise e <EOL> if self . asvar : <EOL> context [ self . asvar ] = url <EOL> return '<STR_LIT>' <EOL> else : <EOL> return url <EOL> @ register . tag <EOL> def custom_url ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> return _parse_custom_url ( bits ) <EOL> def _parse_custom_url ( bits , resolver = None ) : <EOL> if len ( bits ) < <NUM_LIT:3> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> obj = Variable ( bits [ <NUM_LIT:1> ] ) <EOL> viewname = bits [ <NUM_LIT:2> ] <EOL> args = [ ] <EOL> kwargs = { } <EOL> asvar = None <EOL> if len ( bits ) > <NUM_LIT:3> : <EOL> bits = iter ( bits [ <NUM_LIT:3> : ] ) <EOL> for bit in bits : <EOL> if bit == '<STR_LIT>' : <EOL> asvar = bits . next ( ) <EOL> break <EOL> else : <EOL> for arg in bit . split ( "<STR_LIT:U+002C>" ) : <EOL> if '<STR_LIT:=>' in arg : <EOL> k , v = arg . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> k = k . strip ( ) <EOL> kwargs [ k ] = Variable ( v ) <EOL> elif arg : <EOL> args . append ( Variable ( arg ) ) <EOL> return CustomURLNode ( obj , viewname , args , kwargs , asvar , resolver = resolver ) </s>
