Skip to content

Commit

Permalink
Give better warnings when scripts do not load (#1336)
Browse files Browse the repository at this point in the history
* Give better warnings when scripts do not load

Fix #1323 - now indicates Cannot load script /path/to/antechamber.py
Also enables i18n of script loader strings

Signed-off-by: Geoff Hutchison <geoff.hutchison@gmail.com>
  • Loading branch information
ghutchis authored Sep 11, 2023
1 parent 2d1307e commit 7ed1c8c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
27 changes: 15 additions & 12 deletions avogadro/qtgui/pythonscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ QByteArray PythonScript::execute(const QStringList& args,

// Write scriptStdin to the process's stdin
if (!scriptStdin.isNull()) {
if (!proc.waitForStarted(5000)) {
if (!proc.waitForStarted(5000) && m_debug) {
m_errors << tr("Error running script '%1 %2': Timed out waiting for "
"start (%3).")
.arg(m_pythonInterpreter,
Expand All @@ -110,7 +110,7 @@ QByteArray PythonScript::execute(const QStringList& args,
}

qint64 len = proc.write(scriptStdin);
if (len != static_cast<qint64>(scriptStdin.size())) {
if (len != static_cast<qint64>(scriptStdin.size()) && m_debug) {
m_errors << tr("Error running script '%1 %2': failed to write to stdin "
"(len=%3, wrote %4 bytes, QProcess error: %5).")
.arg(m_pythonInterpreter)
Expand All @@ -123,7 +123,7 @@ QByteArray PythonScript::execute(const QStringList& args,
proc.closeWriteChannel();
}

if (!proc.waitForFinished(5000)) {
if (!proc.waitForFinished(5000) && m_debug) {
m_errors << tr("Error running script '%1 %2': Timed out waiting for "
"finish (%3).")
.arg(m_pythonInterpreter, realArgs.join(QStringLiteral(" ")),
Expand All @@ -132,14 +132,17 @@ QByteArray PythonScript::execute(const QStringList& args,
}

if (proc.exitStatus() != QProcess::NormalExit || proc.exitCode() != 0) {
m_errors << tr("Error running script '%1 %2': Abnormal exit status %3 "
"(%4: %5)\n\nOutput:\n%6")
.arg(m_pythonInterpreter)
.arg(realArgs.join(QStringLiteral(" ")))
.arg(proc.exitCode())
.arg(processErrorString(proc))
.arg(proc.errorString())
.arg(QString(proc.readAll()));
if (m_debug)
m_errors << tr("Error running script '%1 %2': Abnormal exit status %3 "
"(%4: %5)\n\nOutput:\n%6")
.arg(m_pythonInterpreter)
.arg(realArgs.join(QStringLiteral(" ")))
.arg(proc.exitCode())
.arg(processErrorString(proc))
.arg(proc.errorString())
.arg(QString(proc.readAll()));
else
m_errors << tr("Warning '%1'").arg(proc.errorString());
return QByteArray();
}

Expand Down Expand Up @@ -254,4 +257,4 @@ QString PythonScript::processErrorString(const QProcess& proc) const
return result;
}

} // namespace Avogadro
} // namespace Avogadro::QtGui
14 changes: 5 additions & 9 deletions avogadro/qtgui/scriptloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@

namespace Avogadro::QtGui {

ScriptLoader::ScriptLoader(QObject* parent_)
: QObject(parent_)
{}
ScriptLoader::ScriptLoader(QObject* parent_) : QObject(parent_) {}

ScriptLoader::~ScriptLoader() {}

Expand All @@ -40,9 +38,7 @@ bool ScriptLoader::queryProgramName(const QString& scriptFilePath,
displayName = gen.displayName();
if (gen.hasErrors()) {
displayName.clear();
qWarning() << "ScriptLoader::queryProgramName: Unable to retrieve program "
"name for"
<< scriptFilePath << ";" << gen.errorList().join("\n\n");
qWarning() << tr("Cannot load script %1").arg(scriptFilePath);
return false;
}
return true;
Expand All @@ -69,7 +65,7 @@ QMap<QString, QString> ScriptLoader::scriptList(const QString& type)
// build up a list of possible files, then we check if they're real scripts
QStringList fileList;
foreach (const QString& dirStr, dirs) {
qDebug() << "Checking for " << type << " scripts in" << dirStr;
qDebug() << tr("Checking for %1 scripts in path %2").arg(type).arg(dirStr);
QDir dir(dirStr);
if (dir.exists() && dir.isReadable()) {
foreach (
Expand Down Expand Up @@ -97,7 +93,7 @@ QMap<QString, QString> ScriptLoader::scriptList(const QString& type)
if (commands.type() == QJsonValue::Array) {
// check if "command.*" exists as a file
QJsonArray list = commands.toArray();
for (auto && i : list) {
for (auto&& i : list) {
QJsonValue command = i.toObject()["command"];
QString name = command.toString();
if (name.isEmpty() || name.isNull())
Expand Down Expand Up @@ -153,4 +149,4 @@ QMap<QString, QString> ScriptLoader::scriptList(const QString& type)
return scriptList;
}

} // namespace Avogadro
} // namespace Avogadro::QtGui
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def potential():
if name:
print(name)
else:
raise RuntimeError("antechamber is unavailable")
sys.exit("antechamber is unavailable")
elif args["charges"]:
print(charges())
elif args["potential"]:
Expand Down
2 changes: 1 addition & 1 deletion avogadro/qtplugins/scriptcharges/chargeScripts/xtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def potential():
if name:
print(name)
else:
raise RuntimeError("xtb is unavailable")
sys.exit("xtb is unavailable")
elif args["charges"]:
print(charges())
elif args["potential"]:
Expand Down

0 comments on commit 7ed1c8c

Please sign in to comment.