PY-18029 Extract Python console script interaction to a separate class

Create `PydevConsoleCli` with setup methods for client and server modes of Python console script.
This commit is contained in:
Alexander Koshevoy
2018-08-22 23:16:40 +03:00
parent 25206955ba
commit 4c4e4d1a63
2 changed files with 85 additions and 8 deletions
@@ -0,0 +1,84 @@
@file:JvmName("PydevConsoleCli")
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.console
import com.intellij.execution.ExecutionException
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.configurations.ParamsGroup
import com.jetbrains.python.PythonHelper
import com.jetbrains.python.run.PythonCommandLineState
const val MODE_OPTION = "mode"
const val MODE_OPTION_SERVER_VALUE = "server"
const val MODE_OPTION_CLIENT_VALUE = "client"
const val PORT_OPTION = "port"
private fun getOptionString(name: String, value: Any): String = "--$name=$value"
/**
* Adds new or replaces existing [PythonCommandLineState.GROUP_SCRIPT]
* parameters of [this] command line with the path to Python console script
* (*pydevconsole.py*) and parameters required for running it in the *client*
* mode.
*
* @param port the port that Python console script will connect to
*
* @see PythonHelper.CONSOLE
*/
fun GeneralCommandLine.setupPythonConsoleScriptInClientMode(port: Int) {
initializePydevConsoleScriptGroup().appendClientModeParameters(port)
}
/**
* Adds new or replaces existing [PythonCommandLineState.GROUP_SCRIPT]
* parameters of [this] command line with the path to Python console script
* (*pydevconsole.py*) and parameters required for running it in the *server*
* mode.
*
* @param port the optional port that Python console script will listen at
*
* @see PythonHelper.CONSOLE
*/
@JvmOverloads
fun GeneralCommandLine.setupPythonConsoleScriptInServerMode(port: Int? = null) {
initializePydevConsoleScriptGroup().appendServerModeParameters(port)
}
private fun GeneralCommandLine.initializePydevConsoleScriptGroup(): ParamsGroup {
val group: ParamsGroup = parametersList.getParamsGroup(PythonCommandLineState.GROUP_SCRIPT)?.apply { parametersList.clearAll() }
?: parametersList.addParamsGroup(PythonCommandLineState.GROUP_SCRIPT)
PythonHelper.CONSOLE.addToGroup(group, this)
return group
}
private fun ParamsGroup.appendServerModeParameters(port: Int? = null) {
addParameter(getOptionString(MODE_OPTION, MODE_OPTION_SERVER_VALUE))
port?.let { addParameter(getOptionString(PORT_OPTION, it)) }
}
private fun ParamsGroup.appendClientModeParameters(port: Int) {
addParameter(getOptionString(MODE_OPTION, MODE_OPTION_CLIENT_VALUE))
addParameter(getOptionString(PORT_OPTION, port))
}
/**
* Waits for Python console server to be started. The indication for this is
* the server port that Python console script outputs to *stdout* when the
* server socket is bound to the port and it is listening to it.
*
* The connection to Python console script server should be established *after*
* this method finishes.
*
* @throws ExecutionException if timeout occurred or an other error
*
* @see PydevConsoleRunnerImpl.PORTS_WAITING_TIMEOUT
* @see PydevConsoleRunnerImpl.getRemotePortFromProcess
*/
@Throws(ExecutionException::class)
fun waitForPythonConsoleServerToBeStarted(process: Process) {
PydevConsoleRunnerImpl.getRemotePortFromProcess(process)
}
@@ -69,7 +69,6 @@ import com.intellij.xdebugger.XDebugProcess;
import com.intellij.xdebugger.XDebugProcessStarter;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.jetbrains.python.PythonHelper;
import com.jetbrains.python.console.actions.ShowVarsAction;
import com.jetbrains.python.console.pydev.ConsoleCommunicationListener;
import com.jetbrains.python.debugger.PyDebugRunner;
@@ -361,13 +360,7 @@ public class PydevConsoleRunnerImpl implements PydevConsoleRunner {
exeGroup.addParametersString(runParams.getInterpreterOptions());
}
ParamsGroup group = cmd.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
if (group == null) {
group = cmd.getParametersList().addParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
}
PythonHelper.CONSOLE.addToGroup(group, cmd);
group.addParameters("--mode=client", "--port=" + port);
PydevConsoleCli.setupPythonConsoleScriptInClientMode(cmd, port);
return cmd;
}