From 829ac65cea8326deab80b0710e19c30d73d46d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 29 Aug 2020 12:59:38 +0200 Subject: [PATCH 1/4] Fix bindings of IModList::setActive. --- src/runner/pythonrunner.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index 2738801..8e429c3 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -646,7 +646,10 @@ BOOST_PYTHON_MODULE(mobase) .def("displayName", &MOBase::IModList::displayName, bpy::arg("name")) .def("allMods", &MOBase::IModList::allMods) .def("state", &MOBase::IModList::state, bpy::arg("name")) - .def("setActive", &MOBase::IModList::setActive, (bpy::arg("name"), "active")) + .def("setActive", + static_cast(&MOBase::IModList::setActive), (bpy::arg("names"), "active")) + .def("setActive", + 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")) .def("onModStateChanged", &MOBase::IModList::onModStateChanged, bpy::arg("callback")) 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 2/4] 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 From 3780f157e41e6a1661af0fb3f09f5c626ff85e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sun, 30 Aug 2020 10:25:11 +0200 Subject: [PATCH 3/4] Move the storage for deprecated lines in show_depreciation_warnings(). --- src/runner/pythonutils.cpp | 10 +++++----- src/runner/pythonutils.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/runner/pythonutils.cpp b/src/runner/pythonutils.cpp index 8dcdd3d..088d1b6 100644 --- a/src/runner/pythonutils.cpp +++ b/src/runner/pythonutils.cpp @@ -7,12 +7,12 @@ #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) { + + // Contains the list of filename / line number for which a depreciation warning has already been shown. + static std::set> DeprecatedLines; // Find the caller: auto inspect = bpy::import("inspect"); @@ -23,12 +23,12 @@ namespace utils { auto lineno = bpy::extract(callable_frame[-1].attr("lineno")); // Only show once if requested: - if (show_once && g_DeprecatedLines.contains({ filename, lineno })) { + if (show_once && DeprecatedLines.contains({ filename, lineno })) { return; } // Register the depreciation: - g_DeprecatedLines.emplace(filename, lineno); + DeprecatedLines.emplace(filename, lineno); auto path = relative(std::filesystem::path(filename), QCoreApplication::applicationDirPath().toStdWString()); diff --git a/src/runner/pythonutils.h b/src/runner/pythonutils.h index 40f4b6d..9d77594 100644 --- a/src/runner/pythonutils.h +++ b/src/runner/pythonutils.h @@ -200,7 +200,7 @@ namespace utils { /** * @brief Show a depreciation warning. - * + * * @param name Name of the deprecated function. * @param message Depreciation message. * @param show_once Only show the message once. From d8657946e950ad34178008dfc38a3bcba1ea92ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sun, 30 Aug 2020 11:15:31 +0200 Subject: [PATCH 4/4] Fix depreciation -> deprecation. --- src/runner/pythonrunner.cpp | 2 +- src/runner/pythonutils.cpp | 6 +++--- src/runner/pythonutils.h | 14 ++++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/runner/pythonrunner.cpp b/src/runner/pythonrunner.cpp index 91b8701..c3e274a 100644 --- a/src/runner/pythonrunner.cpp +++ b/src/runner/pythonrunner.cpp @@ -658,7 +658,7 @@ BOOST_PYTHON_MODULE(mobase) // Kept but deprecated for backward compatibility: .def("onModStateChanged", +[](IModList* modList, const std::function& fn) { - utils::show_depreciation_warning("onModStateChanged", + utils::show_deprecation_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) { diff --git a/src/runner/pythonutils.cpp b/src/runner/pythonutils.cpp index 088d1b6..37445c5 100644 --- a/src/runner/pythonutils.cpp +++ b/src/runner/pythonutils.cpp @@ -9,9 +9,9 @@ namespace utils { - void show_depreciation_warning(std::string_view name, std::string_view message, bool show_once) { + void show_deprecation_warning(std::string_view name, std::string_view message, bool show_once) { - // Contains the list of filename / line number for which a depreciation warning has already been shown. + // Contains the list of filename / line number for which a deprecation warning has already been shown. static std::set> DeprecatedLines; // Find the caller: @@ -27,7 +27,7 @@ namespace utils { return; } - // Register the depreciation: + // Register the deprecation: DeprecatedLines.emplace(filename, lineno); auto path = relative(std::filesystem::path(filename), QCoreApplication::applicationDirPath().toStdWString()); diff --git a/src/runner/pythonutils.h b/src/runner/pythonutils.h index 9d77594..92bb301 100644 --- a/src/runner/pythonutils.h +++ b/src/runner/pythonutils.h @@ -199,15 +199,17 @@ namespace utils { /** - * @brief Show a depreciation warning. + * @brief Show a deprecation warning. + * + * This methods will print a warning in MO2 log containing the location of the call to + * the deprecated function. If show_once is true, the deprecation warning will only be + * logged the first time the function is called at this location. * * @param name Name of the deprecated function. - * @param message Depreciation message. - * @param show_once Only show the message once. - * - * @return a depreciation object. + * @param message Deprecation message. + * @param show_once Only show the message once per call location. */ - void show_depreciation_warning(std::string_view name, std::string_view message = "", bool show_once = true); + void show_deprecation_warning(std::string_view name, std::string_view message = "", bool show_once = true); }