<s> '''<STR_LIT>''' <EOL> from basic_auth import basic_auth <EOL> import logging <EOL> import os <EOL> import threading <EOL> import tornado . web <EOL> import tornado . ioloop <EOL> DEFAULT_CONFIG = { '<STR_LIT:port>' : <NUM_LIT> , '<STR_LIT:username>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT>' } <EOL> AUTH_USER = None <EOL> AUTH_PASS = None <EOL> PORT = None <EOL> SERVER = None <EOL> _THREAD = None <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> def load ( pglot , user_config ) : <EOL> '''<STR_LIT>''' <EOL> global SERVER , _THREAD <EOL> config = DEFAULT_CONFIG <EOL> config . update ( user_config ) <EOL> set_config ( config ) <EOL> SERVER = tornado . web . Application ( [ ] , { } ) <EOL> SERVER . listen ( PORT ) <EOL> _THREAD = threading . Thread ( target = run_server ) <EOL> _THREAD . daemon = True <EOL> _THREAD . start ( ) <EOL> _LOGGER . info ( '<STR_LIT>' , PORT ) <EOL> _LOGGER . info ( '<STR_LIT>' ) <EOL> def unload ( ) : <EOL> '''<STR_LIT>''' <EOL> ioloop = tornado . ioloop . IOLoop . instance ( ) <EOL> ioloop . add_callback ( lambda x : x . stop ( ) , ioloop ) <EOL> _LOGGER . info ( '<STR_LIT>' ) <EOL> def get_config ( ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:password>' : AUTH_PASS , '<STR_LIT:username>' : AUTH_USER , '<STR_LIT:port>' : PORT } <EOL> def set_config ( config ) : <EOL> """<STR_LIT>""" <EOL> global AUTH_USER , AUTH_PASS , PORT <EOL> PORT = config [ '<STR_LIT:port>' ] <EOL> AUTH_USER = config [ '<STR_LIT:username>' ] <EOL> AUTH_PASS = config [ '<STR_LIT:password>' ] <EOL> def register ( handlers = None , subdomain = None , parent_dir = None , urls = None ) : <EOL> '''<STR_LIT>''' <EOL> if subdomain is None : <EOL> subdomain = "<STR_LIT>" <EOL> else : <EOL> subdomain = r'<STR_LIT>' . format ( subdomain ) <EOL> if parent_dir is None : <EOL> parent_dir = '<STR_LIT>' <EOL> else : <EOL> parent_dir = '<STR_LIT>' . format ( parent_dir ) <EOL> if not urls : <EOL> def doc_url ( handler ) : <EOL> """<STR_LIT>""" <EOL> docs = handler . __doc__ <EOL> pieces = docs . strip ( ) . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) <EOL> return '<STR_LIT>' . join ( [ piece . strip ( ) for piece in pieces ] ) <EOL> handlers = [ tornado . web . URLSpec ( '<STR_LIT>' . format ( parent_dir , <EOL> doc_url ( handler ) ) , <EOL> handler ) <EOL> for handler in handlers ] <EOL> else : <EOL> handlers = urls <EOL> def add_handler_callback ( server , subdmn , hndls ) : <EOL> """<STR_LIT>""" <EOL> server . add_handlers ( subdmn , hndls ) <EOL> ioloop = tornado . ioloop . IOLoop . instance ( ) <EOL> ioloop . add_callback ( add_handler_callback , SERVER , subdomain , handlers ) <EOL> def run_server ( ) : <EOL> '''<STR_LIT>''' <EOL> tornado . ioloop . IOLoop . instance ( ) . start ( ) <EOL> def authenticate ( username , password ) : <EOL> '''<STR_LIT>''' <EOL> if AUTH_USER is None : <EOL> return True <EOL> return username == AUTH_USER and password == AUTH_PASS <EOL> @ basic_auth ( authenticate ) <EOL> class AbstractHandler ( tornado . web . RequestHandler ) : <EOL> '''<STR_LIT>''' <EOL> def get ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . write ( '<STR_LIT>' ) <EOL> self . finish ( ) <EOL> def data_received ( self , chunk ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> @ basic_auth ( authenticate ) <EOL> class AuthStaticFileHandler ( tornado . web . StaticFileHandler ) : <EOL> """<STR_LIT>""" <EOL> def data_received ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
