<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> import cgi <EOL> from urllib import quote as url_quote <EOL> import os <EOL> import tokenize <EOL> from cStringIO import StringIO <EOL> from tempita . _looper import looper <EOL> from tempita . compat3 import bytes , basestring_ , next , is_unicode , coerce_text <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:html>' , '<STR_LIT>' ] <EOL> in_re = re . compile ( r'<STR_LIT>' ) <EOL> var_re = re . compile ( r'<STR_LIT>' , re . I ) <EOL> class TemplateError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , position , name = None ) : <EOL> Exception . __init__ ( self , message ) <EOL> self . position = position <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> msg = '<STR_LIT:U+0020>' . join ( self . args ) <EOL> if self . position : <EOL> msg = '<STR_LIT>' % ( <EOL> msg , self . position [ <NUM_LIT:0> ] , self . position [ <NUM_LIT:1> ] ) <EOL> if self . name : <EOL> msg += '<STR_LIT>' % self . name <EOL> return msg <EOL> class _TemplateContinue ( Exception ) : <EOL> pass <EOL> class _TemplateBreak ( Exception ) : <EOL> pass <EOL> def get_file_template ( name , from_template ) : <EOL> path = os . path . join ( os . path . dirname ( from_template . name ) , name ) <EOL> return from_template . __class__ . from_filename ( <EOL> path , namespace = from_template . namespace , <EOL> get_template = from_template . get_template ) <EOL> class Template ( object ) : <EOL> default_namespace = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : looper , <EOL> } <EOL> default_encoding = '<STR_LIT:utf8>' <EOL> default_inherit = None <EOL> def __init__ ( self , content , name = None , namespace = None , stacklevel = None , <EOL> get_template = None , default_inherit = None , line_offset = <NUM_LIT:0> , <EOL> delimeters = None ) : <EOL> self . content = content <EOL> if delimeters is None : <EOL> delimeters = ( self . default_namespace [ '<STR_LIT>' ] , <EOL> self . default_namespace [ '<STR_LIT>' ] ) <EOL> else : <EOL> assert len ( delimeters ) == <NUM_LIT:2> and all ( [ isinstance ( delimeter , basestring ) <EOL> for delimeter in delimeters ] ) <EOL> self . default_namespace = self . __class__ . default_namespace . copy ( ) <EOL> self . default_namespace [ '<STR_LIT>' ] = delimeters [ <NUM_LIT:0> ] <EOL> self . default_namespace [ '<STR_LIT>' ] = delimeters [ <NUM_LIT:1> ] <EOL> self . delimeters = delimeters <EOL> self . _unicode = is_unicode ( content ) <EOL> if name is None and stacklevel is not None : <EOL> try : <EOL> caller = sys . _getframe ( stacklevel ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> globals = caller . f_globals <EOL> lineno = caller . f_lineno <EOL> if '<STR_LIT>' in globals : <EOL> name = globals [ '<STR_LIT>' ] <EOL> if name . endswith ( '<STR_LIT>' ) or name . endswith ( '<STR_LIT>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> elif '<STR_LIT>' in globals : <EOL> name = globals [ '<STR_LIT>' ] <EOL> else : <EOL> name = '<STR_LIT>' <EOL> if lineno : <EOL> name += '<STR_LIT>' % lineno <EOL> self . name = name <EOL> self . _parsed = parse ( content , name = name , line_offset = line_offset , delimeters = self . delimeters ) <EOL> if namespace is None : <EOL> namespace = { } <EOL> self . namespace = namespace <EOL> self . get_template = get_template <EOL> if default_inherit is not None : <EOL> self . default_inherit = default_inherit <EOL> def from_filename ( cls , filename , namespace = None , encoding = None , <EOL> default_inherit = None , get_template = get_file_template ) : <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> c = f . read ( ) <EOL> f . close ( ) <EOL> if encoding : <EOL> c = c . decode ( encoding ) <EOL> return cls ( content = c , name = filename , namespace = namespace , <EOL> default_inherit = default_inherit , get_template = get_template ) <EOL> from_filename = classmethod ( from_filename ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> hex ( id ( self ) ) [ <NUM_LIT:2> : ] , self . name ) <EOL> def substitute ( self , * args , ** kw ) : <EOL> if args : <EOL> if kw : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( args [ <NUM_LIT:0> ] , '<STR_LIT>' ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> % ( args [ <NUM_LIT:0> ] , ) ) <EOL> kw = args [ <NUM_LIT:0> ] <EOL> ns = kw <EOL> ns [ '<STR_LIT>' ] = self . name <EOL> if self . namespace : <EOL> ns . update ( self . namespace ) <EOL> result , defs , inherit = self . _interpret ( ns ) <EOL> if not inherit : <EOL> inherit = self . default_inherit <EOL> if inherit : <EOL> result = self . _interpret_inherit ( result , defs , inherit , ns ) <EOL> return result <EOL> def _interpret ( self , ns ) : <EOL> __traceback_hide__ = True <EOL> parts = [ ] <EOL> defs = { } <EOL> self . _interpret_codes ( self . _parsed , ns , out = parts , defs = defs ) <EOL> if '<STR_LIT>' in defs : <EOL> inherit = defs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> inherit = None <EOL> return '<STR_LIT>' . join ( parts ) , defs , inherit <EOL> def _interpret_inherit ( self , body , defs , inherit_template , ns ) : <EOL> __traceback_hide__ = True <EOL> if not self . get_template : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = None , name = self . name ) <EOL> templ = self . get_template ( inherit_template , self ) <EOL> self_ = TemplateObject ( self . name ) <EOL> for name , value in defs . iteritems ( ) : <EOL> setattr ( self_ , name , value ) <EOL> self_ . body = body <EOL> ns = ns . copy ( ) <EOL> ns [ '<STR_LIT>' ] = self_ <EOL> return templ . substitute ( ns ) <EOL> def _interpret_codes ( self , codes , ns , out , defs ) : <EOL> __traceback_hide__ = True <EOL> for item in codes : <EOL> if isinstance ( item , basestring_ ) : <EOL> out . append ( item ) <EOL> else : <EOL> self . _interpret_code ( item , ns , out , defs ) <EOL> def _interpret_code ( self , code , ns , out , defs ) : <EOL> __traceback_hide__ = True <EOL> name , pos = code [ <NUM_LIT:0> ] , code [ <NUM_LIT:1> ] <EOL> if name == '<STR_LIT>' : <EOL> self . _exec ( code [ <NUM_LIT:2> ] , ns , pos ) <EOL> elif name == '<STR_LIT>' : <EOL> raise _TemplateContinue ( ) <EOL> elif name == '<STR_LIT>' : <EOL> raise _TemplateBreak ( ) <EOL> elif name == '<STR_LIT>' : <EOL> vars , expr , content = code [ <NUM_LIT:2> ] , code [ <NUM_LIT:3> ] , code [ <NUM_LIT:4> ] <EOL> expr = self . _eval ( expr , ns , pos ) <EOL> self . _interpret_for ( vars , expr , content , ns , out , defs ) <EOL> elif name == '<STR_LIT>' : <EOL> parts = code [ <NUM_LIT:2> : ] <EOL> self . _interpret_if ( parts , ns , out , defs ) <EOL> elif name == '<STR_LIT>' : <EOL> parts = code [ <NUM_LIT:2> ] . split ( '<STR_LIT:|>' ) <EOL> base = self . _eval ( parts [ <NUM_LIT:0> ] , ns , pos ) <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> func = self . _eval ( part , ns , pos ) <EOL> base = func ( base ) <EOL> out . append ( self . _repr ( base , pos ) ) <EOL> elif name == '<STR_LIT:default>' : <EOL> var , expr = code [ <NUM_LIT:2> ] , code [ <NUM_LIT:3> ] <EOL> if var not in ns : <EOL> result = self . _eval ( expr , ns , pos ) <EOL> ns [ var ] = result <EOL> elif name == '<STR_LIT>' : <EOL> expr = code [ <NUM_LIT:2> ] <EOL> value = self . _eval ( expr , ns , pos ) <EOL> defs [ '<STR_LIT>' ] = value <EOL> elif name == '<STR_LIT>' : <EOL> name = code [ <NUM_LIT:2> ] <EOL> signature = code [ <NUM_LIT:3> ] <EOL> parts = code [ <NUM_LIT:4> ] <EOL> ns [ name ] = defs [ name ] = TemplateDef ( self , name , signature , body = parts , ns = ns , <EOL> pos = pos ) <EOL> elif name == '<STR_LIT>' : <EOL> return <EOL> else : <EOL> assert <NUM_LIT:0> , "<STR_LIT>" % name <EOL> def _interpret_for ( self , vars , expr , content , ns , out , defs ) : <EOL> __traceback_hide__ = True <EOL> for item in expr : <EOL> if len ( vars ) == <NUM_LIT:1> : <EOL> ns [ vars [ <NUM_LIT:0> ] ] = item <EOL> else : <EOL> if len ( vars ) != len ( item ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> % ( len ( vars ) , len ( item ) ) ) <EOL> for name , value in zip ( vars , item ) : <EOL> ns [ name ] = value <EOL> try : <EOL> self . _interpret_codes ( content , ns , out , defs ) <EOL> except _TemplateContinue : <EOL> continue <EOL> except _TemplateBreak : <EOL> break <EOL> def _interpret_if ( self , parts , ns , out , defs ) : <EOL> __traceback_hide__ = True <EOL> for part in parts : <EOL> assert not isinstance ( part , basestring_ ) <EOL> name , pos = part [ <NUM_LIT:0> ] , part [ <NUM_LIT:1> ] <EOL> if name == '<STR_LIT>' : <EOL> result = True <EOL> else : <EOL> result = self . _eval ( part [ <NUM_LIT:2> ] , ns , pos ) <EOL> if result : <EOL> self . _interpret_codes ( part [ <NUM_LIT:3> ] , ns , out , defs ) <EOL> break <EOL> def _eval ( self , code , ns , pos ) : <EOL> __traceback_hide__ = True <EOL> try : <EOL> try : <EOL> value = eval ( code , self . default_namespace , ns ) <EOL> except SyntaxError , e : <EOL> raise SyntaxError ( <EOL> '<STR_LIT>' % code ) <EOL> return value <EOL> except : <EOL> exc_info = sys . exc_info ( ) <EOL> e = exc_info [ <NUM_LIT:1> ] <EOL> if getattr ( e , '<STR_LIT:args>' , None ) : <EOL> arg0 = e . args [ <NUM_LIT:0> ] <EOL> else : <EOL> arg0 = coerce_text ( e ) <EOL> e . args = ( self . _add_line_info ( arg0 , pos ) , ) <EOL> raise exc_info [ <NUM_LIT:0> ] , e , exc_info [ <NUM_LIT:2> ] <EOL> def _exec ( self , code , ns , pos ) : <EOL> __traceback_hide__ = True <EOL> try : <EOL> exec code in self . default_namespace , ns <EOL> except : <EOL> exc_info = sys . exc_info ( ) <EOL> e = exc_info [ <NUM_LIT:1> ] <EOL> if e . args : <EOL> e . args = ( self . _add_line_info ( e . args [ <NUM_LIT:0> ] , pos ) , ) <EOL> else : <EOL> e . args = ( self . _add_line_info ( None , pos ) , ) <EOL> raise exc_info [ <NUM_LIT:0> ] , e , exc_info [ <NUM_LIT:2> ] <EOL> def _repr ( self , value , pos ) : <EOL> __traceback_hide__ = True <EOL> try : <EOL> if value is None : <EOL> return '<STR_LIT>' <EOL> if self . _unicode : <EOL> try : <EOL> value = unicode ( value ) <EOL> except UnicodeDecodeError : <EOL> value = bytes ( value ) <EOL> else : <EOL> if not isinstance ( value , basestring_ ) : <EOL> value = coerce_text ( value ) <EOL> if ( is_unicode ( value ) <EOL> and self . default_encoding ) : <EOL> value = value . encode ( self . default_encoding ) <EOL> except : <EOL> exc_info = sys . exc_info ( ) <EOL> e = exc_info [ <NUM_LIT:1> ] <EOL> e . args = ( self . _add_line_info ( e . args [ <NUM_LIT:0> ] , pos ) , ) <EOL> raise exc_info [ <NUM_LIT:0> ] , e , exc_info [ <NUM_LIT:2> ] <EOL> else : <EOL> if self . _unicode and isinstance ( value , bytes ) : <EOL> if not self . default_encoding : <EOL> raise UnicodeDecodeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % value ) <EOL> try : <EOL> value = value . decode ( self . default_encoding ) <EOL> except UnicodeDecodeError , e : <EOL> raise UnicodeDecodeError ( <EOL> e . encoding , <EOL> e . object , <EOL> e . start , <EOL> e . end , <EOL> e . reason + '<STR_LIT>' % value ) <EOL> elif not self . _unicode and is_unicode ( value ) : <EOL> if not self . default_encoding : <EOL> raise UnicodeEncodeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % value ) <EOL> value = value . encode ( self . default_encoding ) <EOL> return value <EOL> def _add_line_info ( self , msg , pos ) : <EOL> msg = "<STR_LIT>" % ( <EOL> msg , pos [ <NUM_LIT:0> ] , pos [ <NUM_LIT:1> ] ) <EOL> if self . name : <EOL> msg += "<STR_LIT>" % self . name <EOL> return msg <EOL> def sub ( content , delimeters = None , ** kw ) : <EOL> name = kw . get ( '<STR_LIT>' ) <EOL> tmpl = Template ( content , name = name , delimeters = delimeters ) <EOL> return tmpl . substitute ( kw ) <EOL> def paste_script_template_renderer ( content , vars , filename = None ) : <EOL> tmpl = Template ( content , name = filename ) <EOL> return tmpl . substitute ( vars ) <EOL> class bunch ( dict ) : <EOL> def __init__ ( self , ** kw ) : <EOL> for name , value in kw . iteritems ( ) : <EOL> setattr ( self , name , value ) <EOL> def __setattr__ ( self , name , value ) : <EOL> self [ name ] = value <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> return self [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> def __getitem__ ( self , key ) : <EOL> if '<STR_LIT:default>' in self : <EOL> try : <EOL> return dict . __getitem__ ( self , key ) <EOL> except KeyError : <EOL> return dict . __getitem__ ( self , '<STR_LIT:default>' ) <EOL> else : <EOL> return dict . __getitem__ ( self , key ) <EOL> def __repr__ ( self ) : <EOL> items = [ <EOL> ( k , v ) for k , v in self . iteritems ( ) ] <EOL> items . sort ( ) <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for k , v in items ] ) ) <EOL> class html ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> def __html__ ( self ) : <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . value ) <EOL> def html_quote ( value , force = True ) : <EOL> if not force and hasattr ( value , '<STR_LIT>' ) : <EOL> return value . __html__ ( ) <EOL> if value is None : <EOL> return '<STR_LIT>' <EOL> if not isinstance ( value , basestring_ ) : <EOL> value = coerce_text ( value ) <EOL> if sys . version >= "<STR_LIT:3>" and isinstance ( value , bytes ) : <EOL> value = cgi . escape ( value . decode ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> value = value . encode ( '<STR_LIT>' ) <EOL> else : <EOL> value = cgi . escape ( value , <NUM_LIT:1> ) <EOL> if sys . version < "<STR_LIT:3>" : <EOL> if is_unicode ( value ) : <EOL> value = value . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> return value <EOL> def url ( v ) : <EOL> v = coerce_text ( v ) <EOL> if is_unicode ( v ) : <EOL> v = v . encode ( '<STR_LIT:utf8>' ) <EOL> return url_quote ( v ) <EOL> def attr ( ** kw ) : <EOL> kw = list ( kw . iteritems ( ) ) <EOL> kw . sort ( ) <EOL> parts = [ ] <EOL> for name , value in kw : <EOL> if value is None : <EOL> continue <EOL> if name . endswith ( '<STR_LIT:_>' ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> parts . append ( '<STR_LIT>' % ( html_quote ( name ) , html_quote ( value ) ) ) <EOL> return html ( '<STR_LIT:U+0020>' . join ( parts ) ) <EOL> class HTMLTemplate ( Template ) : <EOL> default_namespace = Template . default_namespace . copy ( ) <EOL> default_namespace . update ( dict ( <EOL> html = html , <EOL> attr = attr , <EOL> url = url , <EOL> html_quote = html_quote , <EOL> ) ) <EOL> def _repr ( self , value , pos ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . __html__ ( ) <EOL> quote = False <EOL> else : <EOL> quote = True <EOL> plain = Template . _repr ( self , value , pos ) <EOL> if quote : <EOL> return html_quote ( plain ) <EOL> else : <EOL> return plain <EOL> def sub_html ( content , ** kw ) : <EOL> name = kw . get ( '<STR_LIT>' ) <EOL> tmpl = HTMLTemplate ( content , name = name ) <EOL> return tmpl . substitute ( kw ) <EOL> class TemplateDef ( object ) : <EOL> def __init__ ( self , template , func_name , func_signature , <EOL> body , ns , pos , bound_self = None ) : <EOL> self . _template = template <EOL> self . _func_name = func_name <EOL> self . _func_signature = func_signature <EOL> self . _body = body <EOL> self . _ns = ns <EOL> self . _pos = pos <EOL> self . _bound_self = bound_self <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . _func_name , self . _func_signature , <EOL> self . _template . name , self . _pos ) <EOL> def __str__ ( self ) : <EOL> return self ( ) <EOL> def __call__ ( self , * args , ** kw ) : <EOL> values = self . _parse_signature ( args , kw ) <EOL> ns = self . _ns . copy ( ) <EOL> ns . update ( values ) <EOL> if self . _bound_self is not None : <EOL> ns [ '<STR_LIT>' ] = self . _bound_self <EOL> out = [ ] <EOL> subdefs = { } <EOL> self . _template . _interpret_codes ( self . _body , ns , out , subdefs ) <EOL> return '<STR_LIT>' . join ( out ) <EOL> def __get__ ( self , obj , type = None ) : <EOL> if obj is None : <EOL> return self <EOL> return self . __class__ ( <EOL> self . _template , self . _func_name , self . _func_signature , <EOL> self . _body , self . _ns , self . _pos , bound_self = obj ) <EOL> def _parse_signature ( self , args , kw ) : <EOL> values = { } <EOL> sig_args , var_args , var_kw , defaults = self . _func_signature <EOL> extra_kw = { } <EOL> for name , value in kw . iteritems ( ) : <EOL> if not var_kw and name not in sig_args : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % name ) <EOL> if name in sig_args : <EOL> values [ sig_args ] = value <EOL> else : <EOL> extra_kw [ name ] = value <EOL> args = list ( args ) <EOL> sig_args = list ( sig_args ) <EOL> while args : <EOL> while sig_args and sig_args [ <NUM_LIT:0> ] in values : <EOL> sig_args . pop ( <NUM_LIT:0> ) <EOL> if sig_args : <EOL> name = sig_args . pop ( <NUM_LIT:0> ) <EOL> values [ name ] = args . pop ( <NUM_LIT:0> ) <EOL> elif var_args : <EOL> values [ var_args ] = tuple ( args ) <EOL> break <EOL> else : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> % '<STR_LIT:U+002CU+0020>' . join ( repr ( v ) for v in args ) ) <EOL> for name , value_expr in defaults . iteritems ( ) : <EOL> if name not in values : <EOL> values [ name ] = self . _template . _eval ( <EOL> value_expr , self . _ns , self . _pos ) <EOL> for name in sig_args : <EOL> if name not in values : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % name ) <EOL> if var_kw : <EOL> values [ var_kw ] = extra_kw <EOL> return values <EOL> class TemplateObject ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . __name = name <EOL> self . get = TemplateObjectGetter ( self ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . __name ) <EOL> class TemplateObjectGetter ( object ) : <EOL> def __init__ ( self , template_obj ) : <EOL> self . __template_obj = template_obj <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . __template_obj , attr , Empty ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . __template_obj ) <EOL> class _Empty ( object ) : <EOL> def __call__ ( self , * args , ** kw ) : <EOL> return self <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' <EOL> def __iter__ ( self ) : <EOL> return iter ( ( ) ) <EOL> def __bool__ ( self ) : <EOL> return False <EOL> if sys . version < "<STR_LIT:3>" : <EOL> __nonzero__ = __bool__ <EOL> Empty = _Empty ( ) <EOL> del _Empty <EOL> def lex ( s , name = None , trim_whitespace = True , line_offset = <NUM_LIT:0> , delimeters = None ) : <EOL> """<STR_LIT>""" <EOL> if delimeters is None : <EOL> delimeters = ( Template . default_namespace [ '<STR_LIT>' ] , <EOL> Template . default_namespace [ '<STR_LIT>' ] ) <EOL> in_expr = False <EOL> chunks = [ ] <EOL> last = <NUM_LIT:0> <EOL> last_pos = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> token_re = re . compile ( r'<STR_LIT>' % ( re . escape ( delimeters [ <NUM_LIT:0> ] ) , <EOL> re . escape ( delimeters [ <NUM_LIT:1> ] ) ) ) <EOL> for match in token_re . finditer ( s ) : <EOL> expr = match . group ( <NUM_LIT:0> ) <EOL> pos = find_position ( s , match . end ( ) , line_offset ) <EOL> if expr == delimeters [ <NUM_LIT:0> ] and in_expr : <EOL> raise TemplateError ( '<STR_LIT>' % delimeters [ <NUM_LIT:0> ] , <EOL> position = pos , <EOL> name = name ) <EOL> elif expr == delimeters [ <NUM_LIT:1> ] and not in_expr : <EOL> raise TemplateError ( '<STR_LIT>' % delimeters [ <NUM_LIT:1> ] , <EOL> position = pos , <EOL> name = name ) <EOL> if expr == delimeters [ <NUM_LIT:0> ] : <EOL> part = s [ last : match . start ( ) ] <EOL> if part : <EOL> chunks . append ( part ) <EOL> in_expr = True <EOL> else : <EOL> chunks . append ( ( s [ last : match . start ( ) ] , last_pos ) ) <EOL> in_expr = False <EOL> last = match . end ( ) <EOL> last_pos = pos <EOL> if in_expr : <EOL> raise TemplateError ( '<STR_LIT>' % delimeters [ <NUM_LIT:1> ] , <EOL> name = name , position = last_pos ) <EOL> part = s [ last : ] <EOL> if part : <EOL> chunks . append ( part ) <EOL> if trim_whitespace : <EOL> chunks = trim_lex ( chunks ) <EOL> return chunks <EOL> statement_re = re . compile ( r'<STR_LIT>' ) <EOL> single_statements = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> trail_whitespace_re = re . compile ( r'<STR_LIT>' ) <EOL> lead_whitespace_re = re . compile ( r'<STR_LIT>' ) <EOL> def trim_lex ( tokens ) : <EOL> r"""<STR_LIT>""" <EOL> last_trim = None <EOL> for i in range ( len ( tokens ) ) : <EOL> current = tokens [ i ] <EOL> if isinstance ( tokens [ i ] , basestring_ ) : <EOL> continue <EOL> item = current [ <NUM_LIT:0> ] <EOL> if not statement_re . search ( item ) and item not in single_statements : <EOL> continue <EOL> if not i : <EOL> prev = '<STR_LIT>' <EOL> else : <EOL> prev = tokens [ i - <NUM_LIT:1> ] <EOL> if i + <NUM_LIT:1> >= len ( tokens ) : <EOL> next_chunk = '<STR_LIT>' <EOL> else : <EOL> next_chunk = tokens [ i + <NUM_LIT:1> ] <EOL> if ( not isinstance ( next_chunk , basestring_ ) <EOL> or not isinstance ( prev , basestring_ ) ) : <EOL> continue <EOL> prev_ok = not prev or trail_whitespace_re . search ( prev ) <EOL> if i == <NUM_LIT:1> and not prev . strip ( ) : <EOL> prev_ok = True <EOL> if last_trim is not None and last_trim + <NUM_LIT:2> == i and not prev . strip ( ) : <EOL> prev_ok = '<STR_LIT>' <EOL> if ( prev_ok <EOL> and ( not next_chunk or lead_whitespace_re . search ( next_chunk ) <EOL> or ( i == len ( tokens ) - <NUM_LIT:2> and not next_chunk . strip ( ) ) ) ) : <EOL> if prev : <EOL> if ( ( i == <NUM_LIT:1> and not prev . strip ( ) ) <EOL> or prev_ok == '<STR_LIT>' ) : <EOL> tokens [ i - <NUM_LIT:1> ] = '<STR_LIT>' <EOL> else : <EOL> m = trail_whitespace_re . search ( prev ) <EOL> prev = prev [ : m . start ( ) + <NUM_LIT:1> ] <EOL> tokens [ i - <NUM_LIT:1> ] = prev <EOL> if next_chunk : <EOL> last_trim = i <EOL> if i == len ( tokens ) - <NUM_LIT:2> and not next_chunk . strip ( ) : <EOL> tokens [ i + <NUM_LIT:1> ] = '<STR_LIT>' <EOL> else : <EOL> m = lead_whitespace_re . search ( next_chunk ) <EOL> next_chunk = next_chunk [ m . end ( ) : ] <EOL> tokens [ i + <NUM_LIT:1> ] = next_chunk <EOL> return tokens <EOL> def find_position ( string , index , line_offset ) : <EOL> """<STR_LIT>""" <EOL> leading = string [ : index ] . splitlines ( ) <EOL> return ( len ( leading ) + line_offset , len ( leading [ - <NUM_LIT:1> ] ) + <NUM_LIT:1> ) <EOL> def parse ( s , name = None , line_offset = <NUM_LIT:0> , delimeters = None ) : <EOL> r"""<STR_LIT>""" <EOL> if delimeters is None : <EOL> delimeters = ( Template . default_namespace [ '<STR_LIT>' ] , <EOL> Template . default_namespace [ '<STR_LIT>' ] ) <EOL> tokens = lex ( s , name = name , line_offset = line_offset , delimeters = delimeters ) <EOL> result = [ ] <EOL> while tokens : <EOL> next_chunk , tokens = parse_expr ( tokens , name ) <EOL> result . append ( next_chunk ) <EOL> return result <EOL> def parse_expr ( tokens , name , context = ( ) ) : <EOL> if isinstance ( tokens [ <NUM_LIT:0> ] , basestring_ ) : <EOL> return tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:1> : ] <EOL> expr , pos = tokens [ <NUM_LIT:0> ] <EOL> expr = expr . strip ( ) <EOL> if expr . startswith ( '<STR_LIT>' ) : <EOL> expr = expr [ <NUM_LIT:3> : ] . lstrip ( '<STR_LIT>' ) <EOL> if expr . startswith ( '<STR_LIT:\n>' ) or expr . startswith ( '<STR_LIT:\r>' ) : <EOL> expr = expr . lstrip ( '<STR_LIT:\r\n>' ) <EOL> if '<STR_LIT:\r>' in expr : <EOL> expr = expr . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) <EOL> expr = expr . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> expr += '<STR_LIT:\n>' <EOL> else : <EOL> if '<STR_LIT:\n>' in expr : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = pos , name = name ) <EOL> return ( '<STR_LIT>' , pos , expr ) , tokens [ <NUM_LIT:1> : ] <EOL> elif expr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in context : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = pos , name = name ) <EOL> return ( expr , pos ) , tokens [ <NUM_LIT:1> : ] <EOL> elif expr . startswith ( '<STR_LIT>' ) : <EOL> return parse_cond ( tokens , name , context ) <EOL> elif ( expr . startswith ( '<STR_LIT>' ) <EOL> or expr == '<STR_LIT>' ) : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' % expr . split ( ) [ <NUM_LIT:0> ] , <EOL> position = pos , name = name ) <EOL> elif expr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' % expr , <EOL> position = pos , name = name ) <EOL> elif expr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' % expr , <EOL> position = pos , name = name ) <EOL> elif expr . startswith ( '<STR_LIT>' ) : <EOL> return parse_for ( tokens , name , context ) <EOL> elif expr . startswith ( '<STR_LIT>' ) : <EOL> return parse_default ( tokens , name , context ) <EOL> elif expr . startswith ( '<STR_LIT>' ) : <EOL> return parse_inherit ( tokens , name , context ) <EOL> elif expr . startswith ( '<STR_LIT>' ) : <EOL> return parse_def ( tokens , name , context ) <EOL> elif expr . startswith ( '<STR_LIT:#>' ) : <EOL> return ( '<STR_LIT>' , pos , tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , tokens [ <NUM_LIT:1> : ] <EOL> return ( '<STR_LIT>' , pos , tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , tokens [ <NUM_LIT:1> : ] <EOL> def parse_cond ( tokens , name , context ) : <EOL> start = tokens [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> pieces = [ ] <EOL> context = context + ( '<STR_LIT>' , ) <EOL> while <NUM_LIT:1> : <EOL> if not tokens : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = start , name = name ) <EOL> if ( isinstance ( tokens [ <NUM_LIT:0> ] , tuple ) <EOL> and tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> return ( '<STR_LIT>' , start ) + tuple ( pieces ) , tokens [ <NUM_LIT:1> : ] <EOL> next_chunk , tokens = parse_one_cond ( tokens , name , context ) <EOL> pieces . append ( next_chunk ) <EOL> def parse_one_cond ( tokens , name , context ) : <EOL> ( first , pos ) , tokens = tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:1> : ] <EOL> content = [ ] <EOL> if first . endswith ( '<STR_LIT::>' ) : <EOL> first = first [ : - <NUM_LIT:1> ] <EOL> if first . startswith ( '<STR_LIT>' ) : <EOL> part = ( '<STR_LIT>' , pos , first [ <NUM_LIT:3> : ] . lstrip ( ) , content ) <EOL> elif first . startswith ( '<STR_LIT>' ) : <EOL> part = ( '<STR_LIT>' , pos , first [ <NUM_LIT:5> : ] . lstrip ( ) , content ) <EOL> elif first == '<STR_LIT>' : <EOL> part = ( '<STR_LIT>' , pos , None , content ) <EOL> else : <EOL> assert <NUM_LIT:0> , "<STR_LIT>" % ( first , pos ) <EOL> while <NUM_LIT:1> : <EOL> if not tokens : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = pos , name = name ) <EOL> if ( isinstance ( tokens [ <NUM_LIT:0> ] , tuple ) <EOL> and ( tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> or tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . startswith ( '<STR_LIT>' ) <EOL> or tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' ) ) : <EOL> return part , tokens <EOL> next_chunk , tokens = parse_expr ( tokens , name , context ) <EOL> content . append ( next_chunk ) <EOL> def parse_for ( tokens , name , context ) : <EOL> first , pos = tokens [ <NUM_LIT:0> ] <EOL> tokens = tokens [ <NUM_LIT:1> : ] <EOL> context = ( '<STR_LIT>' , ) + context <EOL> content = [ ] <EOL> assert first . startswith ( '<STR_LIT>' ) <EOL> if first . endswith ( '<STR_LIT::>' ) : <EOL> first = first [ : - <NUM_LIT:1> ] <EOL> first = first [ <NUM_LIT:3> : ] . strip ( ) <EOL> match = in_re . search ( first ) <EOL> if not match : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' % first , <EOL> position = pos , name = name ) <EOL> vars = first [ : match . start ( ) ] <EOL> if '<STR_LIT:(>' in vars : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' <EOL> % vars , position = pos , name = name ) <EOL> vars = tuple ( [ <EOL> v . strip ( ) for v in first [ : match . start ( ) ] . split ( '<STR_LIT:U+002C>' ) <EOL> if v . strip ( ) ] ) <EOL> expr = first [ match . end ( ) : ] <EOL> while <NUM_LIT:1> : <EOL> if not tokens : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = pos , name = name ) <EOL> if ( isinstance ( tokens [ <NUM_LIT:0> ] , tuple ) <EOL> and tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> return ( '<STR_LIT>' , pos , vars , expr , content ) , tokens [ <NUM_LIT:1> : ] <EOL> next_chunk , tokens = parse_expr ( tokens , name , context ) <EOL> content . append ( next_chunk ) <EOL> def parse_default ( tokens , name , context ) : <EOL> first , pos = tokens [ <NUM_LIT:0> ] <EOL> assert first . startswith ( '<STR_LIT>' ) <EOL> first = first . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> parts = first . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> raise TemplateError ( <EOL> "<STR_LIT>" % first , <EOL> position = pos , name = name ) <EOL> var = parts [ <NUM_LIT:0> ] . strip ( ) <EOL> if '<STR_LIT:U+002C>' in var : <EOL> raise TemplateError ( <EOL> "<STR_LIT>" , <EOL> position = pos , name = name ) <EOL> if not var_re . search ( var ) : <EOL> raise TemplateError ( <EOL> "<STR_LIT>" <EOL> % var , position = pos , name = name ) <EOL> expr = parts [ <NUM_LIT:1> ] . strip ( ) <EOL> return ( '<STR_LIT:default>' , pos , var , expr ) , tokens [ <NUM_LIT:1> : ] <EOL> def parse_inherit ( tokens , name , context ) : <EOL> first , pos = tokens [ <NUM_LIT:0> ] <EOL> assert first . startswith ( '<STR_LIT>' ) <EOL> expr = first . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> return ( '<STR_LIT>' , pos , expr ) , tokens [ <NUM_LIT:1> : ] <EOL> def parse_def ( tokens , name , context ) : <EOL> first , start = tokens [ <NUM_LIT:0> ] <EOL> tokens = tokens [ <NUM_LIT:1> : ] <EOL> assert first . startswith ( '<STR_LIT>' ) <EOL> first = first . split ( None , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> if first . endswith ( '<STR_LIT::>' ) : <EOL> first = first [ : - <NUM_LIT:1> ] <EOL> if '<STR_LIT:(>' not in first : <EOL> func_name = first <EOL> sig = ( ( ) , None , None , { } ) <EOL> elif not first . endswith ( '<STR_LIT:)>' ) : <EOL> raise TemplateError ( "<STR_LIT>" % first , <EOL> position = start , name = name ) <EOL> else : <EOL> first = first [ : - <NUM_LIT:1> ] <EOL> func_name , sig_text = first . split ( '<STR_LIT:(>' , <NUM_LIT:1> ) <EOL> sig = parse_signature ( sig_text , name , start ) <EOL> context = context + ( '<STR_LIT>' , ) <EOL> content = [ ] <EOL> while <NUM_LIT:1> : <EOL> if not tokens : <EOL> raise TemplateError ( <EOL> '<STR_LIT>' , <EOL> position = start , name = name ) <EOL> if ( isinstance ( tokens [ <NUM_LIT:0> ] , tuple ) <EOL> and tokens [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> return ( '<STR_LIT>' , start , func_name , sig , content ) , tokens [ <NUM_LIT:1> : ] <EOL> next_chunk , tokens = parse_expr ( tokens , name , context ) <EOL> content . append ( next_chunk ) <EOL> def parse_signature ( sig_text , name , pos ) : <EOL> tokens = tokenize . generate_tokens ( StringIO ( sig_text ) . readline ) <EOL> sig_args = [ ] <EOL> var_arg = None <EOL> var_kw = None <EOL> defaults = { } <EOL> def get_token ( pos = False ) : <EOL> try : <EOL> tok_type , tok_string , ( srow , scol ) , ( erow , ecol ) , line = next ( tokens ) <EOL> except StopIteration : <EOL> return tokenize . ENDMARKER , '<STR_LIT>' <EOL> if pos : <EOL> return tok_type , tok_string , ( srow , scol ) , ( erow , ecol ) <EOL> else : <EOL> return tok_type , tok_string <EOL> while <NUM_LIT:1> : <EOL> var_arg_type = None <EOL> tok_type , tok_string = get_token ( ) <EOL> if tok_type == tokenize . ENDMARKER : <EOL> break <EOL> if tok_type == tokenize . OP and ( tok_string == '<STR_LIT:*>' or tok_string == '<STR_LIT>' ) : <EOL> var_arg_type = tok_string <EOL> tok_type , tok_string = get_token ( ) <EOL> if tok_type != tokenize . NAME : <EOL> raise TemplateError ( '<STR_LIT>' % sig_text , <EOL> position = pos , name = name ) <EOL> var_name = tok_string <EOL> tok_type , tok_string = get_token ( ) <EOL> if tok_type == tokenize . ENDMARKER or ( tok_type == tokenize . OP and tok_string == '<STR_LIT:U+002C>' ) : <EOL> if var_arg_type == '<STR_LIT:*>' : <EOL> var_arg = var_name <EOL> elif var_arg_type == '<STR_LIT>' : <EOL> var_kw = var_name <EOL> else : <EOL> sig_args . append ( var_name ) <EOL> if tok_type == tokenize . ENDMARKER : <EOL> break <EOL> continue <EOL> if var_arg_type is not None : <EOL> raise TemplateError ( '<STR_LIT>' % sig_text , <EOL> position = pos , name = name ) <EOL> if tok_type == tokenize . OP and tok_string == '<STR_LIT:=>' : <EOL> nest_type = None <EOL> unnest_type = None <EOL> nest_count = <NUM_LIT:0> <EOL> start_pos = end_pos = None <EOL> parts = [ ] <EOL> while <NUM_LIT:1> : <EOL> tok_type , tok_string , s , e = get_token ( True ) <EOL> if start_pos is None : <EOL> start_pos = s <EOL> end_pos = e <EOL> if tok_type == tokenize . ENDMARKER and nest_count : <EOL> raise TemplateError ( '<STR_LIT>' % sig_text , <EOL> position = pos , name = name ) <EOL> if ( not nest_count and <EOL> ( tok_type == tokenize . ENDMARKER or ( tok_type == tokenize . OP and tok_string == '<STR_LIT:U+002C>' ) ) ) : <EOL> default_expr = isolate_expression ( sig_text , start_pos , end_pos ) <EOL> defaults [ var_name ] = default_expr <EOL> sig_args . append ( var_name ) <EOL> break <EOL> parts . append ( ( tok_type , tok_string ) ) <EOL> if nest_count and tok_type == tokenize . OP and tok_string == nest_type : <EOL> nest_count += <NUM_LIT:1> <EOL> elif nest_count and tok_type == tokenize . OP and tok_string == unnest_type : <EOL> nest_count -= <NUM_LIT:1> <EOL> if not nest_count : <EOL> nest_type = unnest_type = None <EOL> elif not nest_count and tok_type == tokenize . OP and tok_string in ( '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' ) : <EOL> nest_type = tok_string <EOL> nest_count = <NUM_LIT:1> <EOL> unnest_type = { '<STR_LIT:(>' : '<STR_LIT:)>' , '<STR_LIT:[>' : '<STR_LIT:]>' , '<STR_LIT:{>' : '<STR_LIT:}>' } [ nest_type ] <EOL> return sig_args , var_arg , var_kw , defaults <EOL> def isolate_expression ( string , start_pos , end_pos ) : <EOL> srow , scol = start_pos <EOL> srow -= <NUM_LIT:1> <EOL> erow , ecol = end_pos <EOL> erow -= <NUM_LIT:1> <EOL> lines = string . splitlines ( True ) <EOL> if srow == erow : <EOL> return lines [ srow ] [ scol : ecol ] <EOL> parts = [ lines [ srow ] [ scol : ] ] <EOL> parts . extend ( lines [ srow + <NUM_LIT:1> : erow ] ) <EOL> if erow < len ( lines ) : <EOL> parts . append ( lines [ erow ] [ : ecol ] ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> _fill_command_usage = """<STR_LIT>""" <EOL> def fill_command ( args = None ) : <EOL> import sys <EOL> import optparse <EOL> import pkg_resources <EOL> import os <EOL> if args is None : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> dist = pkg_resources . get_distribution ( '<STR_LIT>' ) <EOL> parser = optparse . OptionParser ( <EOL> version = coerce_text ( dist ) , <EOL> usage = _fill_command_usage ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> options , args = parser . parse_args ( args ) <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> template_name = args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:1> : ] <EOL> vars = { } <EOL> if options . use_env : <EOL> vars . update ( os . environ ) <EOL> for value in args : <EOL> if '<STR_LIT:=>' not in value : <EOL> print ( '<STR_LIT>' % value ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> name , value = value . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> name = name [ : <NUM_LIT:3> ] <EOL> value = eval ( value ) <EOL> vars [ name ] = value <EOL> if template_name == '<STR_LIT:->' : <EOL> template_content = sys . stdin . read ( ) <EOL> template_name = '<STR_LIT>' <EOL> else : <EOL> f = open ( template_name , '<STR_LIT:rb>' ) <EOL> template_content = f . read ( ) <EOL> f . close ( ) <EOL> if options . use_html : <EOL> TemplateClass = HTMLTemplate <EOL> else : <EOL> TemplateClass = Template <EOL> template = TemplateClass ( template_content , name = template_name ) <EOL> result = template . substitute ( vars ) <EOL> if options . output : <EOL> f = open ( options . output , '<STR_LIT:wb>' ) <EOL> f . write ( result ) <EOL> f . close ( ) <EOL> else : <EOL> sys . stdout . write ( result ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> fill_command ( ) </s>
