<s> import importlib <EOL> import os <EOL> import types <EOL> import jinja2 <EOL> from watson . common import datastructures <EOL> from watson . framework . views . renderers import abc <EOL> TEMPLATES = { <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' , <EOL> '<STR_LIT>' : '''<STR_LIT>''' <EOL> } <EOL> def template_to_posix_path ( template , sep = None ) : <EOL> if not sep : <EOL> sep = os . path . sep <EOL> return template . replace ( sep , '<STR_LIT:/>' ) <EOL> class Renderer ( abc . Renderer ) : <EOL> _env = None <EOL> _debug_mode = False <EOL> @ property <EOL> def env ( self ) : <EOL> return self . _env <EOL> def __init__ ( self , config = None , application = None ) : <EOL> super ( Renderer , self ) . __init__ ( config ) <EOL> self . _debug_mode = application . config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . register_loaders ( ) <EOL> _types = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for _type in _types : <EOL> for module in config [ _type ] : <EOL> mod = importlib . import_module ( module ) <EOL> dic = datastructures . module_to_dict ( <EOL> mod , ignore_starts_with = '<STR_LIT>' ) <EOL> for name , definition in dic . items ( ) : <EOL> obj = '<STR_LIT>' . format ( module , name ) <EOL> env_type = getattr ( self . env , _type ) <EOL> if isinstance ( definition , types . FunctionType ) : <EOL> env_type [ name ] = definition <EOL> else : <EOL> env_type [ name ] = application . container . get ( obj ) <EOL> def register_loaders ( self ) : <EOL> user_loaders = [ jinja2 . FileSystemLoader ( path ) <EOL> for path in self . config . get ( '<STR_LIT>' ) ] <EOL> system_loaders = [ jinja2 . DictLoader ( TEMPLATES ) ] <EOL> if self . _debug_mode : <EOL> loaders = system_loaders + user_loaders <EOL> else : <EOL> loaders = user_loaders + system_loaders <EOL> kwargs = self . config . get ( '<STR_LIT>' , { } ) <EOL> kwargs [ '<STR_LIT>' ] = jinja2 . ChoiceLoader ( loaders ) <EOL> self . _env = jinja2 . Environment ( ** kwargs ) <EOL> def __call__ ( self , view_model , context = None ) : <EOL> template = self . _env . get_template ( <EOL> '<STR_LIT>' . format ( template_to_posix_path ( view_model . template ) , <EOL> self . config [ '<STR_LIT>' ] ) ) <EOL> return template . render ( context = context or { } , ** view_model . data ) </s>
