...
 
Commits (4)
......@@ -4,15 +4,7 @@ stages:
- test
- pages
python2:
stage: test
before_script:
- pip install .
- export LD_LIBRARY_PATH=/usr/local/lib
script:
- python -m pytest tests
python3:
unittests:
stage: test
before_script:
- pip3 install .
......@@ -34,3 +26,5 @@ pages:
when: on_success
paths:
- public
only:
- master
......@@ -22,7 +22,7 @@
# 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
from uci import Uci, UciExceptionNotFound
class EUci(Uci):
......@@ -46,16 +46,35 @@ class EUci(Uci):
def __init__(self, *args, **kwargs):
super(EUci, self).__init__(*args, **kwargs)
def get_boolean(self, *args):
def get(self, *args, default=None):
"""Override UCI get method with additional check for missing keys.
Raises UciExceptionNotFound if key cannot be found and default value is unset.
Returns default value if key cannot be found and default value is set.
"""
try:
return super().get(*args)
except UciExceptionNotFound:
if default is not None:
return default
raise
def get_boolean(self, *args, default=None):
"""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.
Raises UciExceptionNotFound if key cannot be found and default value is unset.
Returns default value if key cannot be found and default value is set.
"""
value = self.get(*args)
if value.lower() in self.__BOOLEAN_VALUES:
return self.__BOOLEAN_VALUES[value.lower()]
raise ValueError
try:
value = self.get(*args)
except UciExceptionNotFound:
if default is not None:
return default
raise
if value.lower() not in self.__BOOLEAN_VALUES:
raise ValueError
return self.__BOOLEAN_VALUES[value.lower()]
def set_boolean(self, *args):
"""Sets boolean value to given UCI config.
......@@ -64,11 +83,18 @@ class EUci(Uci):
nargs[-1] = self.__BOOLEAN_TRUE if nargs[-1] else self.__BOOLEAN_FALSE
self.set(*nargs)
def get_integer(self, *args):
def get_integer(self, *args, default=None):
"""Returns given UCI config as an integer.
Raises ValueError if config value can't be converted to int.
Raises UciExceptionNotFound if key cannot be found and default value is unset.
Returns default value if key cannot be found and default value is set.
"""
return int(self.get(*args))
try:
return int(self.get(*args))
except UciExceptionNotFound:
if default is not None:
return default
raise
def set_integer(self, *args):
"""Sets integer to given UCI config.
......
#include "pyhelper.h"
bool is_pystring(PyObject *pystr) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_Check(pystr);
#else
return PyString_Check(pystr);
#endif
}
const char *pystring(PyObject *pystr) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_AsUTF8(pystr);
#else
return PyString_AsString(pystr);
#endif
}
bool is_pytable(PyObject *pyobj) {
return PyList_Check(pyobj) || PyTuple_Check(pyobj);
}
......@@ -40,5 +24,5 @@ const char *pytable_string(PyObject *pyobj, int index) {
if (!str)
return NULL;
return pystring(str);
return PyUnicode_AsUTF8(str);
}
......@@ -22,10 +22,6 @@
#include <Python.h>
#include <stdbool.h>
// Stupid helper functions for getting string from python str object
bool is_pystring(PyObject *pystr);
const char *pystring(PyObject *pystr);
// Functions for accessing both Lists and Tuples.
bool is_pytable(PyObject *pyobj);
Py_ssize_t pytable_size(PyObject *pyobj);
......
......@@ -242,8 +242,8 @@ static PyObject *pyuci_set(uci_object *self, PyObject *args) {
if (uci_add_list(self->ctx, &ptr))
return pyuci_error(self, UciException);
}
} else if (is_pystring(data)) {
ptr.value = pystring(data);
} else if (PyUnicode_Check(data)) {
ptr.value = PyUnicode_AsUTF8(data);
if (!ptr.value)
return NULL;
if (uci_set(self->ctx, &ptr))
......
......@@ -25,9 +25,6 @@ static PyMethodDef module_methods[] = {
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"uci",
......@@ -37,34 +34,17 @@ static struct PyModuleDef moduledef = {
NULL, NULL, NULL, NULL
};
#define RETURN_ERROR return NULL
#else
#define RETURN_ERROR return
#endif
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_uci(void) {
#else
PyMODINIT_FUNC inituci(void) {
#endif
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule("uci", module_methods);
#endif
if (m == NULL)
RETURN_ERROR;
return NULL;
// Add Uci type to module
if (!pyuci_object_init(m))
RETURN_ERROR;
return NULL;
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}