From 95bd328c31432a952c58dbec7dc460b9fcafc313 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 24 Oct 2019 14:28:58 +0200 Subject: [PATCH] Python: export Interpreter generator Change-Id: I591bd6c14706e1699f028a9a3a6dfd9b89eec66a Reviewed-by: Christian Stenger --- src/plugins/python/pythonsettings.cpp | 24 ++++++++++++++---------- src/plugins/python/pythonsettings.h | 10 +++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/plugins/python/pythonsettings.cpp b/src/plugins/python/pythonsettings.cpp index be1ad7b4ee9..d9ab47093c9 100644 --- a/src/plugins/python/pythonsettings.cpp +++ b/src/plugins/python/pythonsettings.cpp @@ -272,15 +272,14 @@ static bool alreadyRegistered(const QList &pythons, const FilePath }); } -Interpreter interpreterForPythonExecutable(const FilePath &python, - const QString &defaultName, - bool windowedSuffix = false) +Interpreter::Interpreter(const FilePath &python, const QString &defaultName, bool windowedSuffix) + : id(QUuid::createUuid().toString()) + , command(python) { SynchronousProcess pythonProcess; pythonProcess.setProcessChannelMode(QProcess::MergedChannels); SynchronousProcessResponse response = pythonProcess.runBlocking( CommandLine(python, {"--version"})); - QString name; if (response.result == SynchronousProcessResponse::Finished) name = response.stdOut().trimmed(); if (name.isEmpty()) @@ -290,9 +289,14 @@ Interpreter interpreterForPythonExecutable(const FilePath &python, QDir pythonDir(python.parentDir().toString()); if (pythonDir.exists() && pythonDir.exists("activate") && pythonDir.cdUp()) name += QString(" (%1 Virtual Environment)").arg(pythonDir.dirName()); - return Interpreter{QUuid::createUuid().toString(), name, python}; } +Interpreter::Interpreter(const QString &_id, const QString &_name, const FilePath &_command) + : id(_id) + , name(_name) + , command(_command) +{} + static InterpreterOptionsPage &interpreterOptionsPage() { static InterpreterOptionsPage page; @@ -388,11 +392,11 @@ static void addPythonsFromRegistry(QList &pythons) const FilePath &path = FilePath::fromUserInput(regVal.toString()); const FilePath &python = path.pathAppended(HostOsInfo::withExecutableSuffix("python")); if (python.exists() && !alreadyRegistered(pythons, python)) - pythons << interpreterForPythonExecutable(python, "Python " + versionGroup); + pythons << Interpreter(python, "Python " + versionGroup); const FilePath &pythonw = path.pathAppended( HostOsInfo::withExecutableSuffix("pythonw")); if (pythonw.exists() && !alreadyRegistered(pythons, pythonw)) - pythons << interpreterForPythonExecutable(pythonw, "Python " + versionGroup, true); + pythons << Interpreter(pythonw, "Python " + versionGroup, true); } pythonRegistry.endGroup(); } @@ -405,11 +409,11 @@ static void addPythonsFromPath(QList &pythons) if (HostOsInfo::isWindowsHost()) { for (const FilePath &executable : env.findAllInPath("python")) { if (executable.exists() && !alreadyRegistered(pythons, executable)) - pythons << interpreterForPythonExecutable(executable, "Python from Path"); + pythons << Interpreter(executable, "Python from Path"); } for (const FilePath &executable : env.findAllInPath("pythonw")) { if (executable.exists() && !alreadyRegistered(pythons, executable)) - pythons << interpreterForPythonExecutable(executable, "Python from Path", true); + pythons << Interpreter(executable, "Python from Path", true); } } else { const QStringList filters = {"python", @@ -421,7 +425,7 @@ static void addPythonsFromPath(QList &pythons) for (const QFileInfo &fi : dir.entryInfoList(filters)) { const FilePath executable = Utils::FilePath::fromFileInfo(fi); if (executable.exists() && !alreadyRegistered(pythons, executable)) - pythons << interpreterForPythonExecutable(executable, "Python from Path"); + pythons << Interpreter(executable, "Python from Path"); } } } diff --git a/src/plugins/python/pythonsettings.h b/src/plugins/python/pythonsettings.h index 583d4fb212f..7ca5218ae39 100644 --- a/src/plugins/python/pythonsettings.h +++ b/src/plugins/python/pythonsettings.h @@ -35,7 +35,15 @@ namespace Internal { class Interpreter { public: - QString id; + Interpreter() = default; + Interpreter(const Utils::FilePath &python, + const QString &defaultName, + bool windowedSuffix = false); + Interpreter(const QString &id, + const QString &name, + const Utils::FilePath &command); + + QString id = QUuid::createUuid().toString(); QString name; Utils::FilePath command; };