<s> import collections <EOL> import errno <EOL> import os <EOL> import shutil <EOL> from stango . context import Context <EOL> from stango . decorators import cached_property <EOL> from stango . files import Files <EOL> STANGO_TEMPLATE_DIR = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> class Stango ( object ) : <EOL> HOOK_NAMES = [ '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> self . files = Files ( ) <EOL> self . index_file = None <EOL> self . jinja_extensions = [ ] <EOL> self . template_dirs = [ STANGO_TEMPLATE_DIR ] <EOL> default_hook = lambda context , data : data <EOL> self . hooks = { hook_name : default_hook for hook_name in self . HOOK_NAMES } <EOL> @ cached_property <EOL> def jinja_env ( self ) : <EOL> from jinja2 import Environment , FileSystemLoader <EOL> loader = FileSystemLoader ( self . template_dirs ) <EOL> return Environment ( loader = loader , extensions = self . jinja_extensions ) <EOL> def view ( self , filespec , mode ) : <EOL> assert mode in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> context = Context ( self , mode , filespec ) <EOL> view_result = filespec . view ( context , ** filespec . kwargs ) <EOL> if isinstance ( view_result , str ) : <EOL> byte_result = view_result . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( view_result , ( bytes , bytearray ) ) : <EOL> byte_result = view_result <EOL> elif ( hasattr ( view_result , '<STR_LIT>' ) and <EOL> isinstance ( view_result . read , collections . Callable ) ) : <EOL> file_contents = view_result . read ( ) <EOL> if isinstance ( file_contents , str ) : <EOL> byte_result = file_contents . encode ( '<STR_LIT:utf-8>' ) <EOL> elif isinstance ( file_contents , ( bytes , bytearray ) ) : <EOL> byte_result = file_contents <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( filespec . view . __name__ , filespec . path ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( filespec . view . __name__ , filespec . path ) ) <EOL> result = self . hooks [ '<STR_LIT>' ] ( context , byte_result ) <EOL> if not isinstance ( result , ( bytes , bytearray ) ) : <EOL> raise ValueError ( '<STR_LIT>' % context . realpath ) <EOL> return result <EOL> def make_server ( self , host , port , verbose = False ) : <EOL> from stango . http import StangoHTTPServer <EOL> if port < <NUM_LIT:0> or port > <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' % port ) <EOL> httpd = StangoHTTPServer ( ( host , port ) , self ) <EOL> httpd . verbose = verbose <EOL> return httpd <EOL> def generate ( self , outdir ) : <EOL> if os . path . exists ( outdir ) : <EOL> if os . path . isdir ( outdir ) : <EOL> for entry in os . listdir ( outdir ) : <EOL> path = os . path . join ( outdir , entry ) <EOL> if os . path . isdir ( path ) : <EOL> shutil . rmtree ( path ) <EOL> else : <EOL> os . remove ( path ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % outdir ) <EOL> try : <EOL> os . mkdir ( outdir ) <EOL> except OSError as err : <EOL> if err . errno != errno . EEXIST : <EOL> raise <EOL> for filespec in self . files : <EOL> path = os . path . join ( outdir , filespec . realpath ( self . index_file ) ) <EOL> if not os . path . exists ( os . path . dirname ( path ) ) : <EOL> os . makedirs ( os . path . dirname ( path ) ) <EOL> with open ( path , '<STR_LIT:wb>' ) as fobj : <EOL> data = self . view ( filespec , mode = '<STR_LIT>' ) <EOL> fobj . write ( data ) <EOL> def add_hook ( self , hook_name , hook_func ) : <EOL> if hook_name not in self . HOOK_NAMES : <EOL> raise ValueError ( '<STR_LIT>' % hook_name ) <EOL> if not isinstance ( hook_func , collections . Callable ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . hooks [ hook_name ] = hook_func </s>
