grammalectegenerateconfigoptionjob.cpp 4.47 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
   Copyright (C) 2019 Montel Laurent <montel@kde.org>

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "grammalectegenerateconfigoptionjob.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "libgrammalecte_debug.h"
Laurent Montel's avatar
Laurent Montel committed
22 23
#include <QRegularExpression>
#include <QRegularExpressionMatch>
Laurent Montel's avatar
Laurent Montel committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

GrammalecteGenerateConfigOptionJob::GrammalecteGenerateConfigOptionJob(QObject *parent)
    : QObject(parent)
{

}

GrammalecteGenerateConfigOptionJob::~GrammalecteGenerateConfigOptionJob()
{

}
//^([a-zA-Z0-9]+):\s*(True|False)\s*(.*)$
void GrammalecteGenerateConfigOptionJob::start()
{
    if (canStart()) {
        mProcess = new QProcess(this);
        mProcess->setProgram(mPythonPath);
        mProcess->setArguments(QStringList() << mGrammarlecteCliPath << QStringLiteral("-lo"));
Laurent Montel's avatar
Laurent Montel committed
42
        connect(mProcess, QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished), this, &GrammalecteGenerateConfigOptionJob::slotFinished);
Laurent Montel's avatar
Laurent Montel committed
43 44 45
//        connect(mProcess, QOverload<QProcess::ProcessError>::of(&QProcess::error),
//                this, &GrammalecteGenerateConfigOptionJob::receivedError);
//        connect(mProcess, &QProcess::readyReadStandardError, this, &GrammalecteGenerateConfigOptionJob::receivedStdErr);
Laurent Montel's avatar
Laurent Montel committed
46
        connect(mProcess, &QProcess::readyReadStandardOutput, this, &GrammalecteGenerateConfigOptionJob::receivedStandardOutput);
Laurent Montel's avatar
Laurent Montel committed
47 48
        mProcess->start();
        if (!mProcess->waitForStarted()) {
Laurent Montel's avatar
Laurent Montel committed
49
            qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
Laurent Montel's avatar
Laurent Montel committed
50 51 52 53
            Q_EMIT error();
            deleteLater();
        }
    } else {
Laurent Montel's avatar
Laurent Montel committed
54
        qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "Impossible to start GrammalecteGenerateConfigOptionJob";
Laurent Montel's avatar
Laurent Montel committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        Q_EMIT error();
        deleteLater();
    }
}

bool GrammalecteGenerateConfigOptionJob::canStart() const
{
    if (mPythonPath.isEmpty() || mGrammarlecteCliPath.isEmpty()) {
        return false;
    }
    return true;
}

QString GrammalecteGenerateConfigOptionJob::pythonPath() const
{
    return mPythonPath;
}

void GrammalecteGenerateConfigOptionJob::setPythonPath(const QString &pythonPath)
{
    mPythonPath = pythonPath;
}

QString GrammalecteGenerateConfigOptionJob::grammarlecteCliPath() const
{
    return mGrammarlecteCliPath;
}

void GrammalecteGenerateConfigOptionJob::setGrammarlecteCliPath(const QString &grammarlecteCliPath)
{
    mGrammarlecteCliPath = grammarlecteCliPath;
}
Laurent Montel's avatar
Laurent Montel committed
87 88 89 90 91 92 93 94 95

void GrammalecteGenerateConfigOptionJob::receivedStandardOutput()
{
    mResult += QString::fromUtf8(mProcess->readAllStandardOutput());
}

void GrammalecteGenerateConfigOptionJob::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitStatus != 0 || exitCode != 0) {
Laurent Montel's avatar
Laurent Montel committed
96
        qCWarning(LIBGRAMMALECTE_PLUGIN_LOG) << "GrammalecteGenerateConfigOptionJob ERROR :!!!!!!!!!!!!!!!!!!!!" ;
Laurent Montel's avatar
Laurent Montel committed
97
    } else {
Laurent Montel's avatar
Laurent Montel committed
98
        Q_EMIT finished(parseResult());
Laurent Montel's avatar
Laurent Montel committed
99 100 101
    }
    deleteLater();
}
Laurent Montel's avatar
Laurent Montel committed
102 103


Laurent Montel's avatar
Laurent Montel committed
104
QVector<GrammalecteGenerateConfigOptionJob::Option> GrammalecteGenerateConfigOptionJob::parseResult() const
Laurent Montel's avatar
Laurent Montel committed
105
{
Laurent Montel's avatar
Laurent Montel committed
106
    QVector<GrammalecteGenerateConfigOptionJob::Option> opts;
107
    const QRegularExpression reg(QStringLiteral("^([a-zA-Z0-9]+):\\s*(True|False)\\s*(.*)$"));
Laurent Montel's avatar
Laurent Montel committed
108 109 110 111
    const QStringList lst = mResult.split(QLatin1Char('\n'));
    for (const QString &str : lst) {
        const QRegularExpressionMatch match = reg.match(str);
        if (match.hasMatch()) {
Laurent Montel's avatar
Laurent Montel committed
112
            const QString optionName = match.captured(1);
Laurent Montel's avatar
Laurent Montel committed
113
            const QString value = match.captured(2);
Laurent Montel's avatar
Laurent Montel committed
114
            const QString description = match.captured(3);
Laurent Montel's avatar
Laurent Montel committed
115
            if (!optionName.isEmpty() && !description.isEmpty() && !value.isEmpty()) {
116 117 118
                GrammalecteGenerateConfigOptionJob::Option opt;
                opt.description = description;
                opt.optionName = optionName;
Laurent Montel's avatar
Laurent Montel committed
119
                opt.defaultValue = (value == QStringLiteral("True"));
120 121
                opts.append(opt);
            }
Laurent Montel's avatar
Laurent Montel committed
122 123
        }
    }
Laurent Montel's avatar
Laurent Montel committed
124
    return opts;
Laurent Montel's avatar
Laurent Montel committed
125
}