File indexing completed on 2023-05-30 09:03:12

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Filipe Saraiva <filipe@kde.org>
0004 */
0005 
0006 #include "pythonexpression.h"
0007 
0008 #include <config-cantorlib.h>
0009 
0010 #include "textresult.h"
0011 #include "imageresult.h"
0012 #include "helpresult.h"
0013 #include "session.h"
0014 #include "settings.h"
0015 
0016 #include <QDebug>
0017 
0018 #include "pythonsession.h"
0019 
0020 PythonExpression::PythonExpression(Cantor::Session* session, bool internal) : Cantor::Expression(session, internal)
0021 {
0022 }
0023 
0024 PythonExpression::~PythonExpression() {
0025 }
0026 
0027 void PythonExpression::evaluate()
0028 {
0029     session()->enqueueExpression(this);
0030 }
0031 
0032 QString PythonExpression::internalCommand()
0033 {
0034     QString cmd = command();
0035 
0036     if (PythonSettings::integratePlots())
0037     {
0038         auto* pySession = static_cast<PythonSession*>(session());
0039         const QString& filepath = pySession->plotFilePrefixPath() + QString::number(pySession->plotFileCounter()) + QLatin1String(".png");
0040 
0041         for(const auto& package : session()->enabledGraphicPackages())
0042         {
0043             if (package.isHavePlotCommand())
0044             {
0045                 cmd.append(QLatin1String("\n"));
0046                 cmd.append(package.savePlotCommand(filepath, pySession->plotFileCounter()));
0047             }
0048         }
0049     }
0050 
0051     QStringList commandLine = cmd.split(QLatin1String("\n"));
0052     QString commandProcessing;
0053 
0054     for(const QString& command : commandLine){
0055         const QString firstLineWord = command.trimmed().replace(QLatin1String("("), QLatin1String(" "))
0056             .split(QLatin1String(" ")).at(0);
0057 
0058         // Ignore comments
0059         if (firstLineWord.length() != 0 && firstLineWord[0] == QLatin1Char('#')){
0060 
0061             commandProcessing += command + QLatin1String("\n");
0062             continue;
0063         }
0064 
0065         if(firstLineWord.contains(QLatin1String("execfile"))){
0066 
0067             commandProcessing += command;
0068             continue;
0069         }
0070 
0071         commandProcessing += command + QLatin1String("\n");
0072 
0073     }
0074 
0075     return commandProcessing;
0076 }
0077 
0078 void PythonExpression::parseOutput(const QString& output)
0079 {
0080     qDebug() << "expression output: " << output;
0081     if(command().simplified().startsWith(QLatin1String("help(")))
0082     {
0083         QString resultStr = output;
0084         setResult(new Cantor::HelpResult(resultStr.remove(output.lastIndexOf(QLatin1String("None")), 4)));
0085     }
0086     else if (!output.isEmpty())
0087     {
0088         auto* pySession = static_cast<PythonSession*>(session());
0089         const QString& plotFilePrefixPath = pySession->plotFilePrefixPath();
0090         const QString& searchPrefixPath = QLatin1String("INNER PLOT INFO CANTOR: ") + plotFilePrefixPath;
0091 
0092         QStringList buffer;
0093         const QStringList& lines = output.split(QLatin1String("\n"));
0094         for (const QString& line : lines)
0095         {
0096             if (line.startsWith(searchPrefixPath))
0097             {
0098                 if (!buffer.isEmpty() && !(buffer.size() == 1 && buffer[0].isEmpty()))
0099                     addResult(new Cantor::TextResult(buffer.join(QLatin1String("\n"))));
0100 
0101                 QString filepath = plotFilePrefixPath + QString::number(pySession->plotFileCounter()) + QLatin1String(".png");
0102                 pySession->plotFileCounter()++;
0103                 addResult(new Cantor::ImageResult(QUrl::fromLocalFile(filepath)));
0104                 buffer.clear();
0105             }
0106             else
0107                 buffer.append(line);
0108         }
0109         if (!buffer.isEmpty() && !(buffer.size() == 1 && buffer[0].isEmpty()))
0110             addResult(new Cantor::TextResult(buffer.join(QLatin1String("\n"))));
0111     }
0112 
0113     setStatus(Cantor::Expression::Done);
0114 }
0115 
0116 void PythonExpression::parseError(const QString& error)
0117 {
0118     qDebug() << "expression error: " << error;
0119     setErrorMessage(error);
0120 
0121     setStatus(Cantor::Expression::Error);
0122 }
0123 
0124 void PythonExpression::parseWarning(const QString& warning)
0125 {
0126     if (!warning.isEmpty())
0127     {
0128         auto* result = new Cantor::TextResult(warning);
0129         result->setStdErr(true);
0130         addResult(result);
0131     }
0132 }
0133