<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import sys <EOL> from pystache . common import TemplateNotFoundError <EOL> from pystache import defaults <EOL> class Locator ( object ) : <EOL> def __init__ ( self , extension = None ) : <EOL> """<STR_LIT>""" <EOL> if extension is None : <EOL> extension = defaults . TEMPLATE_EXTENSION <EOL> self . template_extension = extension <EOL> def get_object_directory ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> return None <EOL> module = sys . modules [ obj . __module__ ] <EOL> if not hasattr ( module , '<STR_LIT>' ) : <EOL> return None <EOL> path = module . __file__ <EOL> return os . path . dirname ( path ) <EOL> def make_template_name ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> template_name = obj . __class__ . __name__ <EOL> def repl ( match ) : <EOL> return '<STR_LIT:_>' + match . group ( <NUM_LIT:0> ) . lower ( ) <EOL> return re . sub ( '<STR_LIT>' , repl , template_name ) [ <NUM_LIT:1> : ] <EOL> def make_file_name ( self , template_name , template_extension = None ) : <EOL> """<STR_LIT>""" <EOL> file_name = template_name <EOL> if template_extension is None : <EOL> template_extension = self . template_extension <EOL> if template_extension is not False : <EOL> file_name += os . path . extsep + template_extension <EOL> return file_name <EOL> def _find_path ( self , search_dirs , file_name ) : <EOL> """<STR_LIT>""" <EOL> for dir_path in search_dirs : <EOL> file_path = os . path . join ( dir_path , file_name ) <EOL> if os . path . exists ( file_path ) : <EOL> return file_path <EOL> return None <EOL> def _find_path_required ( self , search_dirs , file_name ) : <EOL> """<STR_LIT>""" <EOL> path = self . _find_path ( search_dirs , file_name ) <EOL> if path is None : <EOL> raise TemplateNotFoundError ( '<STR_LIT>' % <EOL> ( repr ( file_name ) , repr ( search_dirs ) ) ) <EOL> return path <EOL> def find_file ( self , file_name , search_dirs ) : <EOL> """<STR_LIT>""" <EOL> return self . _find_path_required ( search_dirs , file_name ) <EOL> def find_name ( self , template_name , search_dirs ) : <EOL> """<STR_LIT>""" <EOL> file_name = self . make_file_name ( template_name ) <EOL> return self . _find_path_required ( search_dirs , file_name ) <EOL> def find_object ( self , obj , search_dirs , file_name = None ) : <EOL> """<STR_LIT>""" <EOL> if file_name is None : <EOL> template_name = self . make_template_name ( obj ) <EOL> file_name = self . make_file_name ( template_name ) <EOL> dir_path = self . get_object_directory ( obj ) <EOL> if dir_path is not None : <EOL> search_dirs = [ dir_path ] + search_dirs <EOL> path = self . _find_path_required ( search_dirs , file_name ) <EOL> return path </s>
