ctkCmdLineModuleManager.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleManager.h"
  16. #include "ctkCmdLineModuleXmlValidator.h"
  17. #include "ctkCmdLineModuleObjectHierarchyReader.h"
  18. #include "ctkCmdLineModuleProcessRunner_p.h"
  19. #include "ctkCmdLineModuleReferencePrivate.h"
  20. #include <QStringList>
  21. #include <QBuffer>
  22. #include <QProcess>
  23. #include <QFuture>
  24. QString normalizeFlag(const QString& flag)
  25. {
  26. return flag.trimmed().remove(QRegExp("^-*"));
  27. }
  28. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ctkCmdLineModuleDescriptionFactory *descriptionFactory)
  29. : descriptionFactory(descriptionFactory)
  30. {
  31. }
  32. QStringList ctkCmdLineModuleManager::createCommandLineArgs(QObject *hierarchy)
  33. {
  34. ctkCmdLineModuleObjectHierarchyReader reader(hierarchy);
  35. QStringList cmdLineArgs;
  36. QHash<int, QString> indexedArgs;
  37. while(reader.readNextParameter())
  38. {
  39. if (reader.index() > -1)
  40. {
  41. indexedArgs.insert(reader.index(), reader.value());
  42. }
  43. else
  44. {
  45. QString argFlag;
  46. if (reader.longFlag().isEmpty())
  47. {
  48. argFlag = QString("-") + normalizeFlag(reader.flag());
  49. }
  50. else
  51. {
  52. argFlag = QString("--") + normalizeFlag(reader.longFlag());
  53. }
  54. QStringList args;
  55. if (reader.isMultiple())
  56. {
  57. args = reader.value().split(',', QString::SkipEmptyParts);
  58. }
  59. else
  60. {
  61. args.push_back(reader.value());
  62. }
  63. foreach(QString arg, args)
  64. {
  65. cmdLineArgs << argFlag << arg;
  66. }
  67. }
  68. }
  69. QList<int> indexes = indexedArgs.keys();
  70. qSort(indexes.begin(), indexes.end());
  71. foreach(int index, indexes)
  72. {
  73. cmdLineArgs << indexedArgs[index];
  74. }
  75. return cmdLineArgs;
  76. }
  77. ctkCmdLineModuleReference ctkCmdLineModuleManager::addModule(const QString& location)
  78. {
  79. QProcess process;
  80. process.setReadChannel(QProcess::StandardOutput);
  81. process.start(location, QStringList("--xml"));
  82. ctkCmdLineModuleReference ref;
  83. ref.d->loc = location;
  84. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  85. process.error() != QProcess::UnknownError)
  86. {
  87. qWarning() << "The executable at" << location << "could not be started:" << process.errorString();
  88. return ref;
  89. }
  90. process.waitForReadyRead();
  91. QByteArray xml = process.readAllStandardOutput();
  92. qDebug() << xml;
  93. // validate the outputted xml description
  94. QBuffer input(&xml);
  95. input.open(QIODevice::ReadOnly);
  96. ctkCmdLineModuleXmlValidator validator(&input);
  97. if (!validator.validateXMLInput())
  98. {
  99. qCritical() << validator.errorString();
  100. return ref;
  101. }
  102. ref.d->xml = xml;
  103. ref.d->objectRepresentation = descriptionFactory->createObjectRepresentationFromXML(ref.d->xml);
  104. ref.d->setGUI(descriptionFactory->createGUIFromXML(ref.d->xml));
  105. cache[location] = ref;
  106. return ref;
  107. }
  108. ctkCmdLineModuleProcessFuture ctkCmdLineModuleManager::run(const ctkCmdLineModuleReference& moduleRef)
  109. {
  110. // TODO: manage memory
  111. QStringList args = createCommandLineArgs(moduleRef.d->objectRepresentation);
  112. ctkCmdLineModuleProcessRunner* moduleProcess = new ctkCmdLineModuleProcessRunner(moduleRef.location(), args);
  113. return moduleProcess->start();
  114. }