Browse Source

python: expose pluginmanager.AbstractManager.set_preferred_plugins().

It used to take an initializer list, not anymore. Will test this better
once I have to build e.g. AssimpImporter for some other reason -- then I
can use this function to prefer it for loading glTFs and see how that
manages.
next
Vladimír Vondruš 3 years ago
parent
commit
aadd07dad6
  1. 3
      doc/python/corrade.pluginmanager.rst
  2. 1
      doc/python/pages/changelog.rst
  3. 15
      src/python/corrade/pluginmanager.cpp
  4. 11
      src/python/magnum/test/test_trade.py

3
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

1
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

15
src/python/corrade/pluginmanager.cpp

@ -27,6 +27,7 @@
#include <pybind11/stl.h> /* for pluginList() and aliasList() */
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/StringStl.h> /** @todo drop once we have our string casters */
#include <Corrade/Containers/StringIterable.h>
#include <Corrade/PluginManager/AbstractManager.h>
#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<std::string>& 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<Containers::StringView> 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<std::string> out;

11
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)

Loading…
Cancel
Save