<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import datetime <EOL> import os <EOL> import re <EOL> import StringIO <EOL> import time <EOL> import zipfile <EOL> from googleapis . codegen . django_helpers import DjangoRenderTemplate <EOL> from googleapis . codegen . language_model import LanguageModel <EOL> from googleapis . codegen . template_objects import UseableInTemplates <EOL> from googleapis . codegen import template_helpers <EOL> from googleapis . codegen . filesys import files <EOL> _GENERATOR_INFORMATION = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _SPECIAL_FILENAMES = [ '<STR_LIT>' ] <EOL> class TemplateGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , language_model = None , options = None ) : <EOL> self . _tool_info = ToolInformation ( ) <EOL> self . _options = options or dict ( ) <EOL> self . _template_dir = os . path . dirname ( __file__ ) <EOL> self . _surface_features = { } <EOL> self . _language_model = language_model or LanguageModel ( ) <EOL> @ property <EOL> def language_model ( self ) : <EOL> return self . _language_model <EOL> def IncludeFileTree ( self , path_to_tree , package ) : <EOL> """<STR_LIT>""" <EOL> top_of_tree = os . path . join ( self . _template_dir , path_to_tree ) <EOL> for path in files . IterFiles ( top_of_tree ) : <EOL> relative_path = path [ len ( top_of_tree ) + <NUM_LIT:1> : ] <EOL> package . IncludeFile ( path , relative_path ) <EOL> def PathToTemplate ( self , template_name ) : <EOL> """<STR_LIT>""" <EOL> return os . path . join ( self . _template_dir , template_name ) <EOL> def RenderTemplate ( self , template_path , context_dict = None ) : <EOL> """<STR_LIT>""" <EOL> variables_dict = { <EOL> '<STR_LIT>' : self . _tool_info , <EOL> '<STR_LIT>' : self . _options , <EOL> '<STR_LIT>' : self . _template_dir , <EOL> '<STR_LIT>' : self . _surface_features , <EOL> '<STR_LIT>' : self . _language_model <EOL> } <EOL> if context_dict : <EOL> variables_dict . update ( context_dict ) <EOL> return DjangoRenderTemplate ( template_path , variables_dict ) <EOL> def RenderTemplateToFile ( self , template_path , context_dict , package , <EOL> output_path ) : <EOL> """<STR_LIT>""" <EOL> output_dir , file_name = os . path . split ( output_path ) <EOL> def WriteFileInPackage ( path , content ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( content , unicode ) : <EOL> content = content . encode ( '<STR_LIT:utf-8>' , errors = '<STR_LIT:ignore>' ) <EOL> out = package . StartFile ( os . path . join ( output_dir , path ) ) <EOL> out . write ( content ) <EOL> package . EndFile ( ) <EOL> try : <EOL> context_dict [ template_helpers . FILE_WRITER ] = WriteFileInPackage <EOL> content = self . RenderTemplate ( template_path , context_dict ) <EOL> WriteFileInPackage ( file_name , content ) <EOL> except template_helpers . Halt : <EOL> pass <EOL> del context_dict [ template_helpers . FILE_WRITER ] <EOL> def WalkTemplateTree ( self , path_to_tree , path_replacements , list_replacements , <EOL> variables , package , file_filter = None ) : <EOL> """<STR_LIT>""" <EOL> def ExpandZipFile ( path ) : <EOL> """<STR_LIT>""" <EOL> full_path = os . path . join ( self . _template_dir , path ) <EOL> zip_slurp = files . GetFileContents ( full_path ) <EOL> archive = zipfile . ZipFile ( StringIO . StringIO ( zip_slurp ) , '<STR_LIT:r>' ) <EOL> for info in archive . infolist ( ) : <EOL> package . WriteDataAsFile ( <EOL> archive . read ( info . filename ) , <EOL> os . path . join ( relative_path , info . filename ) ) <EOL> top_of_tree = os . path . normpath ( <EOL> os . path . join ( self . _template_dir , path_to_tree ) ) <EOL> variables . update ( { '<STR_LIT>' : top_of_tree } ) <EOL> for path in files . IterFiles ( top_of_tree ) : <EOL> root , file_name = os . path . split ( path ) <EOL> template_path = file_name <EOL> relative_path = root [ len ( top_of_tree ) + <NUM_LIT:1> : ] <EOL> for path_item , replacement in path_replacements . iteritems ( ) : <EOL> relative_path = relative_path . replace ( path_item , replacement ) <EOL> for path_item , replacement in path_replacements . iteritems ( ) : <EOL> file_name = file_name . replace ( path_item , replacement ) <EOL> full_template_path = os . path . join ( relative_path , template_path ) <EOL> for path_item , call_info in list_replacements . iteritems ( ) : <EOL> if file_name . find ( path_item ) >= <NUM_LIT:0> : <EOL> self . GenerateListOfFiles ( path_item , call_info , path , relative_path , <EOL> file_name , variables , package , <EOL> file_filter = file_filter ) <EOL> if file_name . startswith ( '<STR_LIT>' ) : <EOL> ExpandZipFile ( path ) <EOL> continue <EOL> if file_name . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> if file_name . endswith ( '<STR_LIT>' ) : <EOL> name_in_zip = file_name [ : - <NUM_LIT:5> ] <EOL> if name_in_zip in _SPECIAL_FILENAMES : <EOL> name_in_zip = name_in_zip . replace ( '<STR_LIT:_>' , '<STR_LIT:.>' ) <EOL> full_output_path = os . path . join ( relative_path , name_in_zip ) <EOL> if file_filter and not file_filter ( full_template_path , <EOL> full_output_path ) : <EOL> continue <EOL> self . RenderTemplateToFile ( path , variables , package , full_output_path ) <EOL> else : <EOL> full_output_path = os . path . join ( relative_path , file_name ) <EOL> if file_filter and not file_filter ( full_template_path , <EOL> full_output_path ) : <EOL> continue <EOL> package . IncludeFile ( path , full_output_path ) <EOL> def GeneratePackage ( self , package_writer ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> def DefaultGeneratePackage ( self , package_writer , path_replacements , <EOL> variables ) : <EOL> """<STR_LIT>""" <EOL> self . WalkTemplateTree ( '<STR_LIT>' , path_replacements , { } , variables , <EOL> package_writer ) <EOL> def SetFeatures ( self , surface_features ) : <EOL> """<STR_LIT>""" <EOL> self . _surface_features = surface_features <EOL> @ property <EOL> def features ( self ) : <EOL> return self . _surface_features <EOL> @ features . setter <EOL> def features ( self , surface_features ) : <EOL> self . _surface_features = surface_features <EOL> @ property <EOL> def language_version ( self ) : <EOL> if self . _surface_features : <EOL> return self . _surface_features . get ( '<STR_LIT>' ) <EOL> def SetTemplateDir ( self , template_dir ) : <EOL> """<STR_LIT>""" <EOL> if template_dir . startswith ( '<STR_LIT:/>' ) : <EOL> self . _template_dir = template_dir <EOL> else : <EOL> self . _template_dir = os . path . join ( self . _template_dir , template_dir ) <EOL> def GenerateListOfFiles ( self , path_prefix , call_info , template_path , <EOL> relative_path , template_file_name , variables , <EOL> package , file_filter = None ) : <EOL> """<STR_LIT>""" <EOL> path_and_var_regex = r'<STR_LIT>' % path_prefix <EOL> match_obj = re . compile ( path_and_var_regex ) . match ( template_file_name ) <EOL> if not match_obj : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> variable_name = match_obj . group ( <NUM_LIT:1> ) <EOL> file_name_piece_to_replace = path_prefix + variable_name + '<STR_LIT>' <EOL> for element in call_info [ <NUM_LIT:1> ] : <EOL> file_name = template_file_name . replace ( <EOL> file_name_piece_to_replace , element . values [ variable_name ] ) <EOL> name_in_zip = file_name [ : - <NUM_LIT:5> ] <EOL> if file_filter and not file_filter ( None , name_in_zip ) : <EOL> continue <EOL> d = dict ( variables ) <EOL> d [ call_info [ <NUM_LIT:0> ] ] = element <EOL> self . RenderTemplateToFile ( <EOL> template_path , d , package , os . path . join ( relative_path , name_in_zip ) ) <EOL> class ToolInformation ( UseableInTemplates ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ToolInformation , self ) . __init__ ( _GENERATOR_INFORMATION ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> self . SetTemplateValue ( '<STR_LIT>' , <EOL> '<STR_LIT>' % ( now . year , now . month , now . day ) ) <EOL> self . SetTemplateValue ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % ( now . hour , now . minute , now . second ) ) </s>
