Browse Source
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
13 changed files with 307 additions and 4 deletions
@ -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 |
||||
@ -0,0 +1,6 @@
|
||||
someKey=42 |
||||
[someGroup] |
||||
value=hello |
||||
[someGroup/subgroup] |
||||
anotherValue=another |
||||
[emptyGroup] |
||||
@ -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") |
||||
|
||||
@ -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 |
||||
Loading…
Reference in new issue