<s> import os . path <EOL> import six . moves . urllib . parse as urlparse <EOL> from web . httpserver import StaticApp <EOL> from nailgun . settings import settings <EOL> class NailgunStaticApp ( StaticApp ) : <EOL> def translate_path ( self , path ) : <EOL> root = settings . STATIC_DIR <EOL> path = path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if path . startswith ( "<STR_LIT>" ) : <EOL> path = path [ <NUM_LIT:7> : ] <EOL> path = os . path . normpath ( urlparse . unquote ( path ) ) <EOL> words = path . split ( '<STR_LIT:/>' ) <EOL> words = filter ( None , words ) <EOL> path = root <EOL> for word in words : <EOL> drive , word = os . path . splitdrive ( word ) <EOL> head , word = os . path . split ( word ) <EOL> if word in ( os . curdir , os . pardir ) : <EOL> continue <EOL> path = os . path . join ( path , word ) <EOL> return path <EOL> class StaticMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , prefix = '<STR_LIT>' ) : <EOL> self . app = app <EOL> self . prefix = prefix <EOL> def __call__ ( self , environ , start_response ) : <EOL> path = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = self . normpath ( path ) <EOL> if path . startswith ( self . prefix ) : <EOL> return NailgunStaticApp ( environ , start_response ) <EOL> else : <EOL> return self . app ( environ , start_response ) <EOL> def normpath ( self , path ) : <EOL> path2 = os . path . normpath ( urlparse . unquote ( path ) ) <EOL> if path . endswith ( "<STR_LIT:/>" ) : <EOL> path2 += "<STR_LIT:/>" <EOL> return path2 </s>
