diff options
author | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-11 18:28:07 +0200 |
---|---|---|
committer | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-11 18:28:07 +0200 |
commit | fa75018c1432d0aaaf4674b502fe6cafdfd5156c (patch) | |
tree | 9eaa84d075157700f563bbcac11637c3e5323fc2 /src/config.c | |
parent | 74ee4280886f8adc28b16404c469f63f6aa83e55 (diff) | |
download | layman-fa75018c1432d0aaaf4674b502fe6cafdfd5156c.tar.gz layman-fa75018c1432d0aaaf4674b502fe6cafdfd5156c.tar.bz2 layman-fa75018c1432d0aaaf4674b502fe6cafdfd5156c.zip |
Get the proper layman Python library and fix BareConfig constructor to
take a Message class as first argument.
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 67 |
1 files changed, 53 insertions, 14 deletions
diff --git a/src/config.c b/src/config.c index db632f2..624f1ab 100644 --- a/src/config.c +++ b/src/config.c @@ -1,33 +1,72 @@ +#include <Python.h> + #include "config.h" #include "interpreter.h" -#include <Python.h> -struct Config +struct BareConfig { PyObject *object; }; -PyObject *_object(Config *c) +PyObject *_bareConfigObject(BareConfig *c) { - return c ? c->object : NULL; + if (c && c->object) + return c->object; + else + Py_RETURN_NONE; } -Config *createConfig(const char *argv[], int argc) +BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) { - PyObject *pyargs = PyList_New(argc); - for (int i = 0; i < argc; i++) - { - PyObject *arg = PyBytes_FromString(argv[i]); - PyList_Insert(pyargs, i, arg); - } + PyObject *pyout = PyFile_FromFile(((!outFd || fileno(outFd) <= 0) ? stdout : outFd), + "", "w", 0); + PyObject *pyin = PyFile_FromFile(((!inFd || fileno(inFd) <= 0) ? stdin : inFd), + "", "r", 0); + PyObject *pyerr = PyFile_FromFile(((!errFd || fileno(errFd) <= 0) ? stderr : errFd), + "", "w", 0); + + PyObject *obj = executeFunction("layman.config", "BareConfig", "OOOO", _messageObject(m), pyout, pyin, pyerr); + Py_DECREF(pyout); + Py_DECREF(pyin); + Py_DECREF(pyerr); - PyObject *obj = executeFunction("layman.config", "Config", "O", pyargs); - Py_DECREF(pyargs); if (!obj) return NULL; - Config *ret = malloc(sizeof(Config)); + BareConfig *ret = malloc(sizeof(BareConfig)); ret->object = obj; return ret; } + +void bareConfigFree(BareConfig* cfg) +{ + if (cfg && cfg->object) + { + Py_DECREF(cfg->object); + } + + if (cfg) + free(cfg); +} + +const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt) +{ + PyObject *obj = PyObject_CallMethod(cfg->object, "get_defaults", NULL); + if (!obj) + return NULL; + + if (PyDict_Contains(obj, PyBytes_FromString(opt))) + return PyBytes_AsString(PyDict_GetItem(obj, PyBytes_FromString(opt))); + else + return ""; +} + +int bareConfigSetOptionValue(BareConfig* cfg, const char* opt, const char* val) +{ + PyObject *obj = PyObject_CallMethod(cfg->object, "set_option", "(zz)", opt, val); + if (obj) + return 1; + else + return 0; +} |