From c90d082b6cae76e47865a0b4088fb6d34db1cd29 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 25 Jan 2013 21:20:40 +0100 Subject: [PATCH] Added an early binding for the Config API (incomplete, untested) --- src/python/libSystem.c | 80 +++++++++++++++++++++++++++++++++++++++-- src/python/libSystem.py | 11 ++++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/python/libSystem.c b/src/python/libSystem.c index 8613b4c..6b9eb16 100644 --- a/src/python/libSystem.c +++ b/src/python/libSystem.c @@ -22,21 +22,33 @@ /* libSystem */ /* private */ /* constants */ +static char const _libsystem_config_name[] = "libSystem::Config"; static char const _libsystem_event_name[] = "libSystem::Event"; /* prototypes */ +/* Config */ +static PyObject * _libsystem_config_new(PyObject * self, PyObject * args); +static void _libsystem_config_delete(PyObject * self); + +static PyObject * _libsystem_config_get(PyObject * self, PyObject * args); +static PyObject * _libsystem_config_set(PyObject * self, PyObject * args); + /* Event */ static PyObject * _libsystem_event_new(PyObject * self, PyObject * args); static void _libsystem_event_delete(PyObject * self); - -/* useful */ static PyObject * _libsystem_event_loop(PyObject * self, PyObject * args); /* variables */ static PyMethodDef _libsystem_methods[] = { + { "config_new", _libsystem_config_new, METH_VARARGS, + "Instantiates a Config object." }, + { "config_get", _libsystem_config_get, METH_VARARGS, + "Obtains a value from the Config object." }, + { "config_set", _libsystem_config_set, METH_VARARGS, + "Sets a value in the Config object." }, { "event_new", _libsystem_event_new, METH_VARARGS, "Instantiates an Event object." }, { "event_loop", _libsystem_event_loop, METH_VARARGS, @@ -60,6 +72,70 @@ PyMODINIT_FUNC init_libSystem(void) /* private */ /* functions */ +/* Config */ +/* libsystem_config_new */ +static PyObject * _libsystem_config_new(PyObject * self, PyObject * args) +{ + Config * config; + + if(!PyArg_ParseTuple(args, "")) + return NULL; + if((config = config_new()) == NULL) + return NULL; + return PyCapsule_New(config, _libsystem_config_name, + _libsystem_config_delete); +} + + +/* libsystem_config_delete */ +static void _libsystem_config_delete(PyObject * self) +{ + Config * config; + + if((config = PyCapsule_GetPointer(self, _libsystem_config_name)) + == NULL) + return; + config_delete(config); +} + + +/* libsystem_config_get */ +static PyObject * _libsystem_config_get(PyObject * self, PyObject * args) +{ + Config * config; + char const * ret; + char const * section; + char const * variable; + + if((config = PyCapsule_GetPointer(self, _libsystem_config_name)) + == NULL) + return NULL; + if(!PyArg_ParseTuple(args, "ss", §ion, &variable)) + return NULL; + ret = config_get(config, section, variable); + return Py_BuildValue("s", ret); +} + + +/* libsystem_config_set */ +static PyObject * _libsystem_config_set(PyObject * self, PyObject * args) +{ + Config * config; + int ret; + char const * section; + char const * variable; + char const * value; + + if((config = PyCapsule_GetPointer(self, _libsystem_config_name)) + == NULL) + return NULL; + if(!PyArg_ParseTuple(args, "sss", §ion, &variable, &value)) + return NULL; + ret = config_set(config, section, variable, value); + return Py_BuildValue("i", ret); +} + + /* Event */ /* libsystem_event_new */ static PyObject * _libsystem_event_new(PyObject * self, PyObject * args) diff --git a/src/python/libSystem.py b/src/python/libSystem.py index a47116d..cf3e919 100644 --- a/src/python/libSystem.py +++ b/src/python/libSystem.py @@ -19,6 +19,17 @@ import _libSystem +#Config +class Config: + def __init__(self): + self.config = _libSystem.config_new() + + def get(self, section, variable): + return _libSystem.config_get(self.config, section, variable) + + def set(self, section, variable, value): + return _libSystem.config_set(self.config, section, variable, + value) #Event class Event: def __init__(self):