Mantid
Loading...
Searching...
No Matches
RecordPythonScript.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 +
9
10#include <Poco/Thread.h>
11
12#include <fstream>
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16
17namespace Mantid::Algorithms {
18
19// Register the algorithm into the AlgorithmFactory
20DECLARE_ALGORITHM(RecordPythonScript)
21
22// This is to overwrite inheriting ExportHistory alias from GeneratePythonScript
23const std::string RecordPythonScript::alias() const { return ""; }
24
25//----------------------------------------------------------------------------------------------
28 useAlgorithm("GeneratePythonScript", 1);
29}
30
31//----------------------------------------------------------------------------------------------
32
33//----------------------------------------------------------------------------------------------
37
38 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Save, ".py"),
39 "The file into which the Python script will be generated.");
40}
41
42//----------------------------------------------------------------------------------------------
47 // Keep going until you get cancelled
48 while (true) {
49 try {
50 // Exit if the user presses cancel
52 } catch (...) {
53 break;
54 }
55 progress(0.0, "Recording...");
56
57 // Sleep for 50 msec
58 Poco::Thread::sleep(50);
59 }
60
61 // save the script to a file
62 const std::string filename = getPropertyValue("Filename");
63 std::ofstream file(filename.c_str(), std::ofstream::trunc);
64
65 if (file.is_open()) {
66 file << m_generatedScript;
67 file.flush();
68 file.close();
69 } else {
70 throw Exception::FileError("Unable to create file: ", filename);
71 }
72
74}
75
80 auto props = alg->getProperties();
81
82 std::string algString;
83 for (auto &prop : props) {
84 std::string opener = "='";
85 if ((*prop).value().find('\\') != std::string::npos) {
86 opener = "=r'";
87 }
88
89 std::string paramString = (*prop).name() + opener + (*prop).value() + "'";
90
91 // Miss out parameters that are empty.
92 if (paramString.length() != 0) {
93 if (algString.length() != 0) {
94 algString += ",";
95 }
96 algString += paramString;
97 }
98 }
99
100 m_generatedScript += alg->name() + "(" + algString + ")\n";
101}
102
103} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
Observes Algorithm notifications: start,progress,finish,error.
void observeStarting()
Connect to AlgorithmManager and observe its starting notifications.
void stopObservingManager()
Disconnect from the algorithm manager.
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
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
void interruption_point()
This is called during long-running operations, and check if the algorithm has requested that it be ca...
Definition: Algorithm.cpp:1687
void useAlgorithm(const std::string &, const int version=-1)
The algorithm to use instead of this one.
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
GeneratePythonScript : TODO: DESCRIPTION.
void startingHandle(API::IAlgorithm_sptr alg) override
Handler of the start notifications.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
std::string m_generatedScript
buffer for the script
Records the filename and the description of failure.
Definition: Exception.h:98
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
STL namespace.