| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <cassert> |
| 25 |
#include <QFile> |
| 26 |
#include <QTextStream> |
| 27 |
|
| 28 |
|
| 29 |
#include <App/Application.h> |
| 30 |
#include <Base/Console.h> |
| 31 |
#include <Base/Exception.h> |
| 32 |
#include <Base/Interpreter.h> |
| 33 |
|
| 34 |
#include "Macro.h" |
| 35 |
#include "MainWindow.h" |
| 36 |
#include "PythonConsole.h" |
| 37 |
#include "PythonConsolePy.h" |
| 38 |
#include "PythonDebugger.h" |
| 39 |
|
| 40 |
|
| 41 |
using namespace Gui; |
| 42 |
|
| 43 |
MacroFile::MacroFile() = default; |
| 44 |
|
| 45 |
void MacroFile::open(const char* sName) |
| 46 |
{ |
| 47 |
// check |
| 48 |
#if _DEBUG |
| 49 |
Q_ASSERT(!this->openMacro); |
| 50 |
#endif |
| 51 |
|
| 52 |
// Convert from Utf-8 |
| 53 |
this->macroName = QString::fromUtf8(sName); |
| 54 |
if (!this->macroName.endsWith(QLatin1String(".FCMacro"))) { |
| 55 |
this->macroName += QLatin1String(".FCMacro"); |
| 56 |
} |
| 57 |
|
| 58 |
this->macroInProgress.clear(); |
| 59 |
this->openMacro = true; |
| 60 |
} |
| 61 |
|
| 62 |
void MacroFile::append(const QString& line) |
| 63 |
{ |
| 64 |
this->macroInProgress.append(line); |
| 65 |
} |
| 66 |
|
| 67 |
void MacroFile::append(const QStringList& lines) |
| 68 |
{ |
| 69 |
this->macroInProgress.append(lines); |
| 70 |
} |
| 71 |
|
| 72 |
bool MacroFile::commit() |
| 73 |
{ |
| 74 |
QFile file(this->macroName); |
| 75 |
if (!file.open(QFile::WriteOnly)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
// sort import lines and avoid duplicates |
| 80 |
QTextStream str(&file); |
| 81 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 82 |
str.setCodec("UTF-8"); |
| 83 |
#endif |
| 84 |
QStringList importCommand; |
| 85 |
importCommand << QStringLiteral("import FreeCAD"); |
| 86 |
QStringList body; |
| 87 |
|
| 88 |
for (const auto& it : std::as_const(this->macroInProgress)) { |
| 89 |
if (it.startsWith(QLatin1String("import ")) || it.startsWith(QLatin1String("#import "))) { |
| 90 |
if (importCommand.indexOf(it) == -1) { |
| 91 |
importCommand.push_back(it); |
| 92 |
} |
| 93 |
} |
| 94 |
else { |
| 95 |
body.push_back(it); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
QString header; |
| 100 |
header += QStringLiteral("# Macro Begin: "); |
| 101 |
header += this->macroName; |
| 102 |
header += QStringLiteral(" +++++++++++++++++++++++++++++++++++++++++++++++++\n"); |
| 103 |
|
| 104 |
QString footer = QStringLiteral("# Macro End: "); |
| 105 |
footer += this->macroName; |
| 106 |
footer += QStringLiteral(" +++++++++++++++++++++++++++++++++++++++++++++++++\n"); |
| 107 |
|
| 108 |
// write the data to the text file |
| 109 |
str << header; |
| 110 |
for (const auto& it : std::as_const(importCommand)) { |
| 111 |
str << it << QLatin1Char('\n'); |
| 112 |
} |
| 113 |
str << QLatin1Char('\n'); |
| 114 |
for (const auto& it : body) { |
| 115 |
str << it << QLatin1Char('\n'); |
| 116 |
} |
| 117 |
str << footer; |
| 118 |
|
| 119 |
this->macroInProgress.clear(); |
| 120 |
this->macroName.clear(); |
| 121 |
this->openMacro = false; |
| 122 |
file.close(); |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
void MacroFile::cancel() |
| 127 |
{ |
| 128 |
this->macroInProgress.clear(); |
| 129 |
this->macroName.clear(); |
| 130 |
this->openMacro = false; |
| 131 |
} |
| 132 |
|
| 133 |
// ---------------------------------------------------------------------------- |
| 134 |
|
| 135 |
MacroOutputBuffer::MacroOutputBuffer() = default; |
| 136 |
|
| 137 |
void MacroOutputBuffer::addPendingLine(int type, const char* line) |
| 138 |
{ |
| 139 |
if (!line) { |
| 140 |
pendingLine.clear(); |
| 141 |
} |
| 142 |
else { |
| 143 |
pendingLine.emplace_back(type, line); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
bool MacroOutputBuffer::addPendingLineIfComment(int type, const char* line) |
| 148 |
{ |
| 149 |
if (MacroOutputOption::isComment(type)) { |
| 150 |
pendingLine.emplace_back(type, line); |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
void MacroOutputBuffer::incrementIfNoComment(int type) |
| 158 |
{ |
| 159 |
if (!MacroOutputOption::isComment(type)) { |
| 160 |
++totalLines; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// ---------------------------------------------------------------------------- |
| 165 |
|
| 166 |
MacroOutputOption::MacroOutputOption() = default; |
| 167 |
|
| 168 |
std::tuple<bool, bool> MacroOutputOption::values(int type) const |
| 169 |
{ |
| 170 |
bool comment = isComment(type); |
| 171 |
bool record = true; |
| 172 |
|
| 173 |
if (isGuiCommand(type)) { |
| 174 |
if (recordGui && guiAsComment) { |
| 175 |
comment = true; |
| 176 |
} |
| 177 |
else if (!recordGui) { |
| 178 |
comment = true; |
| 179 |
record = false; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return std::make_tuple(comment, record); |
| 184 |
} |
| 185 |
|
| 186 |
bool MacroOutputOption::isComment(int type) |
| 187 |
{ |
| 188 |
return type == MacroManager::Cmt; |
| 189 |
} |
| 190 |
|
| 191 |
bool MacroOutputOption::isGuiCommand(int type) |
| 192 |
{ |
| 193 |
return type == MacroManager::Gui; |
| 194 |
} |
| 195 |
|
| 196 |
bool MacroOutputOption::isAppCommand(int type) |
| 197 |
{ |
| 198 |
return type == MacroManager::App; |
| 199 |
} |
| 200 |
|
| 201 |
// ---------------------------------------------------------------------------- |
| 202 |
|
| 203 |
MacroManager::MacroManager() |
| 204 |
: pyDebugger(new PythonDebugger()) |
| 205 |
{ |
| 206 |
// Attach to the Parametergroup regarding macros |
| 207 |
this->params = App::GetApplication().GetParameterGroupByPath( |
| 208 |
"User parameter:BaseApp/Preferences/Macro" |
| 209 |
); |
| 210 |
this->params->Attach(this); |
| 211 |
this->params->NotifyAll(); |
| 212 |
} |
| 213 |
|
| 214 |
MacroManager::~MacroManager() |
| 215 |
{ |
| 216 |
delete pyDebugger; |
| 217 |
this->params->Detach(this); |
| 218 |
} |
| 219 |
|
| 220 |
std::stack<std::function<void(MacroManager::LineType, const char*)>> MacroManager::redirectFuncs; |
| 221 |
|
| 222 |
void MacroManager::OnChange(Base::Subject<const char*>& rCaller, const char* sReason) |
| 223 |
{ |
| 224 |
(void)rCaller; |
| 225 |
(void)sReason; |
| 226 |
option.recordGui = this->params->GetBool("RecordGui", true); |
| 227 |
option.guiAsComment = this->params->GetBool("GuiAsComment", true); |
| 228 |
option.scriptToPyConsole = this->params->GetBool("ScriptToPyConsole", true); |
| 229 |
this->localEnv = this->params->GetBool("LocalEnvironment", true); |
| 230 |
} |
| 231 |
|
| 232 |
void MacroManager::open(MacroType eType, const char* sName) |
| 233 |
{ |
| 234 |
// check |
| 235 |
#if _DEBUG |
| 236 |
assert(eType == File); |
| 237 |
#else |
| 238 |
Q_UNUSED(eType); |
| 239 |
#endif |
| 240 |
|
| 241 |
macroFile.open(sName); |
| 242 |
Base::Console().log("CmdM: Open macro: %s\n", sName); |
| 243 |
} |
| 244 |
|
| 245 |
void MacroManager::commit() |
| 246 |
{ |
| 247 |
QString macroName = macroFile.fileName(); |
| 248 |
if (macroFile.commit()) { |
| 249 |
Base::Console().log("Commit macro: %s\n", (const char*)macroName.toUtf8()); |
| 250 |
} |
| 251 |
else { |
| 252 |
Base::Console().error("Cannot open file to write macro: %s\n", (const char*)macroName.toUtf8()); |
| 253 |
cancel(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void MacroManager::cancel() |
| 258 |
{ |
| 259 |
QString macroName = macroFile.fileName(); |
| 260 |
Base::Console().log("Cancel macro: %s\n", (const char*)macroName.toUtf8()); |
| 261 |
macroFile.cancel(); |
| 262 |
} |
| 263 |
|
| 264 |
void MacroManager::addPendingLine(LineType type, const char* line) |
| 265 |
{ |
| 266 |
buffer.addPendingLine(type, line); |
| 267 |
} |
| 268 |
|
| 269 |
void MacroManager::addLine(LineType Type, const char* sLine) |
| 270 |
{ |
| 271 |
if (!sLine) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
std::function<void(LineType, const char*)> redirectFunc = redirectFuncs.empty() |
| 276 |
? nullptr |
| 277 |
: redirectFuncs.top(); |
| 278 |
if (redirectFunc) { |
| 279 |
redirectFunc(Type, sLine); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
if (buffer.hasPendingLines()) { |
| 284 |
if (buffer.addPendingLineIfComment(Type, sLine)) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
processPendingLines(); |
| 289 |
} |
| 290 |
|
| 291 |
buffer.incrementIfNoComment(Type); |
| 292 |
|
| 293 |
addToOutput(Type, sLine); |
| 294 |
} |
| 295 |
|
| 296 |
void MacroManager::processPendingLines() |
| 297 |
{ |
| 298 |
decltype(buffer.pendingLine) lines; |
| 299 |
lines.swap(buffer.pendingLine); |
| 300 |
for (auto& v : lines) { |
| 301 |
addLine(static_cast<LineType>(v.first), v.second.c_str()); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
void MacroManager::makeComment(QStringList& lines) const |
| 306 |
{ |
| 307 |
for (auto& line : lines) { |
| 308 |
if (!line.startsWith(QLatin1String("#"))) { |
| 309 |
line.prepend(QLatin1String("# ")); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
void MacroManager::addToOutput(LineType type, const char* line) |
| 315 |
{ |
| 316 |
auto [comment, record] = option.values(type); |
| 317 |
|
| 318 |
QStringList lines = QString::fromUtf8(line).split(QLatin1String("\n")); |
| 319 |
if (comment) { |
| 320 |
makeComment(lines); |
| 321 |
} |
| 322 |
|
| 323 |
if (record && macroFile.isOpen()) { |
| 324 |
macroFile.append(lines); |
| 325 |
} |
| 326 |
|
| 327 |
if (option.scriptToPyConsole) { |
| 328 |
// search for the Python console |
| 329 |
auto console = getPythonConsole(); |
| 330 |
if (console) { |
| 331 |
for (auto& line : lines) { |
| 332 |
console->printStatement(line); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
void MacroManager::setModule(const char* sModule) |
| 339 |
{ |
| 340 |
if (macroFile.isOpen() && sModule && *sModule != '\0') { |
| 341 |
macroFile.append(QStringLiteral("import %1").arg(QString::fromLatin1(sModule))); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
PythonConsole* MacroManager::getPythonConsole() const |
| 346 |
{ |
| 347 |
// search for the Python console |
| 348 |
if (!this->pyConsole) { |
| 349 |
this->pyConsole = Gui::getMainWindow()->findChild<Gui::PythonConsole*>(); |
| 350 |
} |
| 351 |
|
| 352 |
return this->pyConsole; |
| 353 |
} |
| 354 |
|
| 355 |
namespace Gui |
| 356 |
{ |
| 357 |
class PythonRedirector |
| 358 |
{ |
| 359 |
public: |
| 360 |
PythonRedirector(const char* type, PyObject* obj) |
| 361 |
: std_out(type) |
| 362 |
, out(obj) |
| 363 |
{ |
| 364 |
if (out) { |
| 365 |
Base::PyGILStateLocker lock; |
| 366 |
old = PySys_GetObject(std_out); |
| 367 |
PySys_SetObject(std_out, out); |
| 368 |
} |
| 369 |
} |
| 370 |
~PythonRedirector() |
| 371 |
{ |
| 372 |
if (out) { |
| 373 |
Base::PyGILStateLocker lock; |
| 374 |
PySys_SetObject(std_out, old); |
| 375 |
Py_DECREF(out); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
private: |
| 380 |
const char* std_out; |
| 381 |
PyObject* out; |
| 382 |
PyObject* old {nullptr}; |
| 383 |
}; |
| 384 |
} // namespace Gui |
| 385 |
|
| 386 |
void MacroManager::run(MacroType eType, const char* sName) |
| 387 |
{ |
| 388 |
Q_UNUSED(eType); |
| 389 |
|
| 390 |
try { |
| 391 |
ParameterGrp::handle hGrp = App::GetApplication() |
| 392 |
.GetUserParameter() |
| 393 |
.GetGroup("BaseApp") |
| 394 |
->GetGroup("Preferences") |
| 395 |
->GetGroup("OutputWindow"); |
| 396 |
PyObject* pyout = hGrp->GetBool("RedirectPythonOutput", true) ? new OutputStdout : nullptr; |
| 397 |
PyObject* pyerr = hGrp->GetBool("RedirectPythonErrors", true) ? new OutputStderr : nullptr; |
| 398 |
PythonRedirector std_out("stdout", pyout); |
| 399 |
PythonRedirector std_err("stderr", pyerr); |
| 400 |
// The given path name is expected to be Utf-8 |
| 401 |
Base::Interpreter().runFile(sName, this->localEnv); |
| 402 |
} |
| 403 |
catch (const Base::SystemExitException&) { |
| 404 |
throw; |
| 405 |
} |
| 406 |
catch (const Base::PyException& e) { |
| 407 |
e.reportException(); |
| 408 |
} |
| 409 |
catch (const Base::Exception& e) { |
| 410 |
qWarning("%s", e.what()); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
PythonDebugger* MacroManager::debugger() const |
| 415 |
{ |
| 416 |
return pyDebugger; |
| 417 |
} |