<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import pkgutil <EOL> from . . core import backend , exc , interface , handler <EOL> from . . utils . misc import minimal_logger <EOL> from . . utils import fs <EOL> LOG = minimal_logger ( __name__ ) <EOL> def output_validator ( klass , obj ) : <EOL> """<STR_LIT>""" <EOL> members = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> interface . validate ( IOutput , obj , members ) <EOL> class IOutput ( interface . Interface ) : <EOL> """<STR_LIT>""" <EOL> class IMeta : <EOL> """<STR_LIT>""" <EOL> label = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> validator = output_validator <EOL> """<STR_LIT>""" <EOL> Meta = interface . Attribute ( '<STR_LIT>' ) <EOL> def _setup ( app_obj ) : <EOL> """<STR_LIT>""" <EOL> def render ( data_dict , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> class CementOutputHandler ( handler . CementBaseHandler ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> label = None <EOL> """<STR_LIT>""" <EOL> interface = IOutput <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( CementOutputHandler , self ) . __init__ ( * args , ** kw ) <EOL> class TemplateOutputHandler ( CementOutputHandler ) : <EOL> """<STR_LIT>""" <EOL> def _load_template_from_file ( self , template_path ) : <EOL> for template_dir in self . app . _meta . template_dirs : <EOL> template_prefix = template_dir . rstrip ( '<STR_LIT:/>' ) <EOL> template_path = template_path . lstrip ( '<STR_LIT:/>' ) <EOL> full_path = fs . abspath ( os . path . join ( template_prefix , <EOL> template_path ) ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> full_path ) <EOL> if os . path . exists ( full_path ) : <EOL> content = open ( full_path , '<STR_LIT:r>' ) . read ( ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> full_path ) <EOL> return content <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" % <EOL> full_path ) <EOL> continue <EOL> return None <EOL> def _load_template_from_module ( self , template_path ) : <EOL> template_module = self . app . _meta . template_module <EOL> template_path = template_path . lstrip ( '<STR_LIT:/>' ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( template_path , template_module ) ) <EOL> if template_module not in sys . modules : <EOL> try : <EOL> __import__ ( template_module , globals ( ) , locals ( ) , [ ] , <NUM_LIT:0> ) <EOL> except ImportError as e : <EOL> LOG . debug ( "<STR_LIT>" <EOL> % template_module ) <EOL> return None <EOL> try : <EOL> content = pkgutil . get_data ( template_module , template_path ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( template_path , template_module ) ) <EOL> return content <EOL> except IOError as e : <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( template_path , template_module ) ) <EOL> return None <EOL> def load_template ( self , template_path ) : <EOL> """<STR_LIT>""" <EOL> if not template_path : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> template_path ) <EOL> content = self . _load_template_from_file ( template_path ) <EOL> if content is None : <EOL> content = self . _load_template_from_module ( template_path ) <EOL> if content is not None : <EOL> return content <EOL> else : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> template_path ) </s>
