From 7a9cd348788a22b5c85598339fb86dd36e636b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 29 Aug 2020 19:55:06 +0200 Subject: [PATCH] Update following onModStateChanged + Depreciation warning. --- src/runner/pythonrunner.cpp | 21 ++++++++++++++--- src/runner/pythonutils.cpp | 46 +++++++++++++++++++++++++++++++++++++ src/runner/pythonutils.h | 14 +++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/runner/pythonutils.cpp diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index 8e429c3..91b8701 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -59,7 +59,6 @@ namespace mp11 = boost::mp11; return bpy::object{ (QClass*)w }.attr(str); \ }) - BOOST_PYTHON_MODULE(mobase) { PyEval_InitThreads(); @@ -111,7 +110,8 @@ BOOST_PYTHON_MODULE(mobase) utils::register_associative_container>(); // Required for QVariant since this is QVariantMap. utils::register_associative_container>(); - + utils::register_associative_container>(); + utils::register_associative_container(); // Tuple: @@ -132,7 +132,7 @@ BOOST_PYTHON_MODULE(mobase) utils::register_functor_converter(); utils::register_functor_converter(); utils::register_functor_converter(); // converter for the onModMoved-callback and onPluginMoved callbacks - utils::register_functor_converter(); // converter for the onModStateChanged-callback (IModList) + utils::register_functor_converter&)>(); // converter for the onModStateChanged-callback (IModList) utils::register_functor_converter(); // converter for the onPluginStateChanged-callback (IPluginList) utils::register_functor_converter(); utils::register_functor_converter(); @@ -143,6 +143,9 @@ BOOST_PYTHON_MODULE(mobase) utils::register_functor_converter const&)>(); utils::register_functor_converter(QString const&)>(); + // This one is kept for backward-compatibility while we deprecate onModStateChanged for singl mod. + utils::register_functor_converter(); // converter for the onModStateChanged-callback (IModList). + // // Class declarations: // @@ -652,6 +655,18 @@ BOOST_PYTHON_MODULE(mobase) static_cast(&MOBase::IModList::setActive), (bpy::arg("name"), "active")) .def("priority", &MOBase::IModList::priority, bpy::arg("name")) .def("setPriority", &MOBase::IModList::setPriority, (bpy::arg("name"), "priority")) + + // Kept but deprecated for backward compatibility: + .def("onModStateChanged", +[](IModList* modList, const std::function& fn) { + utils::show_depreciation_warning("onModStateChanged", + "onModStateChanged(Callable[[str, IModList.ModStates], None]) is deprecated, " + "use onModStateChanged(Callable[[Dict[str, IModList.ModStates], None]) instead."); + return modList->onModStateChanged([fn](auto const& map) { + for (const auto& entry : map) { + fn(entry.first, entry.second); + } + }); + }, bpy::arg("callback")) .def("onModStateChanged", &MOBase::IModList::onModStateChanged, bpy::arg("callback")) .def("onModMoved", &MOBase::IModList::onModMoved, bpy::arg("callback")) ; diff --git a/src/runner/pythonutils.cpp b/src/runner/pythonutils.cpp new file mode 100644 index 0000000..8dcdd3d --- /dev/null +++ b/src/runner/pythonutils.cpp @@ -0,0 +1,46 @@ +#include "pythonutils.h" + +#include +#include + +#include + +#include "log.h" + +// Contains the list of filename / line number for which a depreciation warning has already been shown. +static std::set> g_DeprecatedLines; + +namespace utils { + + void show_depreciation_warning(std::string_view name, std::string_view message, bool show_once) { + + // Find the caller: + auto inspect = bpy::import("inspect"); + auto current_frame = inspect.attr("currentframe")(); + auto callable_frame = inspect.attr("getouterframes")(current_frame, 2); + auto filename = bpy::extract(callable_frame[-1].attr("filename"))(); + auto function = bpy::extract(callable_frame[-1].attr("function"))(); + auto lineno = bpy::extract(callable_frame[-1].attr("lineno")); + + // Only show once if requested: + if (show_once && g_DeprecatedLines.contains({ filename, lineno })) { + return; + } + + // Register the depreciation: + g_DeprecatedLines.emplace(filename, lineno); + + auto path = relative(std::filesystem::path(filename), QCoreApplication::applicationDirPath().toStdWString()); + + // Show the message: + if (message.empty()) { + MOBase::log::warn( + "[deprecated] {} in {} [{}:{}].", name, function, path.native(), lineno); + } + else { + MOBase::log::warn( + "[deprecated] {} in {} [{}:{}]: {}", name, function, path.native(), lineno, message); + } + } + +} \ No newline at end of file diff --git a/src/runner/pythonutils.h b/src/runner/pythonutils.h index b424d15..40f4b6d 100644 --- a/src/runner/pythonutils.h +++ b/src/runner/pythonutils.h @@ -3,6 +3,8 @@ #include +#include "error.h" + namespace utils { namespace bpy = boost::python; @@ -195,6 +197,18 @@ namespace utils { , bpy::type_id()); }; + + /** + * @brief Show a depreciation warning. + * + * @param name Name of the deprecated function. + * @param message Depreciation message. + * @param show_once Only show the message once. + * + * @return a depreciation object. + */ + void show_depreciation_warning(std::string_view name, std::string_view message = "", bool show_once = true); + } #endif \ No newline at end of file