Salome HOME
Merge branch 'omu/multijob'
[tools/ydefx.git] / src / cpp / PyStudyJob.cxx
1 // Copyright (C) 2019  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "PyStudyJob.hxx"
20 #include <py2cpp/py2cpp.hxx>
21
22 namespace ydefx
23 {
24 PyStudyJob::PyStudyJob()
25 : _pyStudy()
26 , _lastError()
27 , _waitDelay(10)
28 {
29   py2cpp::PyFunction objConstructor;
30   objConstructor.loadExp("pydefx", "PyStudy");
31   _pyStudy = objConstructor();
32 }
33
34 PyStudyJob::PyStudyJob(const std::string& pymodule_name, const std::string& pyclass_name)
35 : _pyStudy()
36 , _lastError()
37 , _waitDelay(10)
38 {
39   py2cpp::PyFunction objConstructor;
40   objConstructor.loadExp(pymodule_name, pyclass_name);
41   _pyStudy = objConstructor();
42 }
43
44 PyStudyJob::PyStudyJob(py2cpp::PyPtr& pyStudyObj)
45 : _pyStudy(pyStudyObj)
46 , _lastError()
47 , _waitDelay(10)
48 {
49 }
50
51 PyStudyJob::~PyStudyJob()
52 {
53 }
54
55 std::string PyStudyJob::state()
56 {
57   std::string result;
58   _lastError = "";
59   try
60   {
61     py2cpp::PyFunction pyFn;
62     pyFn.loadExp(_pyStudy, "getJobState");
63     py2cpp::pyResult(result) = pyFn();
64   }
65   catch(std::exception& e)
66   {
67     _lastError = "An error occured while retrieving job's state.\n";
68     _lastError += e.what();
69   }
70   return result;
71 }
72
73 double PyStudyJob::progress()
74 {
75   double result;
76   py2cpp::PyFunction pyFn;
77   _lastError = "";
78   try
79   {
80     pyFn.loadExp(_pyStudy, "getProgress");
81     py2cpp::pyResult(result) = pyFn();
82   }
83   catch(std::exception& e)
84   {
85     _lastError = "An error occured while retrieving job's progress.\n";
86     _lastError += e.what();
87   }
88   return result;
89 }
90
91 std::string PyStudyJob::dump()
92 {
93   std::string result;
94   _lastError = "";
95   try
96   {
97     py2cpp::PyFunction pyFn;
98     pyFn.loadExp(_pyStudy, "dump");
99     py2cpp::pyResult(result) = pyFn();
100   }
101   catch(std::exception& e)
102   {
103     _lastError = "An error occured while dumping the job.\n";
104     _lastError += e.what();
105   }
106   return result;
107 }
108
109 bool PyStudyJob::launch()
110 {
111   _lastError = "";
112   try
113   {
114     py2cpp::PyFunction pyFn;
115     pyFn.loadExp(_pyStudy, "launch");
116     pyFn();
117   }
118   catch(std::exception& e)
119   {
120     _lastError = "An error occured while launching the job.\n";
121     _lastError += e.what();
122   }
123   return _lastError.empty();
124 }
125
126 const std::string& PyStudyJob::lastError()
127 {
128   return _lastError;
129 }
130
131 bool PyStudyJob::wait()
132 {
133   _lastError = "";
134   try
135   {
136     py2cpp::PyFunction pyFn;
137     pyFn.loadExp(_pyStudy, "wait");
138     pyFn(_waitDelay);
139   }
140   catch(std::exception& e)
141   {
142     _lastError = "An error occured while waiting the end of the job.\n";
143     _lastError += e.what();
144   }
145   return _lastError.empty();
146 }
147
148 void PyStudyJob::configureWaitDelay(int seconds)
149 {
150   _waitDelay = seconds;
151 }
152
153 }