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#include "MantidKernel/System.h"
16
17#include <fstream>
18
19using namespace Mantid::Kernel;
20using namespace Mantid::API;
21using Mantid::Types::Core::DateAndTime;
22
23namespace {
24Mantid::Kernel::Logger g_log("GeneratePythonScript");
25}
26
27namespace Mantid::Algorithms {
28
29// Register the algorithm into the AlgorithmFactory
30DECLARE_ALGORITHM(GeneratePythonScript)
31
32// Add alias as Export History
33const std::string GeneratePythonScript::alias() const { return "ExportHistory"; }
34
38 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
39 "An input workspace.");
40
41 std::vector<std::string> exts{".py"};
42
43 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::OptionalSave, exts),
44 "The name of the file into which the workspace history will "
45 "be generated.");
46
47 declareProperty("ScriptText", std::string(""), "Saves the history of the workspace to a variable.",
49
50 declareProperty("UnrollAll", false, "Unroll all algorithms to show just their child algorithms.", Direction::Input);
51
52 declareProperty("StartTimestamp", std::string(""), "The filter start time in the format YYYY-MM-DD HH:mm:ss",
54
55 declareProperty("EndTimestamp", std::string(""), "The filter end time in the format YYYY-MM-DD HH:mm:ss",
57
58 declareProperty("AppendTimestamp", false, "Appends the time the command was run as a comment afterwards",
60
61 std::vector<std::string> saveVersions{"Specify Old", "Specify All", "Specify None"};
62 declareProperty("SpecifyAlgorithmVersions", "Specify Old", std::make_shared<StringListValidator>(saveVersions),
63 "When to specify which algorithm version was used by Mantid.");
64
65 declareProperty("IgnoreTheseAlgs", std::vector<std::string>(),
66 "A list of algorithms to filter out of the built script", Direction::Input);
67
68 declareProperty("IgnoreTheseAlgProperties", std::vector<std::vector<std::string>>(),
69 "A list of algorithm properties to filter out of the built script", Direction::Input);
70 declareProperty("AppendExecCount", false, "Whether execCount should be appended to the end of the built string");
71}
72
76 const Workspace_const_sptr ws = getProperty("InputWorkspace");
77 const bool unrollAll = getProperty("UnrollAll");
78 const std::string startTime = getProperty("StartTimestamp");
79 const std::string endTime = getProperty("EndTimestamp");
80 const std::string saveVersions = getProperty("SpecifyAlgorithmVersions");
81 const bool appendTimestamp = getProperty("AppendTimestamp");
82 const bool appendExecCount = getProperty("AppendExecCount");
83 const std::vector<std::string> ignoreTheseAlgs = getProperty("IgnoreTheseAlgs");
84 const std::vector<std::vector<std::string>> ignoreTheseAlgProperties = getProperty("IgnoreTheseAlgProperties");
85
86 // Get the algorithm histories of the workspace.
87 const WorkspaceHistory wsHistory = ws->getHistory();
88 g_log.information() << "Number of history items: " << wsHistory.size() << '\n';
89
90 auto view = wsHistory.createView();
91
92 if (unrollAll) {
93 view->unrollAll();
94 }
95
96 // Need at least a start time to do time filter
97 if (!startTime.empty()) {
98 if (endTime.empty()) {
99 // If no end time was given then filter up to now
100 view->filterBetweenExecDate(DateAndTime(startTime));
101 } else {
102 view->filterBetweenExecDate(DateAndTime(startTime), DateAndTime(endTime));
103 }
104 }
105
106 std::string versionSpecificity;
107 if (saveVersions == "Specify Old")
108 versionSpecificity = "old";
109 else if (saveVersions == "Specify None")
110 versionSpecificity = "none";
111 else
112 versionSpecificity = "all";
113
114 ScriptBuilder builder(view, versionSpecificity, appendTimestamp, ignoreTheseAlgs, ignoreTheseAlgProperties,
115 appendExecCount);
116 std::string generatedScript;
117 const std::string mantidVersion = std::string(MantidVersion::version());
118 const std::string gitSHA = std::string(MantidVersion::revisionFull());
119
120 generatedScript += "# Python script generated by Mantid\n";
121 generatedScript += "# Version " + mantidVersion + "\n";
122 generatedScript += "# SHA-1 " + gitSHA + "\n\n";
123
124 generatedScript += builder.build();
125
126 setPropertyValue("ScriptText", generatedScript);
127
128 const std::string filename = getPropertyValue("Filename");
129
130 if (!filename.empty()) {
131 std::ofstream file(filename.c_str(), std::ofstream::trunc);
132 file << generatedScript;
133 file.flush();
134 file.close();
135 }
136}
137
138} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
Definition: Algorithm.cpp:1975
@ OptionalSave
to specify a file to write to but an empty string is
Definition: FileProperty.h:50
This class build a sttring which cana be executed as a python script.
Definition: ScriptBuilder.h:31
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:52
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
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)
Definition: Workspace_fwd.h:22
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54