-
Notifications
You must be signed in to change notification settings - Fork 122
/
GeneratePythonScript.cpp
139 lines (109 loc) · 5.37 KB
/
GeneratePythonScript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/GeneratePythonScript.h"
#include "MantidAPI/AlgorithmHistory.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/ScriptBuilder.h"
#include "MantidAPI/Workspace.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MantidVersion.h"
#include "MantidKernel/System.h"
#include <fstream>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using Mantid::Types::Core::DateAndTime;
namespace {
Mantid::Kernel::Logger g_log("GeneratePythonScript");
}
namespace Mantid::Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(GeneratePythonScript)
// Add alias as Export History
const std::string GeneratePythonScript::alias() const { return "ExportHistory"; }
/** Initialize the algorithm's properties.
*/
void GeneratePythonScript::init() {
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
std::vector<std::string> exts{".py"};
declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::OptionalSave, exts),
"The name of the file into which the workspace history will "
"be generated.");
declareProperty("ScriptText", std::string(""), "Saves the history of the workspace to a variable.",
Direction::Output);
declareProperty("UnrollAll", false, "Unroll all algorithms to show just their child algorithms.", Direction::Input);
declareProperty("StartTimestamp", std::string(""), "The filter start time in the format YYYY-MM-DD HH:mm:ss",
Direction::Input);
declareProperty("EndTimestamp", std::string(""), "The filter end time in the format YYYY-MM-DD HH:mm:ss",
Direction::Input);
declareProperty("AppendTimestamp", false, "Appends the time the command was run as a comment afterwards",
Direction::Input);
std::vector<std::string> saveVersions{"Specify Old", "Specify All", "Specify None"};
declareProperty("SpecifyAlgorithmVersions", "Specify Old", std::make_shared<StringListValidator>(saveVersions),
"When to specify which algorithm version was used by Mantid.");
declareProperty("IgnoreTheseAlgs", std::vector<std::string>(),
"A list of algorithms to filter out of the built script", Direction::Input);
declareProperty("IgnoreTheseAlgProperties", std::vector<std::vector<std::string>>(),
"A list of algorithm properties to filter out of the built script", Direction::Input);
declareProperty("AppendExecCount", false, "Whether execCount should be appended to the end of the built string");
}
/** Execute the algorithm.
*/
void GeneratePythonScript::exec() {
const Workspace_const_sptr ws = getProperty("InputWorkspace");
const bool unrollAll = getProperty("UnrollAll");
const std::string startTime = getProperty("StartTimestamp");
const std::string endTime = getProperty("EndTimestamp");
const std::string saveVersions = getProperty("SpecifyAlgorithmVersions");
const bool appendTimestamp = getProperty("AppendTimestamp");
const bool appendExecCount = getProperty("AppendExecCount");
const std::vector<std::string> ignoreTheseAlgs = getProperty("IgnoreTheseAlgs");
const std::vector<std::vector<std::string>> ignoreTheseAlgProperties = getProperty("IgnoreTheseAlgProperties");
// Get the algorithm histories of the workspace.
const WorkspaceHistory wsHistory = ws->getHistory();
g_log.information() << "Number of history items: " << wsHistory.size() << '\n';
auto view = wsHistory.createView();
if (unrollAll) {
view->unrollAll();
}
// Need at least a start time to do time filter
if (!startTime.empty()) {
if (endTime.empty()) {
// If no end time was given then filter up to now
view->filterBetweenExecDate(DateAndTime(startTime));
} else {
view->filterBetweenExecDate(DateAndTime(startTime), DateAndTime(endTime));
}
}
std::string versionSpecificity;
if (saveVersions == "Specify Old")
versionSpecificity = "old";
else if (saveVersions == "Specify None")
versionSpecificity = "none";
else
versionSpecificity = "all";
ScriptBuilder builder(view, versionSpecificity, appendTimestamp, ignoreTheseAlgs, ignoreTheseAlgProperties,
appendExecCount);
std::string generatedScript;
const std::string mantidVersion = std::string(MantidVersion::version());
const std::string gitSHA = std::string(MantidVersion::revisionFull());
generatedScript += "# Python script generated by Mantid\n";
generatedScript += "# Version " + mantidVersion + "\n";
generatedScript += "# SHA-1 " + gitSHA + "\n\n";
generatedScript += "from mantid.simpleapi import *\n\n";
generatedScript += builder.build();
setPropertyValue("ScriptText", generatedScript);
const std::string filename = getPropertyValue("Filename");
if (!filename.empty()) {
std::ofstream file(filename.c_str(), std::ofstream::trunc);
file << generatedScript;
file.flush();
file.close();
}
}
} // namespace Mantid::Algorithms