<s> from PythonQt import QtCore , QtGui <EOL> from director import lcmUtils <EOL> from director . simpletimer import SimpleTimer <EOL> from director . timercallback import TimerCallback <EOL> import subprocess <EOL> import os <EOL> import sys <EOL> class LCMLoggerWidget ( object ) : <EOL> def __init__ ( self , statusBar = None ) : <EOL> self . manager = lcmUtils . LCMLoggerManager ( ) <EOL> self . statusBar = statusBar <EOL> self . lastActiveLogFile = None <EOL> self . numProcesses = <NUM_LIT:0> <EOL> self . numLogFiles = <NUM_LIT:0> <EOL> self . userTag = '<STR_LIT>' <EOL> self . button = QtGui . QPushButton ( '<STR_LIT>' ) <EOL> self . button . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu ) <EOL> self . button . connect ( '<STR_LIT>' , self . showContextMenu ) <EOL> self . button . connect ( '<STR_LIT>' , self . onClick ) <EOL> self . timer = TimerCallback ( targetFps = <NUM_LIT> ) <EOL> self . timer . callback = self . updateState <EOL> self . timer . start ( ) <EOL> def updateState ( self ) : <EOL> t = SimpleTimer ( ) <EOL> self . manager . updateExistingLoggerProcesses ( ) <EOL> activeLogFiles = self . manager . getActiveLogFilenames ( ) <EOL> self . numProcesses = len ( self . manager . getActiveLoggerPids ( ) ) <EOL> self . numLogFiles = len ( activeLogFiles ) <EOL> if self . numLogFiles == <NUM_LIT:1> : <EOL> self . lastActiveLogFile = activeLogFiles [ <NUM_LIT:0> ] <EOL> if self . numProcesses == <NUM_LIT:0> : <EOL> self . button . text = '<STR_LIT>' <EOL> elif self . numProcesses == <NUM_LIT:1> : <EOL> self . button . text = '<STR_LIT>' <EOL> elif self . numProcesses > <NUM_LIT:1> : <EOL> self . button . text = '<STR_LIT>' <EOL> statusDescription = '<STR_LIT>' if self . numProcesses else '<STR_LIT>' <EOL> logFileDescription = self . lastActiveLogFile or '<STR_LIT>' <EOL> self . button . setToolTip ( '<STR_LIT>' % ( statusDescription , logFileDescription ) ) <EOL> def onClick ( self ) : <EOL> if self . numProcesses == <NUM_LIT:0> : <EOL> self . manager . startNewLogger ( tag = self . userTag ) <EOL> self . updateState ( ) <EOL> self . showStatusMessage ( '<STR_LIT>' + self . lastActiveLogFile ) <EOL> else : <EOL> self . manager . killAllLoggingProcesses ( ) <EOL> self . showStatusMessage ( '<STR_LIT>' ) <EOL> self . updateState ( ) <EOL> def showStatusMessage ( self , msg , timeout = <NUM_LIT> ) : <EOL> if self . statusBar : <EOL> self . statusBar . showMessage ( msg , timeout ) <EOL> def showContextMenu ( self , clickPosition ) : <EOL> globalPos = self . button . mapToGlobal ( clickPosition ) <EOL> menu = QtGui . QMenu ( ) <EOL> action = menu . addAction ( '<STR_LIT>' ) <EOL> action . enabled = ( self . numProcesses > <NUM_LIT:0> ) <EOL> action = menu . addAction ( '<STR_LIT>' ) <EOL> action . enabled = ( self . numProcesses > <NUM_LIT:0> and self . lastActiveLogFile ) <EOL> action = menu . addAction ( '<STR_LIT>' ) <EOL> action . enabled = ( self . numProcesses == <NUM_LIT:0> ) <EOL> action = menu . addAction ( '<STR_LIT>' ) <EOL> action . enabled = ( self . lastActiveLogFile is not None ) <EOL> action = menu . addAction ( '<STR_LIT>' ) <EOL> action . enabled = ( self . lastActiveLogFile is not None ) <EOL> selectedAction = menu . exec_ ( globalPos ) <EOL> if selectedAction is None : <EOL> return <EOL> if selectedAction . text == '<STR_LIT>' : <EOL> clipboard = QtGui . QApplication . instance ( ) . clipboard ( ) <EOL> clipboard . setText ( self . lastActiveLogFile ) <EOL> self . showStatusMessage ( '<STR_LIT>' + self . lastActiveLogFile ) <EOL> elif selectedAction . text == '<STR_LIT>' : <EOL> self . manager . killAllLoggingProcesses ( ) <EOL> self . showStatusMessage ( '<STR_LIT>' ) <EOL> self . updateState ( ) <EOL> elif selectedAction . text == '<STR_LIT>' : <EOL> logFileToRemove = self . lastActiveLogFile <EOL> self . manager . killAllLoggingProcesses ( ) <EOL> self . updateState ( ) <EOL> os . remove ( logFileToRemove ) <EOL> self . showStatusMessage ( '<STR_LIT>' + logFileToRemove ) <EOL> elif selectedAction . text == '<STR_LIT>' : <EOL> inputDialog = QtGui . QInputDialog ( ) <EOL> inputDialog . setInputMode ( inputDialog . TextInput ) <EOL> inputDialog . setLabelText ( '<STR_LIT>' ) <EOL> inputDialog . setWindowTitle ( '<STR_LIT>' ) <EOL> inputDialog . setTextValue ( self . userTag ) <EOL> result = inputDialog . exec_ ( ) <EOL> if result : <EOL> tag = inputDialog . textValue ( ) <EOL> self . userTag = tag <EOL> self . showStatusMessage ( '<STR_LIT>' + self . userTag ) <EOL> elif selectedAction . text == '<STR_LIT>' : <EOL> newEnv = dict ( os . environ ) <EOL> newEnv [ '<STR_LIT>' ] = newEnv [ '<STR_LIT>' ] <EOL> devnull = open ( os . devnull , '<STR_LIT:w>' ) <EOL> subprocess . Popen ( sys . argv , stdout = devnull , stderr = devnull , env = newEnv ) <EOL> subprocess . Popen ( [ '<STR_LIT>' , self . lastActiveLogFile ] , stdout = devnull , stderr = devnull , env = newEnv ) <EOL> subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , stdout = devnull , stderr = devnull , env = newEnv ) </s>
