<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import sys <EOL> import time <EOL> import warnings <EOL> from datetime import datetime <EOL> import logging <EOL> from zope . interface import Interface <EOL> from twisted . python . compat import unicode , _PY3 <EOL> from twisted . python import context <EOL> from twisted . python import reflect <EOL> from twisted . python import util <EOL> from twisted . python import failure <EOL> from twisted . python . threadable import synchronize <EOL> class ILogContext : <EOL> """<STR_LIT>""" <EOL> class ILogObserver ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( eventDict ) : <EOL> """<STR_LIT>""" <EOL> context . setDefault ( ILogContext , <EOL> { "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : "<STR_LIT:->" } ) <EOL> def callWithContext ( ctx , func , * args , ** kw ) : <EOL> newCtx = context . get ( ILogContext ) . copy ( ) <EOL> newCtx . update ( ctx ) <EOL> return context . call ( { ILogContext : newCtx } , func , * args , ** kw ) <EOL> def callWithLogger ( logger , func , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> lp = logger . logPrefix ( ) <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> lp = '<STR_LIT>' <EOL> err ( system = lp ) <EOL> try : <EOL> return callWithContext ( { "<STR_LIT>" : lp } , func , * args , ** kw ) <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> err ( system = lp ) <EOL> def err ( _stuff = None , _why = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if _stuff is None : <EOL> _stuff = failure . Failure ( ) <EOL> if isinstance ( _stuff , failure . Failure ) : <EOL> msg ( failure = _stuff , why = _why , isError = <NUM_LIT:1> , ** kw ) <EOL> elif isinstance ( _stuff , Exception ) : <EOL> msg ( failure = failure . Failure ( _stuff ) , why = _why , isError = <NUM_LIT:1> , ** kw ) <EOL> else : <EOL> msg ( repr ( _stuff ) , why = _why , isError = <NUM_LIT:1> , ** kw ) <EOL> deferr = err <EOL> class Logger : <EOL> """<STR_LIT>""" <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:->' <EOL> class LogPublisher : <EOL> """<STR_LIT>""" <EOL> synchronized = [ '<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> self . observers = [ ] <EOL> def addObserver ( self , other ) : <EOL> """<STR_LIT>""" <EOL> assert callable ( other ) <EOL> self . observers . append ( other ) <EOL> def removeObserver ( self , other ) : <EOL> """<STR_LIT>""" <EOL> self . observers . remove ( other ) <EOL> def msg ( self , * message , ** kw ) : <EOL> """<STR_LIT>""" <EOL> actualEventDict = ( context . get ( ILogContext ) or { } ) . copy ( ) <EOL> actualEventDict . update ( kw ) <EOL> actualEventDict [ '<STR_LIT:message>' ] = message <EOL> actualEventDict [ '<STR_LIT:time>' ] = time . time ( ) <EOL> for i in range ( len ( self . observers ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> try : <EOL> self . observers [ i ] ( actualEventDict ) <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except UnicodeEncodeError : <EOL> raise <EOL> except : <EOL> observer = self . observers [ i ] <EOL> self . observers [ i ] = lambda event : None <EOL> try : <EOL> self . _err ( failure . Failure ( ) , <EOL> "<STR_LIT>" % ( observer , ) ) <EOL> except : <EOL> pass <EOL> self . observers [ i ] = observer <EOL> def _err ( self , failure , why ) : <EOL> """<STR_LIT>""" <EOL> self . msg ( failure = failure , why = why , isError = <NUM_LIT:1> ) <EOL> def showwarning ( self , message , category , filename , lineno , file = None , <EOL> line = None ) : <EOL> """<STR_LIT>""" <EOL> if file is None : <EOL> self . msg ( warning = message , category = reflect . qual ( category ) , <EOL> filename = filename , lineno = lineno , <EOL> format = "<STR_LIT>" ) <EOL> else : <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> _oldshowwarning ( message , category , filename , lineno , file ) <EOL> else : <EOL> _oldshowwarning ( message , category , filename , lineno , file , line ) <EOL> synchronize ( LogPublisher ) <EOL> try : <EOL> theLogPublisher <EOL> except NameError : <EOL> theLogPublisher = LogPublisher ( ) <EOL> addObserver = theLogPublisher . addObserver <EOL> removeObserver = theLogPublisher . removeObserver <EOL> msg = theLogPublisher . msg <EOL> showwarning = theLogPublisher . showwarning <EOL> def _safeFormat ( fmtString , fmtDict ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> text = fmtString % fmtDict <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> try : <EOL> text = ( '<STR_LIT>' % ( fmtString , fmtDict ) ) <EOL> except : <EOL> try : <EOL> text = '<STR_LIT>' % ( fmtString , ) <EOL> except : <EOL> text = '<STR_LIT>' <EOL> return text <EOL> def textFromEventDict ( eventDict ) : <EOL> """<STR_LIT>""" <EOL> edm = eventDict [ '<STR_LIT:message>' ] <EOL> if not edm : <EOL> if eventDict [ '<STR_LIT>' ] and '<STR_LIT>' in eventDict : <EOL> text = ( ( eventDict . get ( '<STR_LIT>' ) or '<STR_LIT>' ) <EOL> + '<STR_LIT:\n>' + eventDict [ '<STR_LIT>' ] . getTraceback ( ) ) <EOL> elif '<STR_LIT>' in eventDict : <EOL> text = _safeFormat ( eventDict [ '<STR_LIT>' ] , eventDict ) <EOL> else : <EOL> return <EOL> else : <EOL> text = '<STR_LIT:U+0020>' . join ( map ( reflect . safe_str , edm ) ) <EOL> return text <EOL> class FileLogObserver : <EOL> """<STR_LIT>""" <EOL> timeFormat = None <EOL> def __init__ ( self , f ) : <EOL> self . write = f . write <EOL> self . flush = f . flush <EOL> def getTimezoneOffset ( self , when ) : <EOL> """<STR_LIT>""" <EOL> offset = datetime . utcfromtimestamp ( when ) - datetime . fromtimestamp ( when ) <EOL> return offset . days * ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) + offset . seconds <EOL> def formatTime ( self , when ) : <EOL> """<STR_LIT>""" <EOL> if self . timeFormat is not None : <EOL> return datetime . fromtimestamp ( when ) . strftime ( self . timeFormat ) <EOL> tzOffset = - self . getTimezoneOffset ( when ) <EOL> when = datetime . utcfromtimestamp ( when + tzOffset ) <EOL> tzHour = abs ( int ( tzOffset / <NUM_LIT> / <NUM_LIT> ) ) <EOL> tzMin = abs ( int ( tzOffset / <NUM_LIT> % <NUM_LIT> ) ) <EOL> if tzOffset < <NUM_LIT:0> : <EOL> tzSign = '<STR_LIT:->' <EOL> else : <EOL> tzSign = '<STR_LIT:+>' <EOL> return '<STR_LIT>' % ( <EOL> when . year , when . month , when . day , <EOL> when . hour , when . minute , when . second , <EOL> tzSign , tzHour , tzMin ) <EOL> def emit ( self , eventDict ) : <EOL> text = textFromEventDict ( eventDict ) <EOL> if text is None : <EOL> return <EOL> timeStr = self . formatTime ( eventDict [ '<STR_LIT:time>' ] ) <EOL> fmtDict = { '<STR_LIT>' : eventDict [ '<STR_LIT>' ] , '<STR_LIT:text>' : text . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) } <EOL> msgStr = _safeFormat ( "<STR_LIT>" , fmtDict ) <EOL> util . untilConcludes ( self . write , timeStr + "<STR_LIT:U+0020>" + msgStr ) <EOL> util . untilConcludes ( self . flush ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> addObserver ( self . emit ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> removeObserver ( self . emit ) <EOL> class PythonLoggingObserver ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loggerName = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . logger = logging . getLogger ( loggerName ) <EOL> def emit ( self , eventDict ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in eventDict : <EOL> level = eventDict [ '<STR_LIT>' ] <EOL> elif eventDict [ '<STR_LIT>' ] : <EOL> level = logging . ERROR <EOL> else : <EOL> level = logging . INFO <EOL> text = textFromEventDict ( eventDict ) <EOL> if text is None : <EOL> return <EOL> self . logger . log ( level , text ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> addObserver ( self . emit ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> removeObserver ( self . emit ) <EOL> class StdioOnnaStick : <EOL> """<STR_LIT>""" <EOL> closed = <NUM_LIT:0> <EOL> softspace = <NUM_LIT:0> <EOL> mode = '<STR_LIT:wb>' <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , isError = <NUM_LIT:0> , encoding = None ) : <EOL> self . isError = isError <EOL> if encoding is None : <EOL> encoding = sys . getdefaultencoding ( ) <EOL> self . encoding = encoding <EOL> self . buf = '<STR_LIT>' <EOL> def close ( self ) : <EOL> pass <EOL> def fileno ( self ) : <EOL> return - <NUM_LIT:1> <EOL> def flush ( self ) : <EOL> pass <EOL> def read ( self ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> readline = read <EOL> readlines = read <EOL> seek = read <EOL> tell = read <EOL> def write ( self , data ) : <EOL> if not _PY3 and isinstance ( data , unicode ) : <EOL> data = data . encode ( self . encoding ) <EOL> d = ( self . buf + data ) . split ( '<STR_LIT:\n>' ) <EOL> self . buf = d [ - <NUM_LIT:1> ] <EOL> messages = d [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> for message in messages : <EOL> msg ( message , printed = <NUM_LIT:1> , isError = self . isError ) <EOL> def writelines ( self , lines ) : <EOL> for line in lines : <EOL> if not _PY3 and isinstance ( line , unicode ) : <EOL> line = line . encode ( self . encoding ) <EOL> msg ( line , printed = <NUM_LIT:1> , isError = self . isError ) <EOL> try : <EOL> _oldshowwarning <EOL> except NameError : <EOL> _oldshowwarning = None <EOL> def startLogging ( file , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( file , StdioOnnaStick ) : <EOL> return <EOL> flo = FileLogObserver ( file ) <EOL> startLoggingWithObserver ( flo . emit , * a , ** kw ) <EOL> return flo <EOL> def startLoggingWithObserver ( observer , setStdout = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> global defaultObserver , _oldshowwarning <EOL> if not _oldshowwarning : <EOL> _oldshowwarning = warnings . showwarning <EOL> warnings . showwarning = showwarning <EOL> if defaultObserver : <EOL> defaultObserver . stop ( ) <EOL> defaultObserver = None <EOL> addObserver ( observer ) <EOL> msg ( "<STR_LIT>" ) <EOL> if setStdout : <EOL> sys . stdout = logfile <EOL> sys . stderr = logerr <EOL> class NullFile : <EOL> softspace = <NUM_LIT:0> <EOL> def read ( self ) : pass <EOL> def write ( self , bytes ) : pass <EOL> def flush ( self ) : pass <EOL> def close ( self ) : pass <EOL> def discardLogs ( ) : <EOL> """<STR_LIT>""" <EOL> global logfile <EOL> logfile = NullFile ( ) <EOL> try : <EOL> logfile <EOL> except NameError : <EOL> logfile = StdioOnnaStick ( <NUM_LIT:0> , getattr ( sys . stdout , "<STR_LIT>" , None ) ) <EOL> logerr = StdioOnnaStick ( <NUM_LIT:1> , getattr ( sys . stderr , "<STR_LIT>" , None ) ) <EOL> class DefaultObserver : <EOL> """<STR_LIT>""" <EOL> stderr = sys . stderr <EOL> def _emit ( self , eventDict ) : <EOL> if eventDict [ "<STR_LIT>" ] : <EOL> if '<STR_LIT>' in eventDict : <EOL> text = ( ( eventDict . get ( '<STR_LIT>' ) or '<STR_LIT>' ) <EOL> + '<STR_LIT:\n>' + eventDict [ '<STR_LIT>' ] . getTraceback ( ) ) <EOL> else : <EOL> text = "<STR_LIT:U+0020>" . join ( [ str ( m ) for m in eventDict [ "<STR_LIT:message>" ] ] ) + "<STR_LIT:\n>" <EOL> self . stderr . write ( text ) <EOL> self . stderr . flush ( ) <EOL> def start ( self ) : <EOL> addObserver ( self . _emit ) <EOL> def stop ( self ) : <EOL> removeObserver ( self . _emit ) <EOL> try : <EOL> defaultObserver <EOL> except NameError : <EOL> defaultObserver = DefaultObserver ( ) <EOL> defaultObserver . start ( ) </s>
