Browse Source

python: expose basics of plugin manager.

Just the minimum needed to get Trade::AbstractImporter running.
pull/8/head
Vladimír Vondruš 7 years ago
parent
commit
9faed0651e
  1. 3
      doc/python/conf.py
  2. 11
      src/Corrade/Python.h
  3. 11
      src/Magnum/Python.h
  4. 28
      src/python/corrade/CMakeLists.txt
  5. 1
      src/python/corrade/bootstrap.h
  6. 5
      src/python/corrade/corrade.cpp
  7. 77
      src/python/corrade/pluginmanager.cpp
  8. 29
      src/python/corrade/staticconfigure.h.cmake
  9. 2
      src/python/magnum/shaders.cpp
  10. 1
      src/python/setup.py.cmake

3
doc/python/conf.py

@ -6,6 +6,7 @@ sys.path = [os.path.join(os.path.dirname(__file__), '../../build/src/python/')]
import corrade
import corrade.containers
import corrade.pluginmanager
import magnum
import magnum.gl
@ -22,7 +23,7 @@ import magnum.trade
# So the doc see everything
# TODO: use just +=, m.css should reorder this on its own
corrade.__all__ = ['containers', 'BUILD_STATIC', 'BUILD_MULTITHREADED', 'TARGET_UNIX', 'TARGET_APPLE', 'TARGET_IOS', 'TARGET_IOS_SIMULATOR', 'TARGET_WINDOWS', 'TARGET_WINDOWS_RT', 'TARGET_EMSCRIPTEN', 'TARGET_ANDROID']
corrade.__all__ = ['containers', 'pluginmanager', 'BUILD_STATIC', 'BUILD_MULTITHREADED', 'TARGET_UNIX', 'TARGET_APPLE', 'TARGET_IOS', 'TARGET_IOS_SIMULATOR', 'TARGET_WINDOWS', 'TARGET_WINDOWS_RT', 'TARGET_EMSCRIPTEN', 'TARGET_ANDROID']
magnum.__all__ = ['math', 'gl', 'meshtools', 'platform', 'primitives', 'shaders', 'scenegraph', 'trade', 'BUILD_STATIC', 'TARGET_GL', 'TARGET_GLES', 'TARGET_GLES2', 'TARGET_WEBGL', 'TARGET_VK'] + magnum.__all__
# hide values of the preprocessor defines to avoid confusion by assigning a

11
src/Corrade/Python.h

@ -88,6 +88,17 @@ template<template<class> class T, class U> T<U>& pyObjectHolderFor(U& obj) {
return pyObjectHolderFor<T<U>>(pybind11::detail::get_object_handle(&obj, typeinfo), typeinfo);
}
/* This is a variant of https://github.com/pybind/pybind11/issues/1178,
implemented on the client side instead of patching pybind itself */
template<class, bool> struct PyNonDestructibleBaseDeleter;
template<class T> struct PyNonDestructibleBaseDeleter<T, false> {
void operator()(T*) { CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ }
};
template<class T> struct PyNonDestructibleBaseDeleter<T, true> {
void operator()(T* ptr) { delete ptr; }
};
template<class T, class... Args> using PyNonDestructibleClass = pybind11::class_<T, Args..., std::unique_ptr<T, PyNonDestructibleBaseDeleter<T, std::is_destructible<T>::value>>>;
}
#endif

11
src/Magnum/Python.h

@ -48,17 +48,6 @@ template<class T> PyImageViewHolder<T> pyImageViewHolder(const T& view, pybind11
return PyImageViewHolder<T>{new T{view}, owner};
}
/* This is a variant of https://github.com/pybind/pybind11/issues/1178,
implemented on the client side instead of patching pybind itself */
template<class, bool> struct PyNonDestructibleBaseDeleter;
template<class T> struct PyNonDestructibleBaseDeleter<T, false> {
void operator()(T*) { CORRADE_ASSERT_UNREACHABLE(); /* LCOV_EXCL_LINE */ }
};
template<class T> struct PyNonDestructibleBaseDeleter<T, true> {
void operator()(T* ptr) { delete ptr; }
};
template<class T, class... Args> using PyNonDestructibleClass = pybind11::class_<T, Args..., std::unique_ptr<T, PyNonDestructibleBaseDeleter<T, std::is_destructible<T>::value>>>;
}
PYBIND11_DECLARE_HOLDER_TYPE(T, Magnum::PyImageViewHolder<T>)

28
src/python/corrade/CMakeLists.txt

