<s> import os <EOL> import sys <EOL> from . import parsing <EOL> from . parsing import * <EOL> PYTHON_MAJOR_VERSION = sys . version_info [ <NUM_LIT:0> ] <EOL> if PYTHON_MAJOR_VERSION > <NUM_LIT:2> : <EOL> from past . builtins import basestring <EOL> """<STR_LIT>""" <EOL> class ContentHandler : <EOL> """<STR_LIT>""" <EOL> content = None <EOL> is_file = False <EOL> is_template_path = False <EOL> is_template_content = False <EOL> def is_dynamic ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . is_template_path or self . is_template_content <EOL> def get_content ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> if self . is_file : <EOL> path = self . content <EOL> if self . is_template_path and context : <EOL> path = string . Template ( path ) . safe_substitute ( <EOL> context . get_values ( ) ) <EOL> data = None <EOL> with open ( path , '<STR_LIT:r>' ) as f : <EOL> data = f . read ( ) <EOL> if self . is_template_content and context : <EOL> return string . Template ( data ) . safe_substitute ( context . get_values ( ) ) <EOL> else : <EOL> return data <EOL> else : <EOL> if self . is_template_content and context : <EOL> return safe_substitute_unicode_template ( self . content , context . get_values ( ) ) <EOL> else : <EOL> return self . content <EOL> def create_noread_version ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_file or self . is_template_path : <EOL> return self <EOL> output = ContentHandler ( ) <EOL> output . is_template_content = self . is_template_content <EOL> with open ( self . content , '<STR_LIT:r>' ) as f : <EOL> output . content = f . read ( ) <EOL> return output <EOL> def setup ( self , input , is_file = False , is_template_path = False , is_template_content = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( input , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if is_file : <EOL> input = os . path . abspath ( input ) <EOL> self . content = input <EOL> self . is_file = is_file <EOL> self . is_template_path = is_template_path <EOL> self . is_template_content = is_template_content <EOL> @ staticmethod <EOL> def parse_content ( node ) : <EOL> """<STR_LIT>""" <EOL> output = ContentHandler ( ) <EOL> is_template_path = False <EOL> is_template_content = False <EOL> is_file = False <EOL> is_done = False <EOL> while ( node and not is_done ) : <EOL> if isinstance ( node , basestring ) : <EOL> output . content = node <EOL> output . setup ( node , is_file = is_file , is_template_path = is_template_path , <EOL> is_template_content = is_template_content ) <EOL> return output <EOL> elif not isinstance ( node , dict ) and not isinstance ( node , list ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> is_done = True <EOL> flat = lowercase_keys ( flatten_dictionaries ( node ) ) <EOL> for key , value in flat . items ( ) : <EOL> if key == u'<STR_LIT>' : <EOL> if isinstance ( value , basestring ) : <EOL> if is_file : <EOL> value = os . path . abspath ( value ) <EOL> output . content = value <EOL> is_template_content = is_template_content or not is_file <EOL> output . is_template_content = is_template_content <EOL> output . is_template_path = is_file <EOL> output . is_file = is_file <EOL> return output <EOL> else : <EOL> is_template_content = True <EOL> node = value <EOL> is_done = False <EOL> break <EOL> elif key == '<STR_LIT:file>' : <EOL> if isinstance ( value , basestring ) : <EOL> output . content = os . path . abspath ( value ) <EOL> output . is_file = True <EOL> output . is_template_content = is_template_content <EOL> return output <EOL> else : <EOL> is_file = True <EOL> node = value <EOL> is_done = False <EOL> break <EOL> raise Exception ( "<STR_LIT>" ) </s>
