Browse Source

python: expose essentials of utility.ConfigurationGroup.

Need that for configuring plugins -- originally thought I'd wait until
the class is reworked, but that isn't coming anytime soon given there's
insane pressure on other things, so I have to figure out a plan B.

The interface is the most barebones possible, exposing nothing that I'd
later plan to rename or remove. Just the least possible amount of APIs
to make it possible to configure plugins, and no custom type support yet
either -- if you need to write a vector, write it as a formatted string
instead, sorry.

To make it possible to test the APIs in isolation instead of through
the plugin interface, I added also a Configuration class. Also just the
least amount possible to cover the code with tests.
next
Vladimír Vondruš 3 years ago
parent
commit
12dbfc56c8
  1. 4
      doc/python/conf.py
  2. 33
      doc/python/corrade.utility.rst
  3. 2
      doc/python/pages/changelog.rst
  4. 1
      src/python/CMakeLists.txt
  5. 21
      src/python/corrade/CMakeLists.txt
  6. 2
      src/python/corrade/__init__.py
  7. 1
      src/python/corrade/bootstrap.h
  8. 3
      src/python/corrade/corrade.cpp
  9. 1
      src/python/corrade/test/broken.conf
  10. 6
      src/python/corrade/test/file.conf
  11. 141
      src/python/corrade/test/test_utility.py
  12. 95
      src/python/corrade/utility.cpp
  13. 1
      src/python/setup.py.cmake

4
doc/python/conf.py

