summaryrefslogtreecommitdiffstats
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c67
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;
+}