diff options
author | dol-sen <brian.dolbec@gmail.com> | 2011-10-26 16:19:06 -0700 |
---|---|---|
committer | dol-sen <brian.dolbec@gmail.com> | 2011-10-26 16:19:06 -0700 |
commit | c40874bd95d1525961692dd7d742ed565a2b4334 (patch) | |
tree | d774c213a57cb2ed06d74d664a86ccbad4bf4a5b /c-layman/src/config.c | |
parent | 9a74747e60f08a6f303d36d45ad9bb2ba828af42 (diff) | |
parent | bce27850d7e2d86e1b8046be8eb1d51c83dbdd2c (diff) | |
download | layman-c40874bd95d1525961692dd7d742ed565a2b4334.tar.gz layman-c40874bd95d1525961692dd7d742ed565a2b4334.tar.bz2 layman-c40874bd95d1525961692dd7d742ed565a2b4334.zip |
Merge branch 'master' of git+ssh://git.overlays.gentoo.org/proj/layman
Diffstat (limited to 'c-layman/src/config.c')
-rw-r--r-- | c-layman/src/config.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/c-layman/src/config.c b/c-layman/src/config.c new file mode 100644 index 0000000..c578d90 --- /dev/null +++ b/c-layman/src/config.c @@ -0,0 +1,164 @@ +#include <Python.h> + +#include "config.h" +#include "internal.h" + +/** + * \internal + */ +#define debug(x) printf(x) + +/** \defgroup config Config + * \brief Layman library configuration module + */ + +/** \addtogroup config + * @{ + */ + + +/** + * \internal + */ +struct BareConfig +{ + PyObject *object; +}; + +/** + * \internal + * Returns the internal Python object. + */ +PyObject *_bareConfigObject(BareConfig *c) +{ + if (c && c->object) + return c->object; + else + Py_RETURN_NONE; +} + +/** + * Creates a bare config object with default values. + * + * \param outFd where information must be written to + * \param inFd where information must be read from + * \param errFd where errors must be written to + * + * \return a new instance of a BareConfig object. It must be freed with bareConfigFree() + */ +BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) +{ + if (!outFd || fileno(outFd) <= 0) + outFd = stdout; + if (!inFd || fileno(inFd) <= 0) + inFd = stdin; + if (!errFd || fileno(errFd) <= 0) + errFd = stderr; + + PyObject *pyout = PyFile_FromFile(outFd, "", "w", 0); + PyObject *pyin = PyFile_FromFile(inFd, "", "r", 0); + PyObject *pyerr = PyFile_FromFile(errFd, "", "w", 0); + + PyObject *obj = executeFunction("layman.config", "BareConfig", "OOOO", _messageObject(m), pyout, pyin, pyerr); + Py_DECREF(pyout); + Py_DECREF(pyin); + Py_DECREF(pyerr); + + if (!obj) + { + debug("The execution failed, Are you sure app-portage/layman-8888 is properly installed ?\n"); + return NULL; + } + + BareConfig *ret = malloc(sizeof(BareConfig)); + ret->object = obj; + + return ret; +} + +/** + * Frees a BareConfig object. + */ +void bareConfigFree(BareConfig* cfg) +{ + if (cfg && cfg->object) + { + Py_DECREF(cfg->object); + } + + if (cfg) + free(cfg); +} + +/** + * Get an option's default value. + * + * \param opt the name of the option + * + * \return the value or NULL on failure. + */ +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, PyString_FromString(opt))) + { + PyObject *pyopt = PyString_FromString(opt); + char *tmp = PyString_AsString(PyDict_GetItem(obj, pyopt)); + Py_DECREF(pyopt); + + char *ret = malloc(sizeof(char) * strlen(tmp)); + strcpy(ret, tmp); + Py_DECREF(obj); + + return ret; + } + else + return ""; +} + +/* + * Get an option's current value. + * + * \param opt the name of the option + * + * \return the value or NULL on failure + */ +const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt) +{ + PyObject *obj = PyObject_CallMethod(cfg->object, "get_option", "(z)", opt); + char *tmp = PyString_AsString(obj); + char *ret = malloc(sizeof(char) * (strlen(tmp) + 1)); + + strcpy(ret, tmp); + + Py_DECREF(obj); + + return ret; +} + +/* + * Modifies an option's value + * + * \param opt the name of the option + * \param val the new value for this option + * + * \return True on success, 0 on failure + */ +int bareConfigSetOptionValue(BareConfig* cfg, const char* opt, const char* val) +{ + PyObject *obj = PyObject_CallMethod(cfg->object, "set_option", "(zz)", opt, val); + int ret; + if (obj) + ret = 1; + else + ret = 0; + + Py_DECREF(obj); + + return ret; +} + +/** @} */ |