def __init__(self, mainWindow, console): pConsoleCommand.__init__(self, QStringList()) self.mMainWindow = mainWindow self.setDescription( "ls", pConsole.tr(console, "Execute 'dir' on windows else 'ls' command" ) ) self.setDescription( "echo", pConsole.tr(console, "Simple echo command" ) ) self.setDescription( "quit", pConsole.tr(console, "Quit the application" ) ) self.setDescription( "style", pConsole.tr(console, "Change the application style" ) )
def interpret(self, command): """Interprets command Returns turple (output, exitCode) """ print '~~~~~~~~~ interpret' ec = pConsoleCommand.NotFound output, ec = pConsoleCommand.interpret( command) parts = self.parseCommands( command ) if parts: cmd = parts.takeFirst() else: cmd = '' if ec != pConsoleCommand.NotFound : # nothing to do pass elif cmd == "ls" : if sys.platform.startswith('win'): cmd = "dir %s" % \ " ".join(pConsoleCommand.quotedStringList(parts)).trim() else: cmd = "dir %s" % " ".join( \ pConsoleCommand.quotedStringList(parts)).trimmed() process = QProcess() process.setProcessChannelMode( QProcess.MergedChannels ) process.start( cmd ) process.waitForStarted() process.waitForFinished() output = process.readAll().trimmed() ec = process.exitCode() elif cmd == "echo" : if parts: output = "\n".join(parts) ec = pConsoleCommand.Error else: output = pConsole.tr(console, "No argument given" ) ec = pConsoleCommand.Success elif cmd == "quit": output = pConsole.tr(console, "Quitting the application..." ) ec = pConsoleCommand.Success QTimer.singleShot(1000, qApp.quit() ) elif cmd == "style" : if parts.count() != 1 : output = pConsole.tr(console, "%s take only 1 parameter, %d given" % (cmd, len(parts)) ) ec = pConsoleCommand.Error elif parts[-1] == "list" : output = pConsole.tr(console, "Available styles:\n%s" % '\n'.join(QStyleFactory.keys()) ) ec = pConsoleCommand.Success else: styleExists = parts[-1].lower() in \ [key.lower() for key in QStyleFactory.keys()] if styleExists: output = pConsole.tr(console, "Setting style to %s..." % parts[-1]) self.mMainWindow.setCurrentStyle( parts[-1] ) ec = pConsoleCommand.Success else: output = pConsole.tr(console, "This style does not exists" ) ec = pConsoleCommand.Error return (output, ec)