Example #1
0
 def runx(self):
     self._com = Dispatcher(moduleName='client-%s-%d' % (platform.node(), os.getpid()))
     self._proxy = DaemonProxy(self._opts,
                               dispatcher=self._com,
                               lineHandler=self.handleDaemonLine,
                               connectHandler=self.handleDaemonConnect)
     self._proxy.open()
     self._com.connect('console:', lineHandler=self.handleStdinLine)
     self._printIfInteractive('example commands: "start bc", "stop bc", "get status.bc"\n')
     self._prompt()
     self._com.runForever()
Example #2
0
 def __init__(self,
              opts=None,
              dispatcher=None,
              lineHandler=None,
              connectHandler=None,
              **kwargs):
     if dispatcher == None:
         dispatcher = Dispatcher(moduleName='client-%s-%d' %
                                 (platform.node(), os.getpid()))
     if opts == None:
         opts, _args = commandLineOptions.getClientOptsArgs(['pyrterm'])
     for k, v in kwargs.iteritems():
         setattr(opts, k, v)
     self._opts = opts
     self._lineHandler = lineHandler
     self._connectHandler = connectHandler
     self._dispatcher = dispatcher
     _serverLoc = opts.daemons[0].strip()
     self._conns = {}
     self._currentConn = None
     self._counter = 0
     self._responseIdToWaitFor = None
     self._opened = False
     self._lastMatchingMessage = None
Example #3
0
class Client:
    def __init__(self, opts=None):
        self._opts = opts
        self._needPrompt = True
        self._com = None
        self._proxy = None

    def _printIfInteractive(self, text):
        if self._opts.startupCommand == None:
            sys.stdout.write(text)
            sys.stdout.flush()

    def _prompt(self):
        self._printIfInteractive('pyrterm> ')

    def _queuePrompt(self):
        self._needPrompt = True
        scheduler.enterSimple(delay=0.05, action=self._checkPrompt)

    def _checkPrompt(self):
        if self._needPrompt:
            self._prompt()
            self._needPrompt = False

    def handleStdinLine(self, sock, line):
        if line == 'q' or line == 'quit':
            print 'user quit'
            raise ExitSchedulerLoop(None)
        elif line:
            self._proxy.send(line)
        else:
            self._prompt()

    def handleDaemonConnect(self, sock):
        if self._opts.startupCommand != None:
            self._proxy.send(self._opts.startupCommand)
            raise ExitSchedulerLoop(None)

    def makeStatusHumanReadable(self, result):
        statusDict = result['status']
        statusPairs = statusDict.items()
        statusPairs.sort(key=lambda pair: pair[0].lower())
        if len(statusPairs) == 1:
            return '%s' % statusPairs[0][1]
        else:
            out = StringIO()
            out.write('\n')
            for taskName, info in statusPairs:
                secondaryInfo = ['%s=%s' % (k, info[k])
                                 for k in ('pid', 'sigName', 'sigVerbose', 'returnValue')
                                 if k in info]
                out.write('  %-20s %-11s %s\n' % (taskName, info['procStatus'],
                                               ' '.join(secondaryInfo)))
            out.write('\n')
            out.write('  get status.<taskName> for more details\n')
            return out.getvalue()

    def makeOkResponseHumanReadable(self, result):
        if isinstance(result, dict) and len(result) == 1:
            if 'status' in result:
                return self.makeStatusHumanReadable(result)
            else:
                return '%s' % result.values()[0]
        else:
            if result == None:
                return 'ok'
            else:
                return 'ok %s' % result

    def makeHumanReadable(self, line):
        if line.startswith('#'):
            return line
        else:
            cmd = json.loads(line)
            if (cmd and cmd[0] == 'response'):
                _response, _lineId, returnCode = cmd[:3]
                result = cmd[3:]
                if returnCode == 'ok':
                    return self.makeOkResponseHumanReadable(result[0])
                else:
                    return ' '.join([returnCode] + result)
            else:
                return line

    def handleDaemonLine(self, sock, line):
        displayText = self.makeHumanReadable(line)
        sys.stdout.write(displayText + '\n')
        self._queuePrompt()

    def runx(self):
        self._com = Dispatcher(moduleName='client-%s-%d' % (platform.node(), os.getpid()))
        self._proxy = DaemonProxy(self._opts,
                                  dispatcher=self._com,
                                  lineHandler=self.handleDaemonLine,
                                  connectHandler=self.handleDaemonConnect)
        self._proxy.open()
        self._com.connect('console:', lineHandler=self.handleStdinLine)
        self._printIfInteractive('example commands: "start bc", "stop bc", "get status.bc"\n')
        self._prompt()
        self._com.runForever()

    @staticmethod
    def run(argv):
        opts, _args = commandLineOptions.getClientOptsArgs(argv)
        Client(opts).runx()