| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "fileformatscript.h" |
| 7 |
|
| 8 |
#include <avogadro/core/molecule.h> |
| 9 |
#include <avogadro/qtgui/pythonscript.h> |
| 10 |
|
| 11 |
#include <avogadro/io/cjsonformat.h> |
| 12 |
#include <avogadro/io/cmlformat.h> |
| 13 |
#include <avogadro/io/mdlformat.h> |
| 14 |
#include <avogadro/io/pdbformat.h> |
| 15 |
#include <avogadro/io/sdfformat.h> |
| 16 |
#include <avogadro/io/xyzformat.h> |
| 17 |
|
| 18 |
#include <QtCore/QDebug> |
| 19 |
#include <QtCore/QJsonArray> |
| 20 |
#include <QtCore/QJsonDocument> |
| 21 |
#include <QtCore/QJsonObject> |
| 22 |
#include <QtCore/QScopedPointer> |
| 23 |
|
| 24 |
namespace Avogadro::QtPlugins { |
| 25 |
|
| 26 |
FileFormatScript::FileFormatScript(const QString& scriptFileName_) |
| 27 |
: m_interpreter(new QtGui::PythonScript(scriptFileName_)), m_valid(false), |
| 28 |
m_bondOnRead(false), m_inputModeFile(false), m_outputModeFile(false), |
| 29 |
m_inputFormat(NotUsed), m_outputFormat(NotUsed) |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
FileFormatScript::~FileFormatScript() |
| 34 |
{ |
| 35 |
delete m_interpreter; |
| 36 |
} |
| 37 |
|
| 38 |
void FileFormatScript::setPackageInfo(const QString& packageDir, |
| 39 |
const QString& command, |
| 40 |
const QString& identifier) |
| 41 |
{ |
| 42 |
m_interpreter->setPackageInfo(packageDir, command, identifier); |
| 43 |
} |
| 44 |
|
| 45 |
void FileFormatScript::readMetaData(const QVariantMap& metadata) |
| 46 |
{ |
| 47 |
resetMetaData(); |
| 48 |
|
| 49 |
m_identifier = metadata.value("identifier").toString().toStdString(); |
| 50 |
if (!m_identifier.empty()) |
| 51 |
m_identifier = "User Script: " + m_identifier; |
| 52 |
|
| 53 |
m_name = metadata.value("format-name").toString().toStdString(); |
| 54 |
m_description = metadata.value("description").toString().toStdString(); |
| 55 |
|
| 56 |
QVariantMap support = metadata.value("support").toMap(); |
| 57 |
if (support.value("read", false).toBool()) |
| 58 |
m_operations |= Io::FileFormat::Read; |
| 59 |
if (support.value("write", false).toBool()) |
| 60 |
m_operations |= Io::FileFormat::Write; |
| 61 |
// Parse input-mode / output-mode: "string" (default) or "file" |
| 62 |
m_inputModeFile = metadata.value("input-mode") |
| 63 |
.toString() |
| 64 |
.compare(QLatin1String("file"), Qt::CaseInsensitive) == 0; |
| 65 |
m_outputModeFile = |
| 66 |
metadata.value("output-mode") |
| 67 |
.toString() |
| 68 |
.compare(QLatin1String("file"), Qt::CaseInsensitive) == 0; |
| 69 |
|
| 70 |
if (m_inputModeFile || m_outputModeFile) |
| 71 |
m_operations |= Io::FileFormat::File; |
| 72 |
else |
| 73 |
m_operations |= |
| 74 |
Io::FileFormat::File | Io::FileFormat::Stream | Io::FileFormat::String; |
| 75 |
|
| 76 |
if (m_operations & Io::FileFormat::Write) { |
| 77 |
QString inputFmt = metadata.value("input-format").toString(); |
| 78 |
m_inputFormat = stringToFormat(inputFmt.toStdString()); |
| 79 |
} |
| 80 |
|
| 81 |
if (m_operations & Io::FileFormat::Read) { |
| 82 |
QString outputFmt = metadata.value("output-format").toString(); |
| 83 |
m_outputFormat = stringToFormat(outputFmt.toStdString()); |
| 84 |
} |
| 85 |
|
| 86 |
// Parse file extensions array |
| 87 |
QVariantList extList = metadata.value("file-extensions").toList(); |
| 88 |
for (const auto& ext : extList) |
| 89 |
m_fileExtensions.push_back(ext.toString().toStdString()); |
| 90 |
|
| 91 |
// Parse mime types array |
| 92 |
QVariantList mimeList = metadata.value("mime-types").toList(); |
| 93 |
for (const auto& mime : mimeList) |
| 94 |
m_mimeTypes.push_back(mime.toString().toStdString()); |
| 95 |
|
| 96 |
// Check for bond-on-read |
| 97 |
if (metadata.contains("bond")) |
| 98 |
m_bondOnRead = metadata.value("bond").toBool(); |
| 99 |
|
| 100 |
m_valid = |
| 101 |
!m_identifier.empty() && !m_name.empty() && |
| 102 |
(!(m_operations & Io::FileFormat::Write) || m_inputFormat != NotUsed) && |
| 103 |
(!(m_operations & Io::FileFormat::Read) || m_outputFormat != NotUsed); |
| 104 |
} |
| 105 |
|
| 106 |
QString FileFormatScript::scriptFilePath() const |
| 107 |
{ |
| 108 |
return m_interpreter->scriptFilePath(); |
| 109 |
} |
| 110 |
|
| 111 |
Io::FileFormat* FileFormatScript::newInstance() const |
| 112 |
{ |
| 113 |
auto* copy = new FileFormatScript(); |
| 114 |
copy->m_interpreter->setPackageInfo(m_interpreter->packageDir(), |
| 115 |
m_interpreter->packageCommand(), |
| 116 |
m_interpreter->packageIdentifier()); |
| 117 |
copy->copyMetaDataFrom(*this); |
| 118 |
return copy; |
| 119 |
} |
| 120 |
|
| 121 |
void FileFormatScript::copyMetaDataFrom(const FileFormatScript& other) |
| 122 |
{ |
| 123 |
m_identifier = other.m_identifier; |
| 124 |
m_name = other.m_name; |
| 125 |
m_description = other.m_description; |
| 126 |
m_specificationUrl = other.m_specificationUrl; |
| 127 |
m_operations = other.m_operations; |
| 128 |
m_inputFormat = other.m_inputFormat; |
| 129 |
m_outputFormat = other.m_outputFormat; |
| 130 |
m_fileExtensions = other.m_fileExtensions; |
| 131 |
m_mimeTypes = other.m_mimeTypes; |
| 132 |
m_bondOnRead = other.m_bondOnRead; |
| 133 |
m_inputModeFile = other.m_inputModeFile; |
| 134 |
m_outputModeFile = other.m_outputModeFile; |
| 135 |
m_valid = other.m_valid; |
| 136 |
} |
| 137 |
|
| 138 |
bool FileFormatScript::read(std::istream& in, Core::Molecule& molecule) |
| 139 |
{ |
| 140 |
if (m_inputModeFile) { |
| 141 |
appendError("This format requires a file path and cannot be read from a " |
| 142 |
"stream."); |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
// Create intermediate format reader |
| 147 |
QScopedPointer<FileFormat> format(createFileFormat(m_outputFormat)); |
| 148 |
|
| 149 |
if (format.isNull()) { |
| 150 |
appendError("Invalid intermediate format enum value."); |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
// Copy input into memory |
| 155 |
/// @todo would be nice to pass the stream to the interpreter directly... |
| 156 |
// Get length first: |
| 157 |
in.seekg(0, std::istream::end); |
| 158 |
std::istream::pos_type size = in.tellg(); |
| 159 |
|
| 160 |
// Construct byte array |
| 161 |
QByteArray buffer; |
| 162 |
buffer.resize(static_cast<int>(size)); |
| 163 |
in.seekg(0, std::istream::beg); |
| 164 |
in.read(buffer.data(), size); |
| 165 |
|
| 166 |
// Call the script to convert the file |
| 167 |
QByteArray result = m_interpreter->execute(QStringList() << "--read", buffer); |
| 168 |
|
| 169 |
if (m_interpreter->hasErrors()) { |
| 170 |
foreach (const QString& err, m_interpreter->errorList()) |
| 171 |
appendError(err.toStdString()); |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
if (!format->readString(std::string(result.constData(), result.size()), |
| 176 |
molecule)) { |
| 177 |
appendError(format->error(), false); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
if (m_bondOnRead) { |
| 182 |
molecule.perceiveBondsSimple(); |
| 183 |
molecule.perceiveBondOrders(); |
| 184 |
} |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
bool FileFormatScript::write(std::ostream& out, const Core::Molecule& molecule) |
| 190 |
{ |
| 191 |
if (m_outputModeFile) { |
| 192 |
appendError("This format requires a file path and cannot be written to a " |
| 193 |
"stream."); |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
// Create the intermediate format writer |
| 198 |
std::string intermediate; |
| 199 |
QScopedPointer<FileFormat> format(createFileFormat(m_inputFormat)); |
| 200 |
|
| 201 |
if (format.isNull()) { |
| 202 |
appendError("Invalid intermediate format enum value."); |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
if (!format->writeString(intermediate, molecule)) { |
| 207 |
appendError(format->error(), false); |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
// Call the script to convert the file |
| 212 |
QByteArray result = m_interpreter->execute( |
| 213 |
QStringList() << "--write", |
| 214 |
QByteArray::fromRawData(intermediate.c_str(), intermediate.size())); |
| 215 |
|
| 216 |
if (m_interpreter->hasErrors()) { |
| 217 |
foreach (const QString& err, m_interpreter->errorList()) |
| 218 |
appendError(err.toStdString()); |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
out.write(result.constData(), result.size()); |
| 223 |
|
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
FileFormatScript::Format FileFormatScript::stringToFormat( |
| 228 |
const std::string& str) |
| 229 |
{ |
| 230 |
if (str == "cjson") |
| 231 |
return Cjson; |
| 232 |
else if (str == "cml") |
| 233 |
return Cml; |
| 234 |
else if (str == "mdl" || str == "mol") |
| 235 |
return Mdl; |
| 236 |
else if (str == "pdb") |
| 237 |
return Pdb; |
| 238 |
else if (str == "sdf") |
| 239 |
return Sdf; |
| 240 |
else if (str == "xyz") |
| 241 |
return Xyz; |
| 242 |
return NotUsed; |
| 243 |
} |
| 244 |
|
| 245 |
std::string FileFormatScript::formatToString(Format fmt) |
| 246 |
{ |
| 247 |
switch (fmt) { |
| 248 |
case Cjson: |
| 249 |
return "cjson"; |
| 250 |
case Cml: |
| 251 |
return "cml"; |
| 252 |
case Mdl: |
| 253 |
return "mdl"; |
| 254 |
case Pdb: |
| 255 |
return "pdb"; |
| 256 |
case Sdf: |
| 257 |
return "sdf"; |
| 258 |
case Xyz: |
| 259 |
return "xyz"; |
| 260 |
default: |
| 261 |
return ""; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
bool FileFormatScript::readFile(const std::string& fileName, |
| 266 |
Core::Molecule& molecule) |
| 267 |
{ |
| 268 |
if (!m_inputModeFile) |
| 269 |
return FileFormat::readFile(fileName, molecule); |
| 270 |
|
| 271 |
QScopedPointer<FileFormat> format(createFileFormat(m_outputFormat)); |
| 272 |
if (format.isNull()) { |
| 273 |
appendError("Invalid intermediate format enum value."); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
QJsonObject payload; |
| 278 |
payload["operation"] = QLatin1String("read"); |
| 279 |
payload["filename"] = QString::fromStdString(fileName); |
| 280 |
QByteArray input = QJsonDocument(payload).toJson(QJsonDocument::Compact); |
| 281 |
|
| 282 |
QByteArray result = m_interpreter->execute(QStringList() << "--read", input); |
| 283 |
|
| 284 |
if (m_interpreter->hasErrors()) { |
| 285 |
foreach (const QString& err, m_interpreter->errorList()) |
| 286 |
appendError(err.toStdString()); |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
if (!format->readString(std::string(result.constData(), result.size()), |
| 291 |
molecule)) { |
| 292 |
appendError(format->error(), false); |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
if (m_bondOnRead) { |
| 297 |
molecule.perceiveBondsSimple(); |
| 298 |
molecule.perceiveBondOrders(); |
| 299 |
} |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
bool FileFormatScript::writeFile(const std::string& fileName, |
| 305 |
const Core::Molecule& molecule) |
| 306 |
{ |
| 307 |
if (!m_outputModeFile) |
| 308 |
return FileFormat::writeFile(fileName, molecule); |
| 309 |
|
| 310 |
QScopedPointer<FileFormat> format(createFileFormat(m_inputFormat)); |
| 311 |
if (format.isNull()) { |
| 312 |
appendError("Invalid intermediate format enum value."); |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
std::string intermediate; |
| 317 |
if (!format->writeString(intermediate, molecule)) { |
| 318 |
appendError(format->error(), false); |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
QJsonObject payload; |
| 323 |
payload["operation"] = QLatin1String("write"); |
| 324 |
payload["filename"] = QString::fromStdString(fileName); |
| 325 |
payload[QString::fromStdString(formatToString(m_inputFormat))] = |
| 326 |
QString::fromStdString(intermediate); |
| 327 |
QByteArray input = QJsonDocument(payload).toJson(QJsonDocument::Compact); |
| 328 |
|
| 329 |
m_interpreter->execute(QStringList() << "--write", input); |
| 330 |
|
| 331 |
if (m_interpreter->hasErrors()) { |
| 332 |
foreach (const QString& err, m_interpreter->errorList()) |
| 333 |
appendError(err.toStdString()); |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
Io::FileFormat* FileFormatScript::createFileFormat(FileFormatScript::Format fmt) |
| 341 |
{ |
| 342 |
switch (fmt) { |
| 343 |
case Cjson: |
| 344 |
return new Io::CjsonFormat; |
| 345 |
case Cml: |
| 346 |
return new Io::CmlFormat; |
| 347 |
case Mdl: |
| 348 |
return new Io::MdlFormat; |
| 349 |
case Pdb: |
| 350 |
return new Io::PdbFormat; |
| 351 |
case Sdf: |
| 352 |
return new Io::SdfFormat; |
| 353 |
case Xyz: |
| 354 |
return new Io::XyzFormat; |
| 355 |
default: |
| 356 |
case NotUsed: |
| 357 |
return nullptr; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void FileFormatScript::resetMetaData() |
| 362 |
{ |
| 363 |
m_operations = Io::FileFormat::None; |
| 364 |
m_valid = false; |
| 365 |
m_bondOnRead = false; |
| 366 |
m_inputModeFile = false; |
| 367 |
m_outputModeFile = false; |
| 368 |
m_inputFormat = NotUsed; |
| 369 |
m_identifier.clear(); |
| 370 |
m_name.clear(); |
| 371 |
m_description.clear(); |
| 372 |
m_specificationUrl.clear(); |
| 373 |
m_fileExtensions.clear(); |
| 374 |
m_mimeTypes.clear(); |
| 375 |
} |
| 376 |
|
| 377 |
} // namespace Avogadro::QtPlugins |