@ -23,6 +23,10 @@
# DEALINGS IN THE SOFTWARE.
#
# *Not* REQUIRED
find_package(Corrade COMPONENTS
PluginManager)
set(corrade_SRCS
corrade.cpp)
@ -32,6 +36,9 @@ set(corrade_LIBS )
set(corrade_containers_SRCS
containers.cpp)
set(corrade_pluginmanager_SRCS
pluginmanager.cpp)
# If Corrade is not built as static, compile the sub-libraries as separate
# modules
if(NOT CORRADE_BUILD_STATIC)
@ -47,18 +54,37 @@ if(NOT CORRADE_BUILD_STATIC)
OUTPUT_NAME "containers"
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/corrade)
if(Corrade_PluginManager_FOUND)
pybind11_add_module(corrade_pluginmanager SYSTEM ${corrade_pluginmanager_SRCS})
target_include_directories(corrade_pluginmanager PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/python)
target_link_libraries(corrade_pluginmanager PRIVATE
Corrade::PluginManager)
set_target_properties(corrade_pluginmanager PROPERTIES
FOLDER "python/corrade"
OUTPUT_NAME "pluginmanager"
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/corrade)
endif()
# Otherwise put it all into one library so it's easier to install (which is the
# point of static builds). It also nicely avoids problems with multiply-defined
# global data.
else()
list(APPEND corrade_SRCS ${corrade_containers_SRCS})
list(APPEND corrade_LIBS Corrade::Containers)
if(Corrade_PluginManager_FOUND)
list(APPEND corrade_SRCS ${corrade_pluginmanager_SRCS})
list(APPEND corrade_LIBS Corrade::PluginManager)
endif()
endif()
pybind11_add_module(corrade SYSTEM ${corrade_SRCS})
target_include_directories(corrade PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/python)
${PROJECT_SOURCE_DIR}/src/python
${PROJECT_BINARY_DIR}/src/python) # for static build
target_link_libraries(corrade PRIVATE Corrade::Utility ${corrade_LIBS})
set_target_properties(corrade PROPERTIES
FOLDER "python"

1
src/python/corrade/bootstrap.h

@ -34,6 +34,7 @@ using namespace Corrade;
namespace py = pybind11;
void containers(py::module& m);
void pluginmanager(py::module& m);
}

5
src/python/corrade/corrade.cpp

@ -116,5 +116,10 @@ PYBIND11_MODULE(_corrade, m) {
#ifdef CORRADE_BUILD_STATIC
py::module containers = m.def_submodule("containers");
corrade::containers(containers);
#ifdef Corrade_PluginManager_FOUND
py::module pluginmanager = m.def_submodule("pluginmanager");
corrade::pluginmanager(pluginmanager);
#endif
#endif
}

77
src/python/corrade/pluginmanager.cpp

@ -0,0 +1,77 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h> /* for pluginList() and aliasList() */
#include <Corrade/PluginManager/AbstractManager.h>
#include "Corrade/Python.h"
#include "corrade/bootstrap.h"
#include "corrade/EnumOperators.h"
namespace corrade {
void pluginmanager(py::module& m) {
m.doc() = "Plugin management";
py::enum_<PluginManager::LoadState> loadState{m, "LoadState", "Plugin load state"};
loadState
.value("NOT_FOUND", PluginManager::LoadState::NotFound)
.value("WRONG_PLUGIN_VERSION", PluginManager::LoadState::WrongPluginVersion)
.value("WRONG_INTERFACE_VERSION", PluginManager::LoadState::WrongInterfaceVersion)
.value("WRONG_METADATA_FILE", PluginManager::LoadState::WrongMetadataFile)
.value("UNRESOLVED_DEPENDENCY", PluginManager::LoadState::UnresolvedDependency)
.value("STATIC", PluginManager::LoadState::Static)
.value("LOADED", PluginManager::LoadState::Loaded)
.value("NOT_LOADED", PluginManager::LoadState::NotLoaded)
.value("UNLOAD_FAILED", PluginManager::LoadState::UnloadFailed)
.value("REQUIRED", PluginManager::LoadState::Required)
.value("USED", PluginManager::LoadState::Used);
corrade::enumOperators(loadState);
PyNonDestructibleClass<PluginManager::AbstractManager> manager{m, "AbstractManager", "Base for plugin managers"};
manager.attr("VERSION") = PluginManager::AbstractManager::Version;
manager
.def_property_readonly("plugin_interface", &PluginManager::AbstractManager::pluginInterface, "Plugin interface")
.def_property("plugin_directory", &PluginManager::AbstractManager::pluginDirectory, &PluginManager::AbstractManager::setPluginDirectory, "Plugin directory")
.def("reload_plugin_directory", &PluginManager::AbstractManager::reloadPluginDirectory, "Reload plugin directory")
/** @todo setPreferredPlugins (takes an init list) */
.def("plugin_list", &PluginManager::AbstractManager::pluginList, "List of all available plugin names")
.def("alias_list", &PluginManager::AbstractManager::aliasList, "List of all available alias names")
/** @todo metadata() (figure out the ownership) */
.def("load_state", &PluginManager::AbstractManager::loadState, "Load state of a plugin")
.def("load", &PluginManager::AbstractManager::load, "Load a plugin")
.def("unload", &PluginManager::AbstractManager::unload, "Unload a plugin");
}
}
#ifndef CORRADE_BUILD_STATIC
extern "C" PYBIND11_EXPORT PyObject* PyInit_pluginmanager();
PYBIND11_MODULE(pluginmanager, m) {
corrade::pluginmanager(m);
}
#endif

29
src/python/corrade/staticconfigure.h.cmake

@ -0,0 +1,29 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/* Named corrade/staticconfigure.h to avoid it colliding with Corrade/configure.h
on case-insensitive filesystems */
#cmakedefine Corrade_PluginManager_FOUND

2
src/python/magnum/shaders.cpp

@ -29,7 +29,7 @@
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Shaders/VertexColor.h>
#include "Magnum/Python.h"
#include "Corrade/Python.h"
#include "corrade/EnumOperators.h"
#include "magnum/bootstrap.h"

1
src/python/setup.py.cmake

@ -35,6 +35,7 @@ extension_paths = {
# present.
'_corrade': '$<TARGET_FILE:corrade>',
'corrade.containers': '${corrade_containers_file}',
'corrade.pluginmanager': '${corrade_pluginmanager_file}',
'_magnum': '$<TARGET_FILE:magnum>',
'magnum.gl': '${magnum_gl_file}',
'magnum.meshtools': '${magnum_meshtools_file}',

Loading…
Cancel
Save