예제 #1
0
  public String processInput(String input) {
    String output;
    boolean moreInputRequired;

    if (input == null) {
      // '\nHello {0}\nWelcome to the Ganymede Jython interpreter!\n\nType "quit" to exit.\n{1}'
      return ts.l("processInput.greeting", socket.getInetAddress().getHostAddress(), prompt);
    }

    if (input.equals(ts.l("processInput.quitcommand"))) {
      return doneString;
    }

    try {
      moreInputRequired = interp.push(input);
      if (moreInputRequired) {
        return "... ";
      }

      buffer.flush();
      output = buffer.toString();
      interp.resetbuffer();
      buffer.getBuffer().setLength(0);
    } catch (PyException pex) {
      output = buffer.toString() + "\n" + pex.toString();
      interp.resetbuffer();
      buffer.getBuffer().setLength(0);
    }

    return output + prompt;
  }
예제 #2
0
 public void evalFile(String s) {
   for (String s2 : bindings.keySet()) {
     py.set(s2, bindings.get(s2));
   }
   py.setOut(getWriter());
   py.setErr(getErrorWriter());
   try {
     py.execfile(s);
     PyStringMap locals = (PyStringMap) py.getLocals();
     Object[] values = locals.values().toArray();
     Object[] keys = locals.keys().toArray();
     bindings.clear();
     for (int i = 0; i < keys.length; ++i) {
       bindings.put((String) keys[i], values[i]);
     }
   } catch (PyException pe) {
     getErrorWriter().write(pe.toString());
     getErrorWriter().flush();
   }
 }
  @Override
  public boolean sendLine(String command, Consumer<String> callback) {
    if (interactiveInterpreter == null) {
      try {
        interactiveInterpreter =
            jythonJythonInterpreterFactory.createInstance(InteractiveInterpreter.class);
        interactiveInterpreter.setOut(returnBuffer);
        interactiveInterpreter.setErr(returnBuffer);
      } catch (PlayOnLinuxException e) {
        LOGGER.error(e);
      }
    }

    commandBuffer.append(command);

    if (command.startsWith("\t") || command.startsWith(" ") || command.trim().endsWith(":")) {
      commandBuffer.append("\n");
      callback.accept("");
      return false;
    } else {
      String completeCommand = commandBuffer.toString();
      commandBuffer.setLength(0);
      currentTask =
          executorService.submit(
              () -> {
                returnBuffer.getBuffer().setLength(0);
                try {
                  interactiveInterpreter.exec(completeCommand);
                  callback.accept(returnBuffer.toString());
                } catch (PyException e) {
                  LOGGER.debug(e);
                  callback.accept(e.toString());
                }
              });
      return true;
    }
  }