<s> """<STR_LIT>""" <EOL> import sys , logging , logging . handlers , string , socket , struct , os , traceback , types <EOL> try : <EOL> import thread <EOL> import threading <EOL> except ImportError : <EOL> thread = None <EOL> from SocketServer import ThreadingTCPServer , StreamRequestHandler <EOL> DEFAULT_LOGGING_CONFIG_PORT = <NUM_LIT> <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> RESET_ERROR = <NUM_LIT> <EOL> else : <EOL> RESET_ERROR = <NUM_LIT> <EOL> _listener = None <EOL> def fileConfig ( fname , defaults = None ) : <EOL> """<STR_LIT>""" <EOL> import ConfigParser <EOL> cp = ConfigParser . ConfigParser ( defaults ) <EOL> if hasattr ( cp , '<STR_LIT>' ) and hasattr ( fname , '<STR_LIT>' ) : <EOL> cp . readfp ( fname ) <EOL> else : <EOL> cp . read ( fname ) <EOL> formatters = _create_formatters ( cp ) <EOL> logging . _acquireLock ( ) <EOL> try : <EOL> logging . _handlers . clear ( ) <EOL> del logging . _handlerList [ : ] <EOL> handlers = _install_handlers ( cp , formatters ) <EOL> _install_loggers ( cp , handlers ) <EOL> finally : <EOL> logging . _releaseLock ( ) <EOL> def _resolve ( name ) : <EOL> """<STR_LIT>""" <EOL> name = string . split ( name , '<STR_LIT:.>' ) <EOL> used = name . pop ( <NUM_LIT:0> ) <EOL> found = __import__ ( used ) <EOL> for n in name : <EOL> used = used + '<STR_LIT:.>' + n <EOL> try : <EOL> found = getattr ( found , n ) <EOL> except AttributeError : <EOL> __import__ ( used ) <EOL> found = getattr ( found , n ) <EOL> return found <EOL> def _create_formatters ( cp ) : <EOL> """<STR_LIT>""" <EOL> flist = cp . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not len ( flist ) : <EOL> return { } <EOL> flist = string . split ( flist , "<STR_LIT:U+002C>" ) <EOL> formatters = { } <EOL> for form in flist : <EOL> sectname = "<STR_LIT>" % string . strip ( form ) <EOL> opts = cp . options ( sectname ) <EOL> if "<STR_LIT>" in opts : <EOL> fs = cp . get ( sectname , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> else : <EOL> fs = None <EOL> if "<STR_LIT>" in opts : <EOL> dfs = cp . get ( sectname , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> else : <EOL> dfs = None <EOL> c = logging . Formatter <EOL> if "<STR_LIT:class>" in opts : <EOL> class_name = cp . get ( sectname , "<STR_LIT:class>" ) <EOL> if class_name : <EOL> c = _resolve ( class_name ) <EOL> f = c ( fs , dfs ) <EOL> formatters [ form ] = f <EOL> return formatters <EOL> def _install_handlers ( cp , formatters ) : <EOL> """<STR_LIT>""" <EOL> hlist = cp . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if not len ( hlist ) : <EOL> return { } <EOL> hlist = string . split ( hlist , "<STR_LIT:U+002C>" ) <EOL> handlers = { } <EOL> fixups = [ ] <EOL> for hand in hlist : <EOL> sectname = "<STR_LIT>" % string . strip ( hand ) <EOL> klass = cp . get ( sectname , "<STR_LIT:class>" ) <EOL> opts = cp . options ( sectname ) <EOL> if "<STR_LIT>" in opts : <EOL> fmt = cp . get ( sectname , "<STR_LIT>" ) <EOL> else : <EOL> fmt = "<STR_LIT>" <EOL> klass = eval ( klass , vars ( logging ) ) <EOL> args = cp . get ( sectname , "<STR_LIT:args>" ) <EOL> args = eval ( args , vars ( logging ) ) <EOL> h = apply ( klass , args ) <EOL> if "<STR_LIT>" in opts : <EOL> level = cp . get ( sectname , "<STR_LIT>" ) <EOL> h . setLevel ( logging . _levelNames [ level ] ) <EOL> if len ( fmt ) : <EOL> h . setFormatter ( formatters [ fmt ] ) <EOL> if klass == logging . handlers . MemoryHandler : <EOL> if "<STR_LIT:target>" in opts : <EOL> target = cp . get ( sectname , "<STR_LIT:target>" ) <EOL> else : <EOL> target = "<STR_LIT>" <EOL> if len ( target ) : <EOL> fixups . append ( ( h , target ) ) <EOL> handlers [ hand ] = h <EOL> for h , t in fixups : <EOL> h . setTarget ( handlers [ t ] ) <EOL> return handlers <EOL> def _install_loggers ( cp , handlers ) : <EOL> """<STR_LIT>""" <EOL> llist = cp . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> llist = string . split ( llist , "<STR_LIT:U+002C>" ) <EOL> llist = map ( lambda x : string . strip ( x ) , llist ) <EOL> llist . remove ( "<STR_LIT:root>" ) <EOL> sectname = "<STR_LIT>" <EOL> root = logging . root <EOL> log = root <EOL> opts = cp . options ( sectname ) <EOL> if "<STR_LIT>" in opts : <EOL> level = cp . get ( sectname , "<STR_LIT>" ) <EOL> log . setLevel ( logging . _levelNames [ level ] ) <EOL> for h in root . handlers [ : ] : <EOL> root . removeHandler ( h ) <EOL> hlist = cp . get ( sectname , "<STR_LIT>" ) <EOL> if len ( hlist ) : <EOL> hlist = string . split ( hlist , "<STR_LIT:U+002C>" ) <EOL> for hand in hlist : <EOL> log . addHandler ( handlers [ string . strip ( hand ) ] ) <EOL> existing = root . manager . loggerDict . keys ( ) <EOL> for log in llist : <EOL> sectname = "<STR_LIT>" % log <EOL> qn = cp . get ( sectname , "<STR_LIT>" ) <EOL> opts = cp . options ( sectname ) <EOL> if "<STR_LIT>" in opts : <EOL> propagate = cp . getint ( sectname , "<STR_LIT>" ) <EOL> else : <EOL> propagate = <NUM_LIT:1> <EOL> logger = logging . getLogger ( qn ) <EOL> if qn in existing : <EOL> existing . remove ( qn ) <EOL> if "<STR_LIT>" in opts : <EOL> level = cp . get ( sectname , "<STR_LIT>" ) <EOL> logger . setLevel ( logging . _levelNames [ level ] ) <EOL> for h in logger . handlers [ : ] : <EOL> logger . removeHandler ( h ) <EOL> logger . propagate = propagate <EOL> logger . disabled = <NUM_LIT:0> <EOL> hlist = cp . get ( sectname , "<STR_LIT>" ) <EOL> if len ( hlist ) : <EOL> hlist = string . split ( hlist , "<STR_LIT:U+002C>" ) <EOL> for hand in hlist : <EOL> logger . addHandler ( handlers [ string . strip ( hand ) ] ) <EOL> for log in existing : <EOL> root . manager . loggerDict [ log ] . disabled = <NUM_LIT:1> <EOL> def listen ( port = DEFAULT_LOGGING_CONFIG_PORT ) : <EOL> """<STR_LIT>""" <EOL> if not thread : <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> class ConfigStreamHandler ( StreamRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> import tempfile <EOL> try : <EOL> conn = self . connection <EOL> chunk = conn . recv ( <NUM_LIT:4> ) <EOL> if len ( chunk ) == <NUM_LIT:4> : <EOL> slen = struct . unpack ( "<STR_LIT>" , chunk ) [ <NUM_LIT:0> ] <EOL> chunk = self . connection . recv ( slen ) <EOL> while len ( chunk ) < slen : <EOL> chunk = chunk + conn . recv ( slen - len ( chunk ) ) <EOL> file = tempfile . mktemp ( "<STR_LIT>" ) <EOL> f = open ( file , "<STR_LIT:w>" ) <EOL> f . write ( chunk ) <EOL> f . close ( ) <EOL> try : <EOL> fileConfig ( file ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> traceback . print_exc ( ) <EOL> os . remove ( file ) <EOL> except socket . error , e : <EOL> if type ( e . args ) != types . TupleType : <EOL> raise <EOL> else : <EOL> errcode = e . args [ <NUM_LIT:0> ] <EOL> if errcode != RESET_ERROR : <EOL> raise <EOL> class ConfigSocketReceiver ( ThreadingTCPServer ) : <EOL> """<STR_LIT>""" <EOL> allow_reuse_address = <NUM_LIT:1> <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = DEFAULT_LOGGING_CONFIG_PORT , <EOL> handler = None ) : <EOL> ThreadingTCPServer . __init__ ( self , ( host , port ) , handler ) <EOL> logging . _acquireLock ( ) <EOL> self . abort = <NUM_LIT:0> <EOL> logging . _releaseLock ( ) <EOL> self . timeout = <NUM_LIT:1> <EOL> def serve_until_stopped ( self ) : <EOL> import select <EOL> abort = <NUM_LIT:0> <EOL> while not abort : <EOL> rd , wr , ex = select . select ( [ self . socket . fileno ( ) ] , <EOL> [ ] , [ ] , <EOL> self . timeout ) <EOL> if rd : <EOL> self . handle_request ( ) <EOL> logging . _acquireLock ( ) <EOL> abort = self . abort <EOL> logging . _releaseLock ( ) <EOL> def serve ( rcvr , hdlr , port ) : <EOL> server = rcvr ( port = port , handler = hdlr ) <EOL> global _listener <EOL> logging . _acquireLock ( ) <EOL> _listener = server <EOL> logging . _releaseLock ( ) <EOL> server . serve_until_stopped ( ) <EOL> return threading . Thread ( target = serve , <EOL> args = ( ConfigSocketReceiver , <EOL> ConfigStreamHandler , port ) ) <EOL> def stopListening ( ) : <EOL> """<STR_LIT>""" <EOL> global _listener <EOL> if _listener : <EOL> logging . _acquireLock ( ) <EOL> _listener . abort = <NUM_LIT:1> <EOL> _listener = None <EOL> logging . _releaseLock ( ) </s>
