<s> """<STR_LIT>""" <EOL> import os <EOL> from jinja2 import Environment , Undefined , contextfilter <EOL> from jinja2 . exceptions import TemplateSyntaxError <EOL> from jip . logger import getLogger <EOL> from jip . options import Option <EOL> global_context = None <EOL> environment = None <EOL> log = getLogger ( '<STR_LIT>' ) <EOL> class RenderError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , template , message , line = None ) : <EOL> """<STR_LIT>""" <EOL> self . template = template <EOL> self . message = message <EOL> self . line = line <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def __repr__ ( self ) : <EOL> import jip . cli as c <EOL> split = self . template . split ( "<STR_LIT:\n>" ) <EOL> n_len = len ( len ( split ) ) <EOL> lines = [ ( "<STR_LIT:%>" + n_len + "<STR_LIT>" ) % ( i + <NUM_LIT:1> , s ) <EOL> for i , s in enumerate ( split ) ] <EOL> if self . line and self . line < len ( lines ) : <EOL> lines [ self . line ] = c . colorize ( lines [ self . line ] , c . YELLOW ) <EOL> return "<STR_LIT>" % ( <EOL> c . colorize ( "<STR_LIT>" , c . RED ) , <EOL> "<STR_LIT:U+0020>" if not self . line else c . colorize ( <EOL> "<STR_LIT>" % self . line , c . RED <EOL> ) , <EOL> c . colorize ( self . message , c . YELLOW ) , <EOL> "<STR_LIT:\n>" . join ( lines ) <EOL> ) <EOL> def set_global_context ( global_ctx ) : <EOL> global global_context <EOL> if not global_ctx : <EOL> raise <EOL> global_context = global_ctx <EOL> class JipUndefined ( Undefined ) : <EOL> """<STR_LIT>""" <EOL> def __unicode__ ( self ) : <EOL> log . info ( "<STR_LIT>" , self . _undefined_name ) <EOL> return "<STR_LIT>" % ( self . _undefined_name ) <EOL> def __resolve_options ( ctx , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , JipUndefined ) : <EOL> value = value . _undefined_name <EOL> if not isinstance ( value , Option ) : <EOL> script = ctx . get ( '<STR_LIT>' , None ) <EOL> if script : <EOL> v = script . options [ value ] <EOL> if v is not None : <EOL> value = v <EOL> return value <EOL> @ contextfilter <EOL> def suf_filter ( ctx , value , suffix = None ) : <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> if not value : <EOL> return "<STR_LIT>" <EOL> v = str ( value ) <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( v , suffix ) <EOL> if value . to_cmd ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> v = value . get ( ) <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( v , suffix ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def pre_filter ( ctx , value , prefix = None ) : <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> if not value : <EOL> return "<STR_LIT>" <EOL> v = str ( value ) <EOL> prefix = prefix if prefix is not None else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( prefix , v ) <EOL> if value . to_cmd ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> v = value . get ( ) <EOL> prefix = prefix if prefix is not None else value . get_opt ( ) <EOL> return "<STR_LIT>" % ( prefix , v ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def arg_filter ( ctx , value , prefix = None , suffix = None ) : <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> if not value : <EOL> return "<STR_LIT>" <EOL> v = str ( value ) <EOL> prefix = prefix if prefix is not None else "<STR_LIT>" <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( prefix , v , suffix ) <EOL> if value . to_cmd ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> v = value . get ( ) <EOL> original = prefix <EOL> prefix = prefix if prefix is not None else value . get_opt ( ) <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> space = "<STR_LIT>" if ( original is not None or not v ) else "<STR_LIT:U+0020>" <EOL> return "<STR_LIT>" % ( prefix , space , v , suffix ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def else_filter ( ctx , value , prefix = None , suffix = None ) : <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> if value : <EOL> return value <EOL> prefix = prefix if prefix is not None else "<STR_LIT>" <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( prefix , suffix ) <EOL> if value . to_cmd ( ) != "<STR_LIT>" : <EOL> return value <EOL> v = value . get ( ) <EOL> prefix = prefix if prefix is not None else value . get_opt ( ) <EOL> suffix = suffix if suffix is not None else "<STR_LIT>" <EOL> space = "<STR_LIT>" if ( prefix == "<STR_LIT>" or v == "<STR_LIT>" or prefix [ - <NUM_LIT:1> ] == "<STR_LIT:U+0020>" ) else "<STR_LIT:U+0020>" <EOL> return "<STR_LIT>" % ( prefix , space , v , suffix ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def name_filter ( ctx , value ) : <EOL> if isinstance ( value , JipUndefined ) : <EOL> return "<STR_LIT>" % ( value . _undefined_name ) <EOL> from os . path import basename <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> v = str ( value ) <EOL> else : <EOL> v = value . get ( ) <EOL> return basename ( v ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def abs_filter ( ctx , value , base = None ) : <EOL> if isinstance ( value , JipUndefined ) : <EOL> return "<STR_LIT>" % ( value . _undefined_name ) <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> v = str ( value ) <EOL> else : <EOL> v = value . get ( ) <EOL> if value . source . _job is not None : <EOL> base = value . source . job . working_dir <EOL> if base is None : <EOL> base = os . getcwd ( ) <EOL> if isinstance ( v , basestring ) and v and len ( v ) > <NUM_LIT:0> and not v . startswith ( '<STR_LIT:/>' ) : <EOL> v = os . path . join ( os . path . abspath ( base ) , v ) <EOL> return v <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def parent_filter ( ctx , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , JipUndefined ) : <EOL> return "<STR_LIT>" % ( value . _undefined_name ) <EOL> from os . path import dirname <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> v = str ( value ) <EOL> else : <EOL> v = value . get ( ) <EOL> return dirname ( v ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def replace_filter ( ctx , value , search , replace ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , JipUndefined ) : <EOL> return "<STR_LIT>" % ( value . _undefined_name , <EOL> search , replace ) <EOL> try : <EOL> value = __resolve_options ( ctx , value ) <EOL> if not isinstance ( value , Option ) : <EOL> v = str ( value ) <EOL> else : <EOL> v = value . get ( ) <EOL> import re <EOL> return re . sub ( search , replace , v ) <EOL> except : <EOL> return value <EOL> @ contextfilter <EOL> def ext_filter ( ctx , value , splitter = '<STR_LIT:.>' , all = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( value , JipUndefined ) : <EOL> return "<STR_LIT>" % ( value . _undefined_name , splitter ) <EOL> if not isinstance ( value , Option ) : <EOL> script = ctx . get ( '<STR_LIT>' , None ) <EOL> if script : <EOL> v = script . options [ value ] <EOL> if v is not None : <EOL> value = v <EOL> if not isinstance ( value , Option ) : <EOL> v = str ( value ) <EOL> else : <EOL> v = value . get ( ) <EOL> try : <EOL> if not all : <EOL> i = str ( v ) . rindex ( splitter ) <EOL> else : <EOL> i = str ( v ) . index ( splitter ) <EOL> if i > <NUM_LIT:0> : <EOL> return str ( v ) [ : i ] <EOL> except : <EOL> pass <EOL> return v <EOL> except : <EOL> return value <EOL> def _get_environment ( ) : <EOL> """<STR_LIT>""" <EOL> global environment <EOL> if environment is None : <EOL> import jip <EOL> cfg = jip . config . templates <EOL> environment = Environment ( undefined = JipUndefined , <EOL> variable_start_string = cfg . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> variable_end_string = cfg . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:}>' <EOL> ) ) <EOL> environment . filters [ '<STR_LIT>' ] = arg_filter <EOL> environment . filters [ '<STR_LIT>' ] = else_filter <EOL> environment . filters [ '<STR_LIT:name>' ] = name_filter <EOL> environment . filters [ '<STR_LIT>' ] = ext_filter <EOL> environment . filters [ '<STR_LIT>' ] = abs_filter <EOL> environment . filters [ '<STR_LIT>' ] = suf_filter <EOL> environment . filters [ '<STR_LIT>' ] = pre_filter <EOL> environment . filters [ '<STR_LIT>' ] = parent_filter <EOL> environment . filters [ '<STR_LIT>' ] = replace_filter <EOL> return environment <EOL> def render_values ( options , ctx ) : <EOL> result = { } <EOL> for option in options : <EOL> value = option . raw ( ) <EOL> resolved = None <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> resolved = [ ] <EOL> for v in value : <EOL> if isinstance ( v , basestring ) : <EOL> resolved . append ( render_template ( v , ** ctx ) ) <EOL> else : <EOL> resolved . append ( v ) <EOL> else : <EOL> if isinstance ( value , basestring ) : <EOL> resolved = render_template ( value , ** ctx ) <EOL> else : <EOL> resolved = value <EOL> result [ option . name ] = resolved <EOL> return result <EOL> def render_template ( template , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if template is None or not isinstance ( template , basestring ) : <EOL> return template <EOL> tmpl = _get_environment ( ) . from_string ( template ) <EOL> ctx = dict ( kwargs ) <EOL> if global_context is not None : <EOL> for k , v in global_context . iteritems ( ) : <EOL> if not k in ctx : <EOL> ctx [ k ] = v <EOL> ctx [ '<STR_LIT>' ] = global_context <EOL> if '<STR_LIT>' in ctx : <EOL> del ctx [ '<STR_LIT>' ] <EOL> try : <EOL> return tmpl . render ( ** ctx ) <EOL> except TemplateSyntaxError as err : <EOL> raise RenderError ( template , err . message , line = err . lineno ) </s>
