From 7333bde006b6adcbc31f242b1fe7b5d325a53710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 16 May 2020 17:37:57 +0200 Subject: [PATCH] Add makeTree in moprivate. --- src/runner/pythonrunner.cpp | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index 007f21e..1cd31ca 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -879,6 +879,46 @@ BOOST_PYTHON_MODULE(moprivate) .def("startRecordingExceptionMessage", &ErrWrapper::startRecordingExceptionMessage) .def("stopRecordingExceptionMessage", &ErrWrapper::stopRecordingExceptionMessage) .def("getLastExceptionMessage", &ErrWrapper::getLastExceptionMessage); + + utils::register_functor_converter(); + + // Expose a function to create a particular tree, only for debugging purpose, not in mobase. + bpy::def("makeTree", +[](std::function callback) -> std::shared_ptr { + struct FileTree : IFileTree { + + using callback_t = std::function; + + FileTree(std::shared_ptr parent, QString name, callback_t callback) : + FileTreeEntry(parent, name), IFileTree(), m_Callback(callback){ } + + std::shared_ptr addFile(QString name, QDateTime time) override { + if (m_Callback && !m_Callback(name, false)) { + throw UnsupportedOperationException("File rejected by callback."); + } + return IFileTree::addFile(name, time); + } + + std::shared_ptr addDirectory(QString name) override { + if (m_Callback && !m_Callback(name, true)) { + throw UnsupportedOperationException("Directory rejected by callback."); + } + return IFileTree::addDirectory(name); + } + + protected: + + std::shared_ptr makeDirectory(std::shared_ptr parent, QString name) const override { + return std::make_shared(parent, name, m_Callback); + } + + void doPopulate(std::shared_ptr parent, std::vector>& entries) const override { } + std::shared_ptr doClone() const override { return std::make_shared(nullptr, name(), m_Callback); } + + private: + callback_t m_Callback; + }; + return std::make_shared(nullptr, "", callback); + }, bpy::arg("callback") = bpy::object{}); } bool PythonRunner::initPython(const QString &pythonPath)