Verified Commit dfe98e69 authored by Karel Koci's avatar Karel Koci 🤘

Add EUci

parent b723f63d
......@@ -5,3 +5,4 @@
!/.gitlab-ci.yml
build
__pycache__
# Copyright (c) 2019, CZ.NIC, z.s.p.o. (http://www.nic.cz/)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the CZ.NIC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL CZ.NIC BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from uci import Uci
class EUci(Uci):
"""Extended Uci wrapper
"""
__BOOLEAN_VALUES = {
"0": False,
"no": False,
"off": False,
"false": False,
"disabled": False,
"1": True,
"yes": True,
"on": True,
"true": True,
"enabled": True,
}
__BOOLEAN_TRUE = "1"
__BOOLEAN_FALSE = "0"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_boolean(self, *args):
"""Returns given UCI config as a boolean.
Value '0', 'no', 'off', 'false' or 'disabled' is returned as False.
Value '1' , 'yes', 'on', 'true' or 'enabled' is returned as True.
ValueError is raised on any other value.
"""
value = self.get(*args)
if value.lower() in self.__BOOLEAN_VALUES:
return self.__BOOLEAN_VALUES[value.lower()]
raise ValueError
def set_boolean(self, *args):
"""Sets boolean value to given UCI config.
"""
self.set(*(args[:-1]), self.__BOOLEAN_TRUE if args[-1] else self.__BOOLEAN_FALSE)
def get_integer(self, *args):
"""Returns given UCI config as an integer.
Raises ValueError if config value can't be converted to int.
"""
return int(self.get(*args))
def set_integer(self, *args):
"""Sets integer to given UCI config.
"""
self.set(*(args[:-1]), str(args[-1]))
......@@ -475,7 +475,7 @@ static PyTypeObject uci_type = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flgs */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flgs */
"Top level entry point to uci context. Create instance of this for any uci access", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
......
......@@ -11,13 +11,14 @@ if 'COVERAGE' in os.environ:
setup(
name='pyuci',
version='0.3',
version='0.4',
author='CZ.NIC z.s.p.o',
author_email='karel.koci@nic.cz',
description='Python Uci bindings',
long_description='Python Unified Configuration Interface bimndings.',
license="MIT",
packages=['euci'],
ext_modules=[
Extension("uci", ["ucimodule.c", "pyuci.c", "pyhelper.c"],
libraries=["uci"], language="c",
......
# Copyright 2019, CZ.NIC z.s.p.o. (http://www.nic.cz/)
#
# This file is part of the PyUCI.
#
# PyUCI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyUCI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyUCI. If not, see <http://www.gnu.org/licenses/>.
import pytest
import euci
def test_boolean_get(tmpdir):
'Test get_boolean'
tmpdir.join('test').write("""
config integer 'integer'
option true '1'
option false '0'
config word 'word'
option true 'yes'
option false 'no'
config state 'state'
option true 'on'
option false 'off'
config bool 'bool'
option true 'true'
option false 'false'
config bled 'bled'
option true 'enabled'
option false 'disabled'
""")
u = euci.EUci(confdir=tmpdir.strpath)
assert u.get_boolean('test', 'integer', 'true')
assert u.get_boolean('test', 'word', 'true')
assert u.get_boolean('test', 'state', 'true')
assert u.get_boolean('test', 'bool', 'true')
assert u.get_boolean('test', 'bled', 'true')
assert not u.get_boolean('test', 'integer', 'false')
assert not u.get_boolean('test', 'word', 'false')
assert not u.get_boolean('test', 'state', 'false')
assert not u.get_boolean('test', 'bool', 'false')
assert not u.get_boolean('test', 'bled', 'false')
def test_boolean_set(tmpdir):
'Test set_boolean'
tmpdir.join('test').write("""
config testing 'testing'
""")
u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
u.set_boolean('test', 'testing', 'true', True)
u.set_boolean('test', 'testing', 'false', False)
assert u.get('test', 'testing', 'true') == '1'
assert u.get('test', 'testing', 'false') == '0'
def test_integer_get(tmpdir):
'Test get_integer'
tmpdir.join('test').write("""
config integer 'integer'
option plus '42'
option minus '-42'
""")
u = euci.EUci(confdir=tmpdir.strpath)
assert u.get_integer('test', 'integer', 'plus') == 42
assert u.get_integer('test', 'integer', 'minus') == -42
def test_integer_set(tmpdir):
'Test set_integer'
tmpdir.join('test').write("""
config testing 'testing'
""")
u = euci.EUci(savedir=tmpdir.mkdir('save').strpath, confdir=tmpdir.strpath)
u.set_integer('test', 'testing', 'plus', 42)
u.set_integer('test', 'testing', 'minus', -42)
assert u.get('test', 'testing', 'plus') == '42'
assert u.get('test', 'testing', 'minus') == '-42'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment