<s> import os <EOL> from pulsar import Http404 <EOL> from lux . utils . files import skipfile , get_rel_dir <EOL> from lux . core import cached , Cacheable <EOL> from lux . core . content import get_reader , render_body <EOL> from . models import DataError , snippets_url <EOL> def get_content ( request , model , path ) : <EOL> app = request . app <EOL> if app . rest_api_client : <EOL> api = app . api ( request ) <EOL> return api . get ( '<STR_LIT>' % ( model . url , path ) ) . json ( ) <EOL> else : <EOL> try : <EOL> return model . get_instance ( request , path ) <EOL> except DataError : <EOL> raise Http404 <EOL> def get_context_files ( app ) : <EOL> '''<STR_LIT>''' <EOL> ctx = { } <EOL> location = app . config [ '<STR_LIT>' ] <EOL> if location and os . path . isdir ( location ) : <EOL> for dirpath , dirs , filenames in os . walk ( location , topdown = False ) : <EOL> if skipfile ( os . path . basename ( dirpath ) or dirpath ) : <EOL> continue <EOL> for filename in filenames : <EOL> if skipfile ( filename ) : <EOL> continue <EOL> file_bits = filename . split ( '<STR_LIT:.>' ) <EOL> bits = [ file_bits [ <NUM_LIT:0> ] ] <EOL> prefix = get_rel_dir ( dirpath , location ) <EOL> while prefix : <EOL> prefix , tail = os . path . split ( prefix ) <EOL> bits . append ( tail ) <EOL> filename = os . path . join ( dirpath , filename ) <EOL> suffix = get_reader ( app , filename ) . suffix <EOL> name = '<STR_LIT:_>' . join ( reversed ( bits ) ) <EOL> if suffix : <EOL> name = '<STR_LIT>' % ( suffix , name ) <EOL> ctx [ name ] = filename <EOL> return ctx <EOL> def get_context ( request , context ) : <EOL> app = request . app <EOL> ctx = { } <EOL> if app . rest_api_client : <EOL> api = app . api ( request ) <EOL> for key in get_api_snippets ( api ) : <EOL> ctx [ key ] = LazyApiContext ( api , key , snippets_url , context ) <EOL> else : <EOL> for key , src in get_context_files ( app ) . items ( ) : <EOL> ctx [ key ] = LazyContext ( app , key , src , context ) <EOL> return ctx <EOL> class LazyContext : <EOL> def __init__ ( self , app , name , src , context ) : <EOL> self . app = app <EOL> self . name = name <EOL> self . src = src <EOL> self . context = context <EOL> def __str__ ( self ) : <EOL> if not isinstance ( self . context , str ) : <EOL> context = self . context <EOL> meta = self . _get_meta ( ) <EOL> body = render_body ( self . app . app , meta , context ) if meta else '<STR_LIT>' <EOL> self . context = context [ self . name ] = body <EOL> return self . context <EOL> def _get_meta ( self ) : <EOL> content = get_reader ( self . app , self . src ) . read ( self . src ) <EOL> return content . json ( self . app ) <EOL> class LazyApiContext ( LazyContext , Cacheable ) : <EOL> @ cached <EOL> def _get_meta ( self ) : <EOL> try : <EOL> url = self . cache_key ( ) <EOL> return self . app . get ( url ) . json ( ) <EOL> except Http404 : <EOL> self . app . logger . error ( '<STR_LIT>' , self . name ) <EOL> return '<STR_LIT>' <EOL> def cache_key ( self , app = None ) : <EOL> return '<STR_LIT>' % ( self . src , self . name ) <EOL> @ cached <EOL> def get_api_snippets ( api ) : <EOL> return api . get ( snippets_url ) . json ( ) </s>
