ctkCmdLineModule.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "ctkCmdLineModule.h"
  16. #include "ctkCmdLineModuleDescription.h"
  17. #include "ctkCmdLineModuleParameter.h"
  18. #include "ctkCmdLineModuleParameterGroup.h"
  19. #include "ctkCmdLineModuleReference.h"
  20. #include "ctkCmdLineModuleProcessTask.h"
  21. #include "ctkCmdLineModuleFuture.h"
  22. #include "ctkException.h"
  23. #include <QStringList>
  24. #include <QDebug>
  25. #include <QVariant>
  26. struct ctkCmdLineModulePrivate
  27. {
  28. ctkCmdLineModulePrivate(ctkCmdLineModule* qq,
  29. const ctkCmdLineModuleReference& moduleRef)
  30. : ModuleReference(moduleRef), q(qq)
  31. {
  32. }
  33. QString normalizeFlag(const QString& flag)
  34. {
  35. return flag.trimmed().remove(QRegExp("^-*"));
  36. }
  37. ctkCmdLineModuleReference ModuleReference;
  38. QList<QString> ParameterNames;
  39. private:
  40. ctkCmdLineModule* q;
  41. };
  42. ctkCmdLineModule::ctkCmdLineModule(const ctkCmdLineModuleReference& moduleRef)
  43. : d(new ctkCmdLineModulePrivate(this, moduleRef))
  44. {
  45. }
  46. ctkCmdLineModule::~ctkCmdLineModule()
  47. {
  48. }
  49. QList<QString> ctkCmdLineModule::parameterNames() const
  50. {
  51. if (!d->ParameterNames.isEmpty()) return d->ParameterNames;
  52. foreach (ctkCmdLineModuleParameterGroup paramGroup,
  53. moduleReference().description().parameterGroups())
  54. {
  55. foreach (ctkCmdLineModuleParameter param, paramGroup.parameters())
  56. {
  57. d->ParameterNames.push_back(param.name());
  58. }
  59. }
  60. return d->ParameterNames;
  61. }
  62. ctkCmdLineModuleReference ctkCmdLineModule::moduleReference() const
  63. {
  64. return d->ModuleReference;
  65. }
  66. QString ctkCmdLineModule::location() const
  67. {
  68. return d->ModuleReference.location();
  69. }
  70. QStringList ctkCmdLineModule::commandLineArguments() const
  71. {
  72. QStringList cmdLineArgs;
  73. QHash<int, QString> indexedArgs;
  74. ctkCmdLineModuleDescription description = moduleReference().description();
  75. QHash<QString,QVariant> currentValues = values();
  76. QHashIterator<QString,QVariant> valuesIter(currentValues);
  77. while(valuesIter.hasNext())
  78. {
  79. valuesIter.next();
  80. ctkCmdLineModuleParameter parameter = description.parameter(valuesIter.key());
  81. if (parameter.index() > -1)
  82. {
  83. indexedArgs.insert(parameter.index(), valuesIter.value().toString());
  84. }
  85. else
  86. {
  87. QString argFlag;
  88. if (parameter.longFlag().isEmpty())
  89. {
  90. argFlag = QString("-") + d->normalizeFlag(parameter.flag());
  91. }
  92. else
  93. {
  94. argFlag = QString("--") + d->normalizeFlag(parameter.longFlag());
  95. }
  96. if (parameter.tag() == "boolean")
  97. {
  98. if (valuesIter.value().toBool())
  99. {
  100. cmdLineArgs << argFlag;
  101. }
  102. }
  103. else
  104. {
  105. QStringList args;
  106. if (parameter.multiple())
  107. {
  108. args = valuesIter.value().toString().split(',', QString::SkipEmptyParts);
  109. }
  110. else
  111. {
  112. QString arg = valuesIter.value().toString();
  113. if (arg.isEmpty())
  114. {
  115. arg = parameter.defaultValue();
  116. }
  117. if (!arg.isEmpty())
  118. {
  119. args.push_back(valuesIter.value().toString());
  120. }
  121. }
  122. if (args.length() > 0) // don't write the argFlag if there was no argument, and no default.
  123. {
  124. foreach(QString arg, args)
  125. {
  126. cmdLineArgs << argFlag << arg;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. QList<int> indexes = indexedArgs.keys();
  133. qSort(indexes.begin(), indexes.end());
  134. foreach(int index, indexes)
  135. {
  136. cmdLineArgs << indexedArgs[index];
  137. }
  138. return cmdLineArgs;
  139. }
  140. ctkCmdLineModuleFuture ctkCmdLineModule::run() const
  141. {
  142. QStringList args = commandLineArguments();
  143. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the
  144. // thread pool.
  145. ctkCmdLineModuleProcessTask* moduleProcess =
  146. new ctkCmdLineModuleProcessTask(d->ModuleReference.location(), args);
  147. return moduleProcess->start();
  148. }
  149. QHash<QString, QVariant> ctkCmdLineModule::values() const
  150. {
  151. QHash<QString,QVariant> result;
  152. foreach(QString parameterName, parameterNames())
  153. {
  154. result.insert(parameterName, value(parameterName));
  155. }
  156. return result;
  157. }
  158. void ctkCmdLineModule::setValues(const QHash<QString, QVariant> &values)
  159. {
  160. QHashIterator<QString,QVariant> iter(values);
  161. while(iter.hasNext())
  162. {
  163. iter.next();
  164. setValue(iter.key(), iter.value());
  165. }
  166. }