Changeset View
Changeset View
Standalone View
Standalone View
src/backends/python/pythonsession.cpp
Show All 15 Lines | 1 | /* | |||
---|---|---|---|---|---|
16 | 16 | | |||
17 | --- | 17 | --- | ||
18 | Copyright (C) 2012 Filipe Saraiva <filipe@kde.org> | 18 | Copyright (C) 2012 Filipe Saraiva <filipe@kde.org> | ||
19 | Copyright (C) 2015 Minh Ngo <minh@fedoraproject.org> | 19 | Copyright (C) 2015 Minh Ngo <minh@fedoraproject.org> | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "pythonsession.h" | 22 | #include "pythonsession.h" | ||
23 | #include "pythonexpression.h" | 23 | #include "pythonexpression.h" | ||
24 | #include "pythonvariablemodel.h" | ||||
24 | #include "pythonhighlighter.h" | 25 | #include "pythonhighlighter.h" | ||
25 | #include "pythoncompletionobject.h" | 26 | #include "pythoncompletionobject.h" | ||
26 | #include "pythonkeywords.h" | 27 | #include "pythonkeywords.h" | ||
27 | #include "pythonutils.h" | 28 | #include "pythonutils.h" | ||
28 | 29 | | |||
29 | #include <QDebug> | 30 | #include <QDebug> | ||
30 | #include <QDir> | 31 | #include <QDir> | ||
31 | 32 | | |||
32 | #include <QStandardPaths> | 33 | #include <QStandardPaths> | ||
33 | #include <QDBusConnection> | 34 | #include <QDBusConnection> | ||
34 | #include <QDBusInterface> | 35 | #include <QDBusInterface> | ||
35 | #include <QDBusReply> | 36 | #include <QDBusReply> | ||
36 | 37 | | |||
37 | #include <KProcess> | 38 | #include <KProcess> | ||
38 | 39 | | |||
39 | #include <KDirWatch> | 40 | #include <KDirWatch> | ||
40 | 41 | | |||
41 | #include <defaultvariablemodel.h> | 42 | #include <defaultvariablemodel.h> | ||
42 | 43 | | |||
43 | 44 | | |||
44 | PythonSession::PythonSession(Cantor::Backend* backend, int pythonVersion, const QString serverName, const QString DbusChannelName) | 45 | PythonSession::PythonSession(Cantor::Backend* backend, int pythonVersion, const QString serverName, const QString DbusChannelName) | ||
45 | : Session(backend) | 46 | : Session(backend) | ||
46 | , m_variableModel(new Cantor::DefaultVariableModel(this)) | 47 | , m_variableModel(new PythonVariableModel(this)) | ||
47 | , m_currentExpression(nullptr) | 48 | , m_currentExpression(nullptr) | ||
48 | , m_pIface(nullptr) | 49 | , m_pIface(nullptr) | ||
49 | , m_pProcess(nullptr) | 50 | , m_pProcess(nullptr) | ||
50 | , serverName(serverName) | 51 | , serverName(serverName) | ||
51 | , DbusChannelName(DbusChannelName) | 52 | , DbusChannelName(DbusChannelName) | ||
52 | , m_pythonVersion(pythonVersion) | 53 | , m_pythonVersion(pythonVersion) | ||
54 | , m_needUpdate(false) | ||||
53 | { | 55 | { | ||
54 | } | 56 | } | ||
55 | 57 | | |||
56 | void PythonSession::login() | 58 | void PythonSession::login() | ||
57 | { | 59 | { | ||
58 | qDebug()<<"login"; | 60 | qDebug()<<"login"; | ||
59 | emit loginStarted(); | 61 | emit loginStarted(); | ||
60 | 62 | | |||
Show All 32 Lines | |||||
93 | m_pIface = new QDBusInterface(serviceName, | 95 | m_pIface = new QDBusInterface(serviceName, | ||
94 | QString::fromLatin1("/"), QString(), QDBusConnection::sessionBus()); | 96 | QString::fromLatin1("/"), QString(), QDBusConnection::sessionBus()); | ||
95 | if (!m_pIface->isValid()) | 97 | if (!m_pIface->isValid()) | ||
96 | { | 98 | { | ||
97 | qWarning() << QDBusConnection::sessionBus().lastError().message(); | 99 | qWarning() << QDBusConnection::sessionBus().lastError().message(); | ||
98 | return; | 100 | return; | ||
99 | } | 101 | } | ||
100 | 102 | | |||
103 | m_variableModel->setPythonServer(m_pIface); | ||||
104 | | ||||
101 | m_pIface->call(QString::fromLatin1("login")); | 105 | m_pIface->call(QString::fromLatin1("login")); | ||
102 | m_pIface->call(QString::fromLatin1("setFilePath"), worksheetPath); | 106 | m_pIface->call(QString::fromLatin1("setFilePath"), worksheetPath); | ||
103 | 107 | | |||
104 | const QStringList& scripts = autorunScripts(); | 108 | const QStringList& scripts = autorunScripts(); | ||
105 | if(!scripts.isEmpty()){ | 109 | if(!scripts.isEmpty()){ | ||
106 | QString autorunScripts = scripts.join(QLatin1String("\n")); | 110 | QString autorunScripts = scripts.join(QLatin1String("\n")); | ||
107 | getPythonCommandOutput(autorunScripts); | 111 | getPythonCommandOutput(autorunScripts); | ||
112 | m_variableModel->update(); | ||||
108 | } | 113 | } | ||
109 | 114 | | |||
110 | const QString& importerFile = QLatin1String(":py/import_default_modules.py"); | 115 | const QString& importerFile = QLatin1String(":py/import_default_modules.py"); | ||
111 | 116 | | |||
112 | evaluateExpression(fromSource(importerFile), Cantor::Expression::DeleteOnFinish, true); | 117 | evaluateExpression(fromSource(importerFile), Cantor::Expression::DeleteOnFinish, true); | ||
113 | 118 | | |||
114 | listVariables(); | | |||
115 | | ||||
116 | changeStatus(Session::Done); | 119 | changeStatus(Session::Done); | ||
117 | emit loginDone(); | 120 | emit loginDone(); | ||
118 | } | 121 | } | ||
119 | 122 | | |||
120 | void PythonSession::logout() | 123 | void PythonSession::logout() | ||
121 | { | 124 | { | ||
122 | // TODO: T6113, T6114 | 125 | // TODO: T6113, T6114 | ||
123 | m_pProcess->terminate(); | 126 | m_pProcess->terminate(); | ||
124 | 127 | | |||
125 | m_variableModel->clearVariables(); | 128 | m_variableModel->clearVariables(); | ||
126 | emit clearVariables(); | | |||
127 | 129 | | |||
128 | qDebug()<<"logout"; | 130 | qDebug()<<"logout"; | ||
129 | changeStatus(Status::Disable); | 131 | changeStatus(Status::Disable); | ||
130 | } | 132 | } | ||
131 | 133 | | |||
132 | void PythonSession::interrupt() | 134 | void PythonSession::interrupt() | ||
133 | { | 135 | { | ||
134 | // TODO: T6113, T6114 | 136 | // TODO: T6113, T6114 | ||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Line(s) | 278 | { | |||
278 | PythonExpression* expression = qobject_cast<PythonExpression*>(sender()); | 280 | PythonExpression* expression = qobject_cast<PythonExpression*>(sender()); | ||
279 | 281 | | |||
280 | m_runningExpressions.removeAll(expression); | 282 | m_runningExpressions.removeAll(expression); | ||
281 | qDebug() << "size: " << m_runningExpressions.size(); | 283 | qDebug() << "size: " << m_runningExpressions.size(); | ||
282 | } | 284 | } | ||
283 | 285 | | |||
284 | void PythonSession::updateOutput() | 286 | void PythonSession::updateOutput() | ||
285 | { | 287 | { | ||
288 | m_needUpdate |= !m_currentExpression->isInternal(); | ||||
286 | if(m_error.isEmpty()){ | 289 | if(m_error.isEmpty()){ | ||
287 | m_currentExpression->parseOutput(m_output); | 290 | m_currentExpression->parseOutput(m_output); | ||
288 | 291 | | |||
289 | qDebug() << "output: " << m_output; | 292 | qDebug() << "output: " << m_output; | ||
290 | } else { | 293 | } else { | ||
291 | m_currentExpression->parseError(m_error); | 294 | m_currentExpression->parseError(m_error); | ||
292 | 295 | | |||
293 | qDebug() << "error: " << m_error; | 296 | qDebug() << "error: " << m_error; | ||
294 | } | 297 | } | ||
295 | 298 | | |||
296 | listVariables(); | 299 | if (m_needUpdate) | ||
300 | { | ||||
301 | m_variableModel->update(); | ||||
302 | m_needUpdate = false; | ||||
303 | } | ||||
297 | 304 | | |||
298 | changeStatus(Cantor::Session::Done); | 305 | changeStatus(Cantor::Session::Done); | ||
299 | } | 306 | } | ||
300 | 307 | | |||
301 | void PythonSession::readOutput(const QString& commandProcessing) | 308 | void PythonSession::readOutput(const QString& commandProcessing) | ||
302 | { | 309 | { | ||
303 | qDebug() << "readOutput"; | 310 | qDebug() << "readOutput"; | ||
304 | 311 | | |||
305 | getPythonCommandOutput(commandProcessing); | 312 | getPythonCommandOutput(commandProcessing); | ||
306 | 313 | | |||
307 | updateOutput(); | 314 | updateOutput(); | ||
308 | } | 315 | } | ||
309 | 316 | | |||
310 | void PythonSession::listVariables() | | |||
311 | { | | |||
312 | const QString& listVariableCommand = QLatin1String( | | |||
313 | "try: \n" | | |||
314 | " import numpy \n" | | |||
315 | " __cantor_numpy_internal__ = numpy.get_printoptions()['threshold'] \n" | | |||
316 | " numpy.set_printoptions(threshold=100000000) \n" | | |||
317 | "except ModuleNotFoundError: \n" | | |||
318 | " pass \n" | | |||
319 | | ||||
320 | "print(globals()) \n" | | |||
321 | | ||||
322 | "try: \n" | | |||
323 | " import numpy \n" | | |||
324 | " numpy.set_printoptions(threshold=__cantor_numpy_internal__) \n" | | |||
325 | " del __cantor_numpy_internal__ \n" | | |||
326 | "except ModuleNotFoundError: \n" | | |||
327 | " pass \n" | | |||
328 | ); | | |||
329 | | ||||
330 | getPythonCommandOutput(listVariableCommand); | | |||
331 | | ||||
332 | qDebug() << m_output; | | |||
333 | | ||||
334 | m_output.remove(QLatin1String("{")); | | |||
335 | m_output.remove(QLatin1String("<")); | | |||
336 | m_output.remove(QLatin1String(">")); | | |||
337 | m_output.remove(QLatin1String("}")); | | |||
338 | | ||||
339 | foreach(QString line, m_output.split(QLatin1String(", '"))){ | | |||
340 | | ||||
341 | QStringList parts = line.simplified().split(QLatin1String(":")); | | |||
342 | const QString& first = parts.first(); | | |||
343 | const QString& last = parts.last(); | | |||
344 | if(!first.startsWith(QLatin1String("'__")) && !first.startsWith(QLatin1String("__")) && !first.startsWith(QLatin1String("CatchOutPythonBackend'")) && | | |||
345 | !first.startsWith(QLatin1String("errorPythonBackend'")) && !first.startsWith(QLatin1String("outputPythonBackend'")) && | | |||
346 | !last.startsWith(QLatin1String(" class ")) && !last.startsWith(QLatin1String(" function ")) && | | |||
347 | !last.startsWith(QLatin1String(" module '")) /*skip imported modules*/ ) | | |||
348 | { | | |||
349 | | ||||
350 | m_variableModel->addVariable(parts.first().remove(QLatin1String("'")).simplified(), parts.last().simplified()); | | |||
351 | emit newVariable(parts.first().remove(QLatin1String("'")).simplified()); | | |||
352 | } | | |||
353 | } | | |||
354 | | ||||
355 | emit updateHighlighter(); | | |||
356 | } | | |||
357 | | ||||
358 | QSyntaxHighlighter* PythonSession::syntaxHighlighter(QObject* parent) | 317 | QSyntaxHighlighter* PythonSession::syntaxHighlighter(QObject* parent) | ||
359 | { | 318 | { | ||
360 | PythonHighlighter* highlighter = new PythonHighlighter(parent, m_pythonVersion); | 319 | PythonHighlighter* highlighter = new PythonHighlighter(parent, m_pythonVersion); | ||
361 | QObject::connect(this, SIGNAL(updateHighlighter()), highlighter, SLOT(updateHighlight())); | 320 | connect ( m_variableModel, &Cantor::DefaultVariableModel::variablesAdded, highlighter, &PythonHighlighter::addUserVariable); | ||
362 | QObject::connect(this, SIGNAL(newVariable(QString)), highlighter, SLOT(addVariable(QString))); | 321 | connect ( m_variableModel, &Cantor::DefaultVariableModel::variablesRemoved, highlighter, &PythonHighlighter::removeUserVariable); | ||
363 | connect(this, &PythonSession::clearVariables, highlighter, &PythonHighlighter::clearVariables); | | |||
364 | 322 | | |||
365 | return highlighter; | 323 | return highlighter; | ||
366 | } | 324 | } | ||
367 | 325 | | |||
368 | Cantor::CompletionObject* PythonSession::completionFor(const QString& command, int index) | 326 | Cantor::CompletionObject* PythonSession::completionFor(const QString& command, int index) | ||
369 | { | 327 | { | ||
370 | return new PythonCompletionObject(command, index, this); | 328 | return new PythonCompletionObject(command, index, this); | ||
371 | } | 329 | } | ||
Show All 36 Lines |