@ -7,6 +7,7 @@ sys.path = [os.path.join(os.path.dirname(__file__), '../../build/src/python/')]
import corrade import corrade
import corrade.containers import corrade.containers
import corrade.pluginmanager import corrade.pluginmanager
import corrade.utility
import magnum import magnum
import magnum.gl import magnum.gl
@ -24,7 +25,7 @@ import magnum.trade
# So the doc see everything # So the doc see everything
# TODO: use just +=, m.css should reorder this on its own # TODO: use just +=, m.css should reorder this on its own
corrade.__all__ = ['containers', 'pluginmanager', 'BUILD_DEPRECATED', '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', 'utility', 'BUILD_DEPRECATED', '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', 'text', 'trade', 'BUILD_DEPRECATED', 'BUILD_STATIC', 'TARGET_GL', 'TARGET_GLES', 'TARGET_GLES2', 'TARGET_WEBGL', 'TARGET_EGL', 'TARGET_VK'] + magnum.__all__ magnum.__all__ = ['math', 'gl', 'meshtools', 'platform', 'primitives', 'shaders', 'scenegraph', 'text', 'trade', 'BUILD_DEPRECATED', 'BUILD_STATIC', 'TARGET_GL', 'TARGET_GLES', 'TARGET_GLES2', 'TARGET_WEBGL', 'TARGET_EGL', 'TARGET_VK'] + magnum.__all__
# hide values of the preprocessor defines to avoid confusion by assigning a # hide values of the preprocessor defines to avoid confusion by assigning a
@ -176,6 +177,7 @@ INPUT_DOCS = [
'corrade.rst', 'corrade.rst',
'corrade.containers.rst', 'corrade.containers.rst',
'corrade.pluginmanager.rst', 'corrade.pluginmanager.rst',
'corrade.utility.rst',
'magnum.rst', 'magnum.rst',
'magnum.gl.rst', 'magnum.gl.rst',

33
doc/python/corrade.utility.rst

@ -0,0 +1,33 @@
..
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022 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.
..
.. py:function:: corrade.utility.ConfigurationGroup.group
:raise KeyError: If group :p:`name` doesn't exist
.. py:function:: corrade.utility.Configuration.__init__
:raise IOError: If :p:`filename` contains a parse error
.. py:function:: corrade.utility.Configuration.save
:raise IOError: If the file can't be saved

2
doc/python/pages/changelog.rst

@ -123,6 +123,8 @@ Changelog
- Exposed :ref:`Color3.red()` and other convenience constructors (see - Exposed :ref:`Color3.red()` and other convenience constructors (see
:gh:`mosra/magnum-bindings#12`) :gh:`mosra/magnum-bindings#12`)
- Exposed the :ref:`text` library - Exposed the :ref:`text` library
- Exposed the minimal interface of :ref:`utility.ConfigurationGroup` and
:ref:`utility.Configuration`
- Fixed issues with an in-source build (see :gh:`mosra/magnum-bindings#13`) - Fixed issues with an in-source build (see :gh:`mosra/magnum-bindings#13`)
- All CMake build options are now prefixed with ``MAGNUM_``. For backwards - All CMake build options are now prefixed with ``MAGNUM_``. For backwards
compatibility, unless ``MAGNUM_BUILD_DEPRECATED`` is disabled and unless a compatibility, unless ``MAGNUM_BUILD_DEPRECATED`` is disabled and unless a

1
src/python/CMakeLists.txt

@ -62,6 +62,7 @@ add_subdirectory(magnum)
foreach(target foreach(target
corrade_containers corrade_containers
corrade_pluginmanager corrade_pluginmanager
corrade_utility
magnum_gl magnum_gl
magnum_meshtools magnum_meshtools
magnum_primitives magnum_primitives

21
src/python/corrade/CMakeLists.txt

@ -43,6 +43,9 @@ set(corrade_containers_SRCS
set(corrade_pluginmanager_SRCS set(corrade_pluginmanager_SRCS
pluginmanager.cpp) pluginmanager.cpp)
set(corrade_utility_SRCS
utility.cpp)
# If Corrade is not built as static, compile the sub-libraries as separate # If Corrade is not built as static, compile the sub-libraries as separate
# modules # modules
if(NOT CORRADE_BUILD_STATIC) if(NOT CORRADE_BUILD_STATIC)
@ -57,6 +60,16 @@ if(NOT CORRADE_BUILD_STATIC)
OUTPUT_NAME "containers" OUTPUT_NAME "containers"
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/corrade) LIBRARY_OUTPUT_DIRECTORY ${output_dir}/corrade)
pybind11_add_module(corrade_utility ${pybind11_add_module_SYSTEM} ${corrade_utility_SRCS})
target_include_directories(corrade_utility PRIVATE
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/python)
target_link_libraries(corrade_utility PRIVATE
Corrade::Utility)
set_target_properties(corrade_utility PROPERTIES
OUTPUT_NAME "utility"
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/corrade)
if(Corrade_PluginManager_FOUND) if(Corrade_PluginManager_FOUND)
pybind11_add_module(corrade_pluginmanager ${pybind11_add_module_SYSTEM} ${corrade_pluginmanager_SRCS}) pybind11_add_module(corrade_pluginmanager ${pybind11_add_module_SYSTEM} ${corrade_pluginmanager_SRCS})
target_include_directories(corrade_pluginmanager PRIVATE target_include_directories(corrade_pluginmanager PRIVATE
@ -76,8 +89,12 @@ else()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/staticconfigure.h.cmake configure_file(${CMAKE_CURRENT_SOURCE_DIR}/staticconfigure.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/staticconfigure.h) ${CMAKE_CURRENT_BINARY_DIR}/staticconfigure.h)
list(APPEND corrade_SRCS ${corrade_containers_SRCS}) list(APPEND corrade_SRCS
list(APPEND corrade_LIBS Corrade::Containers) ${corrade_containers_SRCS}
${corrade_utility_SRCS})
list(APPEND corrade_LIBS
Corrade::Containers
Corrade::Utility)
if(Corrade_PluginManager_FOUND) if(Corrade_PluginManager_FOUND)
list(APPEND corrade_SRCS ${corrade_pluginmanager_SRCS}) list(APPEND corrade_SRCS ${corrade_pluginmanager_SRCS})

2
src/python/corrade/__init__.py

@ -33,7 +33,7 @@ import sys
# _corrade. The following feels extremely hackish, but without that it wouldn't # _corrade. The following feels extremely hackish, but without that it wouldn't
# be possible to do `import corrade.containers`, which is weird # be possible to do `import corrade.containers`, which is weird
# (`from corrade import containers` works, tho, for whatever reason) # (`from corrade import containers` works, tho, for whatever reason)
for i in ['containers', 'pluginmanager']: for i in ['containers', 'pluginmanager', 'utility']:
if i in globals(): sys.modules['corrade.' + i] = globals()[i] if i in globals(): sys.modules['corrade.' + i] = globals()[i]
# Prevent all submodules being pulled in when saying `from corrade import *` -- # Prevent all submodules being pulled in when saying `from corrade import *` --

1
src/python/corrade/bootstrap.h

@ -48,6 +48,7 @@ namespace py = pybind11;
void containers(py::module_& m); void containers(py::module_& m);
void pluginmanager(py::module_& m); void pluginmanager(py::module_& m);
void utility(py::module_& m);
} }

3
src/python/corrade/corrade.cpp

@ -132,5 +132,8 @@ PYBIND11_MODULE(_corrade, m) {
py::module_ pluginmanager = m.def_submodule("pluginmanager"); py::module_ pluginmanager = m.def_submodule("pluginmanager");
corrade::pluginmanager(pluginmanager); corrade::pluginmanager(pluginmanager);
#endif #endif
py::module_ utility = m.def_submodule("utility");
corrade::utility(utility);
#endif #endif
} }

