<s> import os <EOL> import uuid <EOL> import imp <EOL> from . _version import __version__ <EOL> from . template import Template , Resolver <EOL> from . error import ParseError , FormatError , NotFound <EOL> def discover_templates ( paths = None , recursive = True ) : <EOL> '''<STR_LIT>''' <EOL> templates = [ ] <EOL> if paths is None : <EOL> paths = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( os . pathsep ) <EOL> for path in paths : <EOL> for base , directories , filenames in os . walk ( path ) : <EOL> for filename in filenames : <EOL> _ , extension = os . path . splitext ( filename ) <EOL> if extension != '<STR_LIT>' : <EOL> continue <EOL> module_path = os . path . join ( base , filename ) <EOL> module_name = uuid . uuid4 ( ) . hex <EOL> module = imp . load_source ( module_name , module_path ) <EOL> try : <EOL> registered = module . register ( ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if registered : <EOL> templates . extend ( registered ) <EOL> if not recursive : <EOL> del directories [ : ] <EOL> return templates <EOL> def parse ( path , templates ) : <EOL> '''<STR_LIT>''' <EOL> for template in templates : <EOL> try : <EOL> data = template . parse ( path ) <EOL> except ParseError : <EOL> continue <EOL> else : <EOL> return ( data , template ) <EOL> raise ParseError ( <EOL> '<STR_LIT>' <EOL> . format ( path ) <EOL> ) <EOL> def format ( data , templates ) : <EOL> '''<STR_LIT>''' <EOL> for template in templates : <EOL> try : <EOL> path = template . format ( data ) <EOL> except FormatError : <EOL> continue <EOL> else : <EOL> return ( path , template ) <EOL> raise FormatError ( <EOL> '<STR_LIT>' <EOL> . format ( data ) <EOL> ) <EOL> def get_template ( name , templates ) : <EOL> '''<STR_LIT>''' <EOL> for template in templates : <EOL> if template . name == name : <EOL> return template <EOL> raise NotFound ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) </s>
