diff --git a/doc/python/corrade.pluginmanager.rst b/doc/python/corrade.pluginmanager.rst index 43ee815..e866f24 100644 --- a/doc/python/corrade.pluginmanager.rst +++ b/doc/python/corrade.pluginmanager.rst @@ -34,3 +34,6 @@ .. py:function:: corrade.pluginmanager.AbstractManager.unload :raise RuntimeError: When unloading fails :return: Either :ref:`LoadState.NOT_LOADED` or :ref:`LoadState.STATIC` + +.. py:function:: corrade.pluginmanager.AbstractManager.set_preferred_plugins + :raise KeyError: If the alias doesn't exist diff --git a/doc/python/pages/changelog.rst b/doc/python/pages/changelog.rst index 99f6998..13f4e78 100644 --- a/doc/python/pages/changelog.rst +++ b/doc/python/pages/changelog.rst @@ -125,6 +125,7 @@ Changelog - Exposed the :ref:`text` library - Exposed the minimal interface of :ref:`utility.ConfigurationGroup` and :ref:`utility.Configuration` +- Exposed :ref:`pluginmanager.AbstractManager.set_preferred_plugins()` - Fixed issues with an in-source build (see :gh:`mosra/magnum-bindings#13`) - All CMake build options are now prefixed with ``MAGNUM_``. For backwards compatibility, unless ``MAGNUM_BUILD_DEPRECATED`` is disabled and unless a diff --git a/src/python/corrade/pluginmanager.cpp b/src/python/corrade/pluginmanager.cpp index 487bf06..bc65dc3 100644 --- a/src/python/corrade/pluginmanager.cpp +++ b/src/python/corrade/pluginmanager.cpp @@ -27,6 +27,7 @@ #include /* for pluginList() and aliasList() */ #include #include /** @todo drop once we have our string casters */ +#include #include #include "Corrade/PythonBindings.h" @@ -69,7 +70,19 @@ void pluginmanager(py::module_& m) { self.setPluginDirectory(directory); }, "Plugin directory") .def("reload_plugin_directory", &PluginManager::AbstractManager::reloadPluginDirectory, "Reload plugin directory") - /** @todo setPreferredPlugins (takes an init list) */ + .def("set_preferred_plugins", [](PluginManager::AbstractManager& self, const std::string& alias, const std::vector& plugins) { + if(self.loadState(alias) == PluginManager::LoadState::NotFound) { + PyErr_SetNone(PyExc_KeyError); + throw py::error_already_set{}; + } + + /** @todo drop all this once StringIterable can be a view on + std::strings */ + Containers::Array pluginViews{NoInit, plugins.size()}; + for(std::size_t i = 0; i != plugins.size(); ++i) + pluginViews[i] = plugins[i]; + self.setPreferredPlugins(alias, pluginViews); + }, "Set preferred plugins for given alias", py::arg("alias"), py::arg("plugins")) .def_property_readonly("plugin_list", [](PluginManager::AbstractManager& self) { /** @todo make a generic caster for arbitrary arrays and strings */ std::vector out; diff --git a/src/python/magnum/test/test_trade.py b/src/python/magnum/test/test_trade.py index ed9e075..da7a7e4 100644 --- a/src/python/magnum/test/test_trade.py +++ b/src/python/magnum/test/test_trade.py @@ -1001,6 +1001,17 @@ class Importer(unittest.TestCase): with self.assertRaisesRegex(RuntimeError, "can't unload plugin"): manager.unload('NonexistentImporter') + def test_set_preferred_plugins(self): + manager = trade.ImporterManager() + # TODO test this better once we can verify it gets actually loaded + manager.set_preferred_plugins('TgaImporter', ['StbImageImporter', 'DevIlImageImporter']) + self.assertIn('StbImageImporter', manager.alias_list) + + def test_set_preferred_plugins_alias_not_found(self): + manager = trade.ImporterManager() + with self.assertRaises(KeyError): + manager.set_preferred_plugins('ApngImporter', []) + def test_no_file_opened(self): importer = trade.ImporterManager().load_and_instantiate('StbImageImporter') self.assertFalse(importer.is_opened)