1
src/python/corrade/test/broken.conf

@ -0,0 +1 @@
]]] yes this is invalid!!!

6
src/python/corrade/test/file.conf

@ -0,0 +1,6 @@
someKey=42
[someGroup]
value=hello
[someGroup/subgroup]
anotherValue=another
[emptyGroup]

141
src/python/corrade/test/test_utility.py

@ -0,0 +1,141 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
# 2020, 2021, 2022 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.
#
import os
import sys
import tempfile
import unittest
from corrade import utility
# Tests also the ConfigurationGroup bindings, as a ConfigurationGroup cannot be
# constructed as a standalone type
class Configuration(unittest.TestCase):
def test_open(self):
a = utility.Configuration(os.path.join(os.path.dirname(__file__), "file.conf"))
a_refcount = sys.getrefcount(a)
self.assertEqual(a['someKey'], '42')
self.assertEqual(a['nonexistent'], '')
b = a.group('someGroup')
b_refcount = sys.getrefcount(b)
self.assertEqual(sys.getrefcount(a), a_refcount + 1)
self.assertEqual(b['value'], 'hello')
c = b.group('subgroup')
self.assertEqual(sys.getrefcount(a), a_refcount + 1)
self.assertEqual(sys.getrefcount(b), b_refcount + 1)
self.assertEqual(c['anotherValue'], 'another')
del c
self.assertEqual(sys.getrefcount(b), b_refcount)
del b
self.assertEqual(sys.getrefcount(a), a_refcount)
def test_nonexistent_group(self):
a = utility.Configuration(os.path.join(os.path.dirname(__file__), "file.conf"))
a_refcount = sys.getrefcount(a)
with self.assertRaises(KeyError):
a.group('nonexistent')
self.assertEqual(sys.getrefcount(a), a_refcount)
def test_save(self):
with tempfile.TemporaryDirectory() as tmp:
filename = os.path.join(tmp, "file.conf")
a = utility.Configuration(filename)
a['value'] = 'hello'
a_refcount = sys.getrefcount(a)
b = a.add_group('someGroup')
self.assertEqual(sys.getrefcount(a), a_refcount + 1)
b['someKey'] = '42'
# This should not delete the group from the configuration
del b
self.assertEqual(sys.getrefcount(a), a_refcount)
a.save()
with open(filename, 'r') as f:
self.assertEqual(f.read(), "value=hello\n[someGroup]\nsomeKey=42\n")
def test_save_different_filename(self):
a = utility.Configuration()
a['value'] = 'hello'
with tempfile.TemporaryDirectory() as tmp:
filename = os.path.join(tmp, "file.conf")
a.save(filename)
with open(filename, 'r') as f:
self.assertEqual(f.read(), "value=hello\n")
def test_save_implicit(self):
with tempfile.TemporaryDirectory() as tmp:
filename = os.path.join(tmp, "file.conf")
a = utility.Configuration(filename)
a['value'] = 'hello'
self.assertFalse(os.path.exists(filename))
del a
self.assertTrue(os.path.exists(filename))
with open(filename, 'r') as f:
self.assertEqual(f.read(), "value=hello\n")
def test_open_nonexistent(self):
# This should not raise any exception as it's a valid use case (i.e,
# opening an app for the first time)
a = utility.Configuration("nonexistent.conf")
self.assertFalse(a.has_groups)
self.assertFalse(a.has_values)
def test_open_failed(self):
with self.assertRaises(IOError):
utility.Configuration(os.path.join(os.path.dirname(__file__), "broken.conf"))
@unittest.skipIf(os.access("/", os.W_OK), "root dir is writable")
def test_save_failed(self):
# The file doesn't exist, which means nothing is parsed during
# construction. But saving will fail as the directory is not writable.
a = utility.Configuration("/nonexistent.conf")
self.assertFalse(a.has_groups)
self.assertFalse(a.has_values)
with self.assertRaises(IOError):
a.save()
def test_save_different_filename_failed(self):
a = utility.Configuration()
with self.assertRaises(IOError):
a.save("/some/path/that/does/not/exist.conf")

