diff --git a/src/backends/python3/python3server/python3server.cpp b/src/backends/python3/python3server/python3server.cpp index cb77e970..cb8bb29d 100644 --- a/src/backends/python3/python3server/python3server.cpp +++ b/src/backends/python3/python3server/python3server.cpp @@ -1,62 +1,67 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2015 Minh Ngo */ #include "python3server.h" #include Python3Server::Python3Server(QObject* parent) : QObject(parent) { } +namespace +{ + QString pyObjectToQString(PyObject* obj) + { + return QString::fromUtf8(PyUnicode_AsUTF8(obj)); + } +} + void Python3Server::login() { Py_Initialize(); m_pModule = PyImport_AddModule("__main__"); } void Python3Server::runPythonCommand(const QString& command) const { PyRun_SimpleString(command.toStdString().c_str()); } QString Python3Server::getError() const { PyObject *errorPython = PyObject_GetAttrString(m_pModule, "errorPythonBackend"); PyObject *error = PyObject_GetAttrString(errorPython, "value"); return pyObjectToQString(error); } QString Python3Server::getOutput() const { PyObject *outputPython = PyObject_GetAttrString(m_pModule, "outputPythonBackend"); PyObject *output = PyObject_GetAttrString(outputPython, "value"); return pyObjectToQString(output); } -QString Python3Server::pyObjectToQString(PyObject* obj) const -{ - return QString::fromUtf8(PyUnicode_AsUTF8(obj)); -} + #include "python3server.moc" diff --git a/src/backends/python3/python3server/python3server.h b/src/backends/python3/python3server/python3server.h index d924b357..c8a6e30f 100644 --- a/src/backends/python3/python3server/python3server.h +++ b/src/backends/python3/python3server/python3server.h @@ -1,48 +1,45 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2015 Minh Ngo */ #ifndef _PYTHON3SERVER_H #define _PYTHON3SERVER_H #include #include struct _object; typedef _object PyObject; class Python3Server : public QObject { Q_OBJECT public: Python3Server(QObject* parent = nullptr); public Q_SLOTS: Q_SCRIPTABLE void login(); Q_SCRIPTABLE void runPythonCommand(const QString& command) const; Q_SCRIPTABLE QString getOutput() const; Q_SCRIPTABLE QString getError() const; - private: - QString pyObjectToQString(PyObject* obj) const; - private: PyObject* m_pModule; }; #endif diff --git a/src/backends/python3/python3session.cpp b/src/backends/python3/python3session.cpp index 12e5f106..3573505b 100644 --- a/src/backends/python3/python3session.cpp +++ b/src/backends/python3/python3session.cpp @@ -1,152 +1,140 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2015 Minh Ngo */ #include "python3session.h" #include "settings.h" #include "../python/pythonexpression.h" #include #include #include #include #include Python3Session::Python3Session(Cantor::Backend* backend) : PythonSession(backend) , m_pIface(nullptr) , m_pProcess(nullptr) { } void Python3Session::login() { if (m_pProcess) m_pProcess->deleteLater(); m_pProcess = new KProcess(this); m_pProcess->setOutputChannelMode(KProcess::SeparateChannels); (*m_pProcess) << QStandardPaths::findExecutable(QLatin1String("cantor_python3server")); m_pProcess->start(); m_pProcess->waitForStarted(); m_pProcess->waitForReadyRead(); QTextStream stream(m_pProcess->readAllStandardOutput()); const QString& readyStatus = QString::fromLatin1("ready"); while (m_pProcess->state() == QProcess::Running) { const QString& rl = stream.readLine(); if (rl == readyStatus) break; } if (!QDBusConnection::sessionBus().isConnected()) { qWarning() << "Can't connect to the D-Bus session bus.\n" "To start it, run: eval `dbus-launch --auto-syntax`"; return; } const QString& serviceName = QString::fromLatin1("org.kde.Cantor.Python3-%1").arg(m_pProcess->pid()); m_pIface = new QDBusInterface(serviceName, QString::fromLatin1("/"), QString(), QDBusConnection::sessionBus()); if (!m_pIface->isValid()) { qWarning() << QDBusConnection::sessionBus().lastError().message(); return; } m_pIface->call(QString::fromLatin1("login")); PythonSession::login(); } void Python3Session::logout() { m_pProcess->terminate(); PythonSession::logout(); } void Python3Session::interrupt() { if (m_pProcess->pid()) m_pProcess->kill(); PythonSession::interrupt(); } void Python3Session::runPythonCommand(const QString& command) const { m_pIface->call(QString::fromLatin1("runPythonCommand"), command); } -void Python3Session::runPythonCommandAsync(const QString& command) -{ - m_pIface->callWithCallback(QString::fromLatin1("runPythonCommand"), {command}, - (Python3Session*)this, - SLOT(onResultReady())); -} - void Python3Session::readExpressionOutput(const QString& commandProcessing) { runClassOutputPython(); - runPythonCommandAsync(commandProcessing); - changeStatus(Cantor::Session::Running); -} - -void Python3Session::onResultReady() -{ + runPythonCommand(commandProcessing); m_output = getOutput(); m_error = getError(); updateOutput(); } QString Python3Session::getOutput() const { const QDBusReply& reply = m_pIface->call(QString::fromLatin1("getOutput")); if (reply.isValid()) return reply.value(); return reply.error().message(); } QString Python3Session::getError() const { const QDBusReply& reply = m_pIface->call(QString::fromLatin1("getError")); if (reply.isValid()) return reply.value(); return reply.error().message(); } bool Python3Session::integratePlots() const { return PythonSettings::integratePlots(); } QStringList Python3Session::autorunScripts() const { return PythonSettings::autorunScripts(); } diff --git a/src/backends/python3/python3session.h b/src/backends/python3/python3session.h index 01fbac1c..80bddbec 100644 --- a/src/backends/python3/python3session.h +++ b/src/backends/python3/python3session.h @@ -1,60 +1,56 @@ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2015 Minh Ngo */ #ifndef _PYTHON3SESSION_H #define _PYTHON3SESSION_H #include "../python/pythonsession.h" class QDBusInterface; class KProcess; class Python3Session : public PythonSession { Q_OBJECT public: Python3Session(Cantor::Backend* backend); void login(); void logout(); void interrupt(); bool integratePlots() const; QStringList autorunScripts() const; private: void runPythonCommand(const QString& command) const; - void runPythonCommandAsync(const QString& command); void readExpressionOutput(const QString& commandProcessing); QString getOutput() const; QString getError() const; - private Q_SLOTS: - void onResultReady(); - private: QDBusInterface* m_pIface; KProcess* m_pProcess; Q_SIGNALS: void updateHighlighter(); }; #endif