<s> import os <EOL> from . import core <EOL> from . util import memoize , relpath <EOL> from markupsafe import Markup <EOL> import six <EOL> from six . moves import map <EOL> from six . moves import zip <EOL> SEP , ALTSEP , EXTSEP = os . path . sep , os . path . altsep , os . path . extsep <EOL> engine_name_cache = { } <EOL> _default_rendering_extension_lookup = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT:html>' ] , <EOL> } <EOL> def get_rendering_extensions_lookup ( mw ) : <EOL> if mw is None : <EOL> rl = core . request_local ( ) <EOL> mw = rl . get ( '<STR_LIT>' ) <EOL> if mw is None : <EOL> return _default_rendering_extension_lookup <EOL> return mw . config . rendering_extension_lookup <EOL> @ memoize <EOL> def get_engine_name ( template_name , mw = None ) : <EOL> if template_name in engine_name_cache : <EOL> return engine_name_cache [ template_name ] <EOL> if template_name and '<STR_LIT::>' in template_name : <EOL> engine_name = template_name . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> engine_name_cache [ template_name ] = engine_name <EOL> return engine_name <EOL> try : <EOL> if mw is None : <EOL> rl = core . request_local ( ) <EOL> mw = rl [ '<STR_LIT>' ] <EOL> pref_rend_eng = mw . config . preferred_rendering_engines <EOL> except ( KeyError , AttributeError ) : <EOL> pref_rend_eng = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for engine_name in pref_rend_eng : <EOL> try : <EOL> get_source ( engine_name , template_name , mw = mw ) <EOL> engine_name_cache [ template_name ] = engine_name <EOL> return engine_name <EOL> except IOError : <EOL> pass <EOL> if not mw . config . strict_engine_selection : <EOL> pref_rend_eng = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for engine_name in pref_rend_eng : <EOL> try : <EOL> get_source ( engine_name , template_name , mw = mw ) <EOL> engine_name_cache [ template_name ] = engine_name <EOL> return engine_name <EOL> except IOError : <EOL> pass <EOL> raise ValueError ( "<STR_LIT>" % template_name ) <EOL> @ memoize <EOL> def _get_dotted_filename ( engine_name , template , mw = None ) : <EOL> rendering_extension_lookup = get_rendering_extensions_lookup ( mw ) <EOL> template = _strip_engine_name ( template , mw ) <EOL> location , filename = template . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = __import__ ( location , globals ( ) , locals ( ) , [ '<STR_LIT:*>' ] ) <EOL> parent_dir = SEP . join ( module . __file__ . split ( SEP ) [ : - <NUM_LIT:1> ] ) <EOL> for extension in rendering_extension_lookup [ engine_name ] : <EOL> abs_filename = parent_dir + SEP + filename + EXTSEP + extension <EOL> if os . path . exists ( abs_filename ) : <EOL> return abs_filename <EOL> raise IOError ( "<STR_LIT>" % template ) <EOL> def _strip_engine_name ( template , mw = None ) : <EOL> """<STR_LIT>""" <EOL> rendering_extension_lookup = get_rendering_extensions_lookup ( mw ) <EOL> if any ( map ( template . lstrip ( ) . startswith , rendering_extension_lookup ) ) : <EOL> return template . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> return template <EOL> @ memoize <EOL> def get_source ( engine_name , template , inline = False , mw = None ) : <EOL> if inline : <EOL> return template <EOL> if SEP in template or ( ALTSEP and ALTSEP in template ) : <EOL> filename = _strip_engine_name ( template , mw = mw ) <EOL> else : <EOL> filename = _get_dotted_filename ( engine_name , template , mw = mw ) <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> try : <EOL> source = f . read ( ) <EOL> finally : <EOL> f . close ( ) <EOL> return source <EOL> @ memoize <EOL> def get_render_callable ( engine_name , displays_on , src , filename = None , inline = False ) : <EOL> """<STR_LIT>""" <EOL> directory = None <EOL> if filename and not inline : <EOL> if SEP not in filename and ( not ALTSEP or ALTSEP not in filename ) : <EOL> filename = _get_dotted_filename ( engine_name , filename ) <EOL> directory = os . path . dirname ( filename ) <EOL> if engine_name == '<STR_LIT>' : <EOL> import mako . template <EOL> args = dict ( text = src , imports = [ "<STR_LIT>" ] , <EOL> default_filters = [ '<STR_LIT>' ] ) <EOL> if directory : <EOL> args [ '<STR_LIT:filename>' ] = relpath ( filename , directory ) <EOL> from mako . lookup import TemplateLookup <EOL> args [ '<STR_LIT>' ] = TemplateLookup ( <EOL> directories = [ directory ] ) <EOL> tmpl = mako . template . Template ( ** args ) <EOL> return lambda kwargs : Markup ( tmpl . render_unicode ( ** kwargs ) ) <EOL> elif engine_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> import genshi . template <EOL> args = dict ( <EOL> source = src , <EOL> ) <EOL> if directory : <EOL> args [ '<STR_LIT>' ] = genshi . template . TemplateLoader ( [ <EOL> genshi . template . loader . directory ( directory ) , <EOL> ] ) <EOL> tmpl = genshi . template . MarkupTemplate ( ** args ) <EOL> return lambda kwargs : Markup ( <EOL> '<STR_LIT>' . join ( tmpl . generate ( ** kwargs ) . serialize ( '<STR_LIT>' ) ) <EOL> ) <EOL> elif engine_name == '<STR_LIT>' : <EOL> import jinja2 <EOL> from . jinja_util import htmlbools <EOL> env = jinja2 . environment . Environment ( autoescape = True ) <EOL> env . filters [ '<STR_LIT>' ] = htmlbools <EOL> tmpl = env . from_string ( src , template_class = jinja2 . Template ) <EOL> tmpl . filename = filename <EOL> return lambda kwargs : Markup ( tmpl . render ( ** kwargs ) ) <EOL> elif engine_name == '<STR_LIT>' : <EOL> import kajiki <EOL> tmpl = kajiki . XMLTemplate ( six . u ( src ) , filename = filename , <EOL> cdata_scripts = False ) <EOL> return lambda kwargs : Markup ( tmpl ( kwargs ) . render ( ) ) <EOL> elif engine_name == '<STR_LIT>' : <EOL> import chameleon <EOL> tmpl = chameleon . PageTemplate ( src , filename = filename ) <EOL> return lambda kwargs : Markup ( tmpl . render ( ** kwargs ) . strip ( ) ) <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def render ( template_name , displays_on , kwargs , inline = False , mw = None ) : <EOL> """<STR_LIT>""" <EOL> if not inline : <EOL> engine_name = get_engine_name ( template_name , mw ) <EOL> else : <EOL> engine_name = inline <EOL> if mw is not None and mw . config . auto_reload_templates : <EOL> get_source . _flush ( ) <EOL> get_render_callable . _flush ( ) <EOL> source = get_source ( engine_name , template_name , inline , mw ) <EOL> callback = get_render_callable ( <EOL> engine_name , displays_on , source , template_name , inline ) <EOL> return callback ( kwargs ) </s>
