Mantid
Loading...
Searching...
No Matches
GeneratePythonScript.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
12#include "MantidAPI/Workspace.h"
15
16#include <fstream>
17
18using namespace Mantid::Kernel;
19using namespace Mantid::API;
20using Mantid::Types::Core::DateAndTime;
21
22namespace {
23Mantid::Kernel::Logger g_log("GeneratePythonScript");
24}
25
26namespace Mantid::Algorithms {
27
28// Register the algorithm into the AlgorithmFactory
29DECLARE_ALGORITHM(GeneratePythonScript)
30
31// Add alias as Export History
32const std::string GeneratePythonScript::alias() const { return "ExportHistory"; }
33
37 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
38 "An input workspace.");
39
40 std::vector<std::string> exts{".py"};
41
42 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::OptionalSave, exts),
43 "The name of the file into which the workspace history will "
44 "be generated.");
45
46 declareProperty("ScriptText", std::string(""), "Saves the history of the workspace to a variable.",
48
49 declareProperty("UnrollAll", false, "Unroll all algorithms to show just their child algorithms.", Direction::Input);
50
51 declareProperty("StartTimestamp", std::string(""), "The filter start time in the format YYYY-MM-DD HH:mm:ss",
53
54 declareProperty("EndTimestamp", std::string(""), "The filter end time in the format YYYY-MM-DD HH:mm:ss",
56
57 declareProperty("AppendTimestamp", false, "Appends the time the command was run as a comment afterwards",
59
60 std::vector<std::string> saveVersions{"Specify Old", "Specify All", "Specify None"};
61 declareProperty("SpecifyAlgorithmVersions", "Specify Old", std::make_shared<StringListValidator>(saveVersions),
62 "When to specify which algorithm version was used by Mantid.");
63
64 declareProperty("IgnoreTheseAlgs", std::vector<std::string>(),
65 "A list of algorithms to filter out of the built script", Direction::Input);
66
67 declareProperty("IgnoreTheseAlgProperties", std::vector<std::vector<std::string>>(),
68 "A list of algorithm properties to filter out of the built script", Direction::Input);
69 declareProperty("AppendExecCount", false, "Whether execCount should be appended to the end of the built string");
70
71 declareProperty("ExcludeHeader", false,
72 "Whether the header comments should be excluded from the beginning of the built string");
73}
74
78 const Workspace_const_sptr ws = getProperty("InputWorkspace");
79 const bool unrollAll = getProperty("UnrollAll");
80 const std::string startTime = getProperty("StartTimestamp");
81 const std::string endTime = getProperty("EndTimestamp");
82 const std::string saveVersions = getProperty("SpecifyAlgorithmVersions");
83 const bool appendTimestamp = getProperty("AppendTimestamp");
84 const bool appendExecCount = getProperty("AppendExecCount");
85 const bool excludeHeader = getProperty("ExcludeHeader");
86 const std::vector<std::string> ignoreTheseAlgs = getProperty("IgnoreTheseAlgs");
87 const std::vector<std::vector<std::string>> ignoreTheseAlgProperties = getProperty("IgnoreTheseAlgProperties");
88
89 // Get the algorithm histories of the workspace.
90 const WorkspaceHistory wsHistory = ws->getHistory();
91 g_log.information() << "Number of history items: " << wsHistory.size() << '\n';
92
93 auto view = wsHistory.createView();
94
95 if (unrollAll) {
96 view->unrollAll();
97 }
98
99 // Need at least a start time to do time filter
100 if (!startTime.empty()) {
101 if (endTime.empty()) {
102 // If no end time was given then filter up to now
103 view->filterBetweenExecDate(DateAndTime(startTime));
104 } else {
105 view->filterBetweenExecDate(DateAndTime(startTime), DateAndTime(endTime));
106 }
107 }
108
109 std::string versionSpecificity;
110 if (saveVersions == "Specify Old")
111 versionSpecificity = "old";
112 else if (saveVersions == "Specify None")
113 versionSpecificity = "none";
114 else
115 versionSpecificity = "all";
116
117 ScriptBuilder builder(view, versionSpecificity, appendTimestamp, ignoreTheseAlgs, ignoreTheseAlgProperties,
118 appendExecCount);
119 std::string generatedScript;
120
121 if (!excludeHeader) {
122
123 const std::string mantidVersion = std::string(MantidVersion::version());
124 const std::string gitSHA = std::string(MantidVersion::revisionFull());
125
126 generatedScript += "# Python script generated by Mantid\n";
127 generatedScript += "# Version " + mantidVersion + "\n";
128 generatedScript += "# SHA-1 " + gitSHA + "\n\n";
129 }
130
131 generatedScript += builder.build();
132
133 setPropertyValue("ScriptText", generatedScript);
134
135 const std::string filename = getPropertyValue("Filename");
136
137 if (!filename.empty()) {
138 std::ofstream file(filename.c_str(), std::ofstream::trunc);
139 file << generatedScript;
140 file.flush();
141 file.close();
142 }
143}
144
145} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
@ OptionalSave
to specify a file to write to but an empty string is
This class build a sttring which cana be executed as a python script.
const std::string build()
build a python script from the history view
This class stores information about the Workspace History used by algorithms on a workspace and the e...
std::shared_ptr< HistoryView > createView() const
Create a flat view of the workspaces algorithm history.
size_t size() const
How many entries are there.
A property class for workspaces.
GeneratePythonScript : TODO: DESCRIPTION.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
static const char * version()
The full version number.
static const char * revisionFull()
The full SHA-1 of the last commit.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54