95
src/python/corrade/utility.cpp

@ -0,0 +1,95 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022 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 <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Configuration.h>
#include "corrade/bootstrap.h"
namespace corrade {
void utility(py::module_& m) {
m.doc() = "Utilities";
py::class_<Utility::ConfigurationGroup>{m, "ConfigurationGroup", "Group of values in a configuration file"}
.def_property_readonly("has_groups", &Utility::ConfigurationGroup::hasGroups, "Whether this group has any subgroups")
.def("group", [](Utility::ConfigurationGroup& self, const std::string& name) {
Utility::ConfigurationGroup* group = self.group(name);
if(!group) {
PyErr_SetNone(PyExc_KeyError);
throw py::error_already_set{};
}
return group;
}, "Group", py::arg("name"), py::return_value_policy::reference_internal)
.def("add_group", [](Utility::ConfigurationGroup& self, const std::string& name) {
Utility::ConfigurationGroup* group = self.addGroup(name);
CORRADE_INTERNAL_ASSERT(group);
return group;
}, "Add a group", py::arg("name"), py::return_value_policy::reference_internal)
.def_property_readonly("has_values", &Utility::ConfigurationGroup::hasValues, "Whether this group has any values")
.def("__getitem__", [](Utility::ConfigurationGroup& self, const std::string& key) {
/** @todo should return an Optional once ConfigurationGroup is
reworked */
return self.value(key);
}, "Value", py::arg("key"))
.def("__setitem__", [](Utility::ConfigurationGroup& self, const std::string& key, const std::string& value) {
self.setValue(key, value);
}, "Set a value", py::arg("key"), py::arg("value"));
py::class_<Utility::Configuration, Utility::ConfigurationGroup>{m, "Configuration", "Parser and writer for configuration files"}
.def(py::init(), "Construct an empty configuration")
.def(py::init([](const std::string& filename) {
std::unique_ptr<Utility::Configuration> self{new Utility::Configuration{filename}};
if(!self->isValid()) {
PyErr_SetNone(PyExc_IOError);
throw py::error_already_set{};
}
return self;
}), "Parse a configuration file", py::arg("filename"))
.def("save", [](Utility::Configuration& self) {
if(!self.save()) {
PyErr_SetNone(PyExc_IOError);
throw py::error_already_set{};
}
}, "Save the configuration")
.def("save", [](Utility::Configuration& self, const std::string& filename) {
if(!self.save(filename)) {
PyErr_SetNone(PyExc_IOError);
throw py::error_already_set{};
}
}, "Save the configuration to another file", py::arg("filename"));
}
}
#ifndef CORRADE_BUILD_STATIC
/* TODO: remove declaration when https://github.com/pybind/pybind11/pull/1863
is released */
extern "C" PYBIND11_EXPORT PyObject* PyInit_utility();
PYBIND11_MODULE(utility, m) {
corrade::utility(m);
}
#endif

1
src/python/setup.py.cmake

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

Loading…
Cancel
Save