<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> 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> from twisted . logger import ( <EOL> Logger as NewLogger , LogLevel as NewLogLevel , <EOL> STDLibLogObserver as NewSTDLibLogObserver , <EOL> LegacyLogObserverWrapper , LoggingFile , LogPublisher as NewPublisher , <EOL> globalLogPublisher as newGlobalLogPublisher , <EOL> globalLogBeginner as newGlobalLogBeginner , <EOL> ) <EOL> from twisted . logger . _global import LogBeginner <EOL> from twisted . logger . _legacy import publishToNewObserver as _publishNew <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>" : "<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 , observerPublisher = None , publishPublisher = None , <EOL> logBeginner = None , warningsModule = warnings ) : <EOL> if publishPublisher is None : <EOL> publishPublisher = NewPublisher ( ) <EOL> if observerPublisher is None : <EOL> observerPublisher = publishPublisher <EOL> if observerPublisher is None : <EOL> observerPublisher = NewPublisher ( ) <EOL> self . _observerPublisher = observerPublisher <EOL> self . _publishPublisher = publishPublisher <EOL> self . _legacyObservers = [ ] <EOL> if logBeginner is None : <EOL> beginnerPublisher = NewPublisher ( ) <EOL> beginnerPublisher . addObserver ( observerPublisher ) <EOL> logBeginner = LogBeginner ( beginnerPublisher , NullFile ( ) , sys , <EOL> warnings ) <EOL> self . _logBeginner = logBeginner <EOL> self . _warningsModule = warningsModule <EOL> self . _oldshowwarning = warningsModule . showwarning <EOL> self . showwarning = self . _logBeginner . showwarning <EOL> @ property <EOL> def observers ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ x . legacyObserver for x in self . _legacyObservers ] <EOL> def _startLogging ( self , other , setStdout ) : <EOL> """<STR_LIT>""" <EOL> wrapped = LegacyLogObserverWrapper ( other ) <EOL> self . _legacyObservers . append ( wrapped ) <EOL> self . _logBeginner . beginLoggingTo ( [ wrapped ] , True , setStdout ) <EOL> def _stopLogging ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _warningsModule . showwarning == self . showwarning : <EOL> self . _warningsModule . showwarning = self . _oldshowwarning <EOL> def addObserver ( self , other ) : <EOL> """<STR_LIT>""" <EOL> wrapped = LegacyLogObserverWrapper ( other ) <EOL> self . _legacyObservers . append ( wrapped ) <EOL> self . _observerPublisher . addObserver ( wrapped ) <EOL> def removeObserver ( self , other ) : <EOL> """<STR_LIT>""" <EOL> for observer in self . _legacyObservers : <EOL> if observer . legacyObserver == other : <EOL> self . _legacyObservers . remove ( observer ) <EOL> self . _observerPublisher . removeObserver ( observer ) <EOL> break <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> if "<STR_LIT>" not in actualEventDict : <EOL> actualEventDict [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> _publishNew ( self . _publishPublisher , actualEventDict , textFromEventDict ) <EOL> synchronize ( LogPublisher ) <EOL> if '<STR_LIT>' not in globals ( ) : <EOL> def _actually ( something ) : <EOL> """<STR_LIT>""" <EOL> def decorate ( thingWithADocstring ) : <EOL> return something <EOL> return decorate <EOL> theLogPublisher = LogPublisher ( <EOL> observerPublisher = newGlobalLogPublisher , <EOL> publishPublisher = newGlobalLogPublisher , <EOL> logBeginner = newGlobalLogBeginner , <EOL> ) <EOL> @ _actually ( theLogPublisher . addObserver ) <EOL> def addObserver ( observer ) : <EOL> """<STR_LIT>""" <EOL> @ _actually ( theLogPublisher . removeObserver ) <EOL> def removeObserver ( observer ) : <EOL> """<STR_LIT>""" <EOL> @ _actually ( theLogPublisher . msg ) <EOL> def msg ( * message , ** event ) : <EOL> """<STR_LIT>""" <EOL> @ _actually ( theLogPublisher . showwarning ) <EOL> def showwarning ( ) : <EOL> """<STR_LIT>""" <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>' <EOL> '<STR_LIT>' % ( fmtString , fmtDict ) ) <EOL> except : <EOL> try : <EOL> text = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( fmtString , ) ) <EOL> except : <EOL> text = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if _PY3 : <EOL> if isinstance ( text , bytes ) : <EOL> text = text . decode ( "<STR_LIT:utf-8>" ) <EOL> else : <EOL> if isinstance ( text , unicode ) : <EOL> text = text . encode ( "<STR_LIT:utf-8>" ) <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> why = eventDict . get ( '<STR_LIT>' ) <EOL> if why : <EOL> why = reflect . safe_str ( why ) <EOL> else : <EOL> why = '<STR_LIT>' <EOL> try : <EOL> traceback = eventDict [ '<STR_LIT>' ] . getTraceback ( ) <EOL> except Exception as e : <EOL> traceback = '<STR_LIT>' + str ( e ) <EOL> text = ( why + '<STR_LIT:\n>' + traceback ) <EOL> elif '<STR_LIT>' in eventDict : <EOL> text = _safeFormat ( eventDict [ '<STR_LIT>' ] , eventDict ) <EOL> else : <EOL> return None <EOL> else : <EOL> text = '<STR_LIT:U+0020>' . join ( map ( reflect . safe_str , edm ) ) <EOL> return text <EOL> class _GlobalStartStopMixIn : <EOL> """<STR_LIT>""" <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> addObserver ( self . emit ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> removeObserver ( self . emit ) <EOL> class FileLogObserver ( _GlobalStartStopMixIn ) : <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> """<STR_LIT>""" <EOL> text = textFromEventDict ( eventDict ) <EOL> if text is None : <EOL> return <EOL> timeStr = self . formatTime ( eventDict [ "<STR_LIT:time>" ] ) <EOL> fmtDict = { <EOL> "<STR_LIT>" : eventDict [ "<STR_LIT>" ] , <EOL> "<STR_LIT:text>" : text . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> } <EOL> msgStr = _safeFormat ( "<STR_LIT>" , fmtDict ) <EOL> util . untilConcludes ( self . write , timeStr + "<STR_LIT:U+0020>" + msgStr ) <EOL> util . untilConcludes ( self . flush ) <EOL> class PythonLoggingObserver ( _GlobalStartStopMixIn , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , loggerName = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . _newObserver = NewSTDLibLogObserver ( loggerName ) <EOL> def emit ( self , eventDict ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in eventDict : <EOL> _publishNew ( self . _newObserver , eventDict , textFromEventDict ) <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> def startLogging ( file , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( file , LoggingFile ) : <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> theLogPublisher . _startLogging ( observer , setStdout ) <EOL> msg ( "<STR_LIT>" ) <EOL> class NullFile : <EOL> """<STR_LIT>""" <EOL> softspace = <NUM_LIT:0> <EOL> def read ( self ) : <EOL> """<STR_LIT>""" <EOL> def write ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> def discardLogs ( ) : <EOL> """<STR_LIT>""" <EOL> global logfile <EOL> logfile = NullFile ( ) <EOL> if '<STR_LIT>' not in globals ( ) : <EOL> logfile = LoggingFile ( logger = NewLogger ( ) , <EOL> level = NewLogLevel . info , <EOL> encoding = getattr ( sys . stdout , "<STR_LIT>" , None ) ) <EOL> logerr = LoggingFile ( logger = NewLogger ( ) , <EOL> level = NewLogLevel . error , <EOL> encoding = getattr ( sys . stderr , "<STR_LIT>" , None ) ) <EOL> class DefaultObserver ( _GlobalStartStopMixIn ) : <EOL> """<STR_LIT>""" <EOL> stderr = sys . stderr <EOL> def emit ( self , eventDict ) : <EOL> """<STR_LIT>""" <EOL> if eventDict [ "<STR_LIT>" ] : <EOL> text = textFromEventDict ( eventDict ) <EOL> self . stderr . write ( text ) <EOL> self . stderr . flush ( ) <EOL> if '<STR_LIT>' not in globals ( ) : <EOL> defaultObserver = DefaultObserver ( ) </s>
