diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/interpreter.c | 146 | ||||
-rw-r--r-- | src/interpreter.h | 10 | ||||
-rw-r--r-- | src/overlay.c | 102 | ||||
-rw-r--r-- | src/overlay.h | 14 | ||||
-rw-r--r-- | src/tester.c | 21 |
5 files changed, 167 insertions, 126 deletions
diff --git a/src/interpreter.c b/src/interpreter.c index 3653e4f..e68f1a2 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -82,36 +82,41 @@ void freeList(PyObjectList *list, int deref) * A Python interpreter object keeps the context like the loaded modules. */ -typedef struct Interpreter +struct Interpreter { PyObjectList *modules; -} Interpreter; +} *in = 0; -Interpreter *createInterpreter() +//Interpreter *in = 0; + +void interpreterInit() { - Interpreter *ret = malloc(sizeof(Interpreter)); - ret->modules = createObjectList(); - return ret; + if (in) + return; + + if (!Py_IsInitialized()) + Py_Initialize(); + + in = malloc(sizeof(struct Interpreter)); + in->modules = createObjectList(); } -void freeInterpreter(Interpreter *inter) +void interpreterFinalize() { - if (!inter) + if (!in) return; - freeList(inter->modules, 1); - free(inter); + freeList(in->modules, 1); + free(in); - Py_Finalize(); + if (Py_IsInitialized()) + Py_Finalize(); } -Interpreter *in = 0; - /* * printf() like function that executes a python function - * @param interpreter Python interpreter object on which the function should be ran * @param module name of the python module in which the function is * @param funcName the function name to call - * @param arg_types printf() like list of arguments. See Python documentation + * @param format printf() like list of arguments. See Python documentation * @param ... arguments for the function */ @@ -172,114 +177,3 @@ PyObject *executeFunction(const char *module, const char *funcName, const char* return val; } - -typedef struct Overlay Overlay; - -struct Overlay -{ - PyObject *object; -}; - -/* - * FIXME: should the xml argument be an expat element ? - */ -Overlay *createOverlay(const char *xml, const char *config, int ignore, int quiet) -{ - //First argument must be a xml.etree.Element - //PyObject *elem = executeFunction("layman.overlays.overlay", "testfunc", NULL); - PyObject *elem = executeFunction("xml.etree.ElementTree", "fromstring", "(s)", xml); - if (!elem) - return NULL; - - config = config; - PyObject *cfg = PyDict_New(); - if (!cfg) - return NULL; - - PyObject *overlay = executeFunction("layman.overlays.overlay", "Overlay", "(OOIb)", elem, cfg, ignore, quiet); - if (!overlay) - return NULL; - Overlay *ret = malloc(sizeof(Overlay)); - ret->object = overlay; - - return ret; -} - -const char *overlayName(Overlay *o) -{ - if (!o || !o->object) - return NULL; - - PyObject *name = PyObject_GetAttrString(o->object, "name"); - - //TODO:Py_DECREF me ! - - return PyBytes_AsString(name); -} - -const char *overlayOwnerEmail(Overlay *o) -{ - if (!o || !o->object) - return NULL; - - PyObject *ret = PyObject_GetAttrString(o->object, "owner_email"); - - //TODO:Py_DECREF me ! - - return PyBytes_AsString(ret); -} - -int overlayPriority(Overlay *o) -{ - if (!o || !o->object) - return -1; - - PyObject *prio = PyObject_GetAttrString(o->object, "priority"); - - //TODO:Py_DECREF me ! - - return (int) PyLong_AsLong(prio); -} - -const char *overlayDescription(Overlay *o) -{ - if (!o || !o->object) - return NULL; - - PyObject *desc = PyObject_GetAttrString(o->object, "description"); - - //TODO:Py_DECREF me ! - - return PyBytes_AsString(desc); -} - -int overlayIsOfficial(Overlay *o) -{ - if (!o || !o->object) - return -1; - - PyObject *iso = PyObject_CallMethod(o->object, "is_official", NULL); - - //TODO:Py_DECREF me ! - - return (int) PyLong_AsLong(iso); -} - -int main(int argc, char *argv[]) -{ - in = createInterpreter(); - - Overlay *o = createOverlay("<overlay type='svn' src='https://overlays.gentoo.org/svn/dev/wrobel' contact='nobody@gentoo.org' name='wrobel' status='official' priorit='10'><description>Test</description></overlay>", "", 1, 0); - - if (!o) - { - printf("Error creating overlay.\n"); - return 0; - } - - printf("Overlay name = %s, owner email : %s, description : %s, priority : %d, it is %sofficial.\n", overlayName(o), overlayOwnerEmail(o), overlayDescription(o), overlayPriority(o), overlayIsOfficial(o) ? "" : "not "); - - freeInterpreter(in); - - return 0; -} diff --git a/src/interpreter.h b/src/interpreter.h new file mode 100644 index 0000000..d21ad44 --- /dev/null +++ b/src/interpreter.h @@ -0,0 +1,10 @@ +#ifndef INTERPRETER_H +#define INTERPRETER_H + +#include <Python.h> + +void interpreterInit(); +void interpreterFinalize(); +PyObject* executeFunction(const char *module, const char *funcName, const char* format, ...); + +#endif diff --git a/src/overlay.c b/src/overlay.c new file mode 100644 index 0000000..79fee08 --- /dev/null +++ b/src/overlay.c @@ -0,0 +1,102 @@ +#include <Python.h> + +#include "interpreter.h" +#include "overlay.h" + +struct Overlay +{ + PyObject *object; +}; + +/* + * FIXME: should the xml argument be an expat element ? + */ +Overlay *createOverlay(const char *xml, const char *config, int ignore, int quiet) +{ + //First argument must be a xml.etree.Element + //PyObject *elem = executeFunction("layman.overlays.overlay", "testfunc", NULL); + PyObject *elem = executeFunction("xml.etree.ElementTree", "fromstring", "(s)", xml); + if (!elem) + return NULL; + + config = config; + PyObject *cfg = PyDict_New(); + if (!cfg) + return NULL; + + PyObject *overlay = executeFunction("layman.overlays.overlay", "Overlay", "(OOIb)", elem, cfg, ignore, quiet); + if (!overlay) + return NULL; + Overlay *ret = malloc(sizeof(Overlay)); + ret->object = overlay; + + return ret; +} + +const char *overlayName(Overlay *o) +{ + if (!o || !o->object) + return NULL; + + PyObject *name = PyObject_GetAttrString(o->object, "name"); + + //TODO:Py_DECREF me ! + + return PyBytes_AsString(name); +} + +const char *overlayOwnerEmail(Overlay *o) +{ + if (!o || !o->object) + return NULL; + + PyObject *ret = PyObject_GetAttrString(o->object, "owner_email"); + + //TODO:Py_DECREF me ! + + return PyBytes_AsString(ret); +} + +int overlayPriority(Overlay *o) +{ + if (!o || !o->object) + return -1; + + PyObject *prio = PyObject_GetAttrString(o->object, "priority"); + + //TODO:Py_DECREF me ! + + return (int) PyLong_AsLong(prio); +} + +const char *overlayDescription(Overlay *o) +{ + if (!o || !o->object) + return NULL; + + PyObject *desc = PyObject_GetAttrString(o->object, "description"); + + //TODO:Py_DECREF me ! + + return PyBytes_AsString(desc); +} + +int overlayIsOfficial(Overlay *o) +{ + if (!o || !o->object) + return -1; + + PyObject *iso = PyObject_CallMethod(o->object, "is_official", NULL); + + //TODO:Py_DECREF me ! + + return (int) PyLong_AsLong(iso); +} + +void overlayFree(Overlay *o) +{ + if (o && o->object) + Py_DECREF(o->object); + if (o) + free(o); +} diff --git a/src/overlay.h b/src/overlay.h new file mode 100644 index 0000000..9125de0 --- /dev/null +++ b/src/overlay.h @@ -0,0 +1,14 @@ +#ifndef OVERLAY_H +#define OVERLAY_H + +typedef struct Overlay Overlay; + +Overlay* createOverlay(const char*, const char*, int, int); +const char* overlayName(Overlay*); +const char* overlayOwnerEmail(Overlay*); +int overlayPriority(Overlay*); +const char* overlayDescription(Overlay*); +int overlayIsOfficial(Overlay*); +void overlayFree(Overlay*); + +#endif diff --git a/src/tester.c b/src/tester.c new file mode 100644 index 0000000..53e4776 --- /dev/null +++ b/src/tester.c @@ -0,0 +1,21 @@ +#include "overlay.h" +#include "interpreter.h" + +int main(int argc, char *argv[]) +{ + interpreterInit(); + + Overlay *o = createOverlay("<overlay type='svn' src='https://overlays.gentoo.org/svn/dev/wrobel' contact='nobody@gentoo.org' name='wrobel' status='official' priorit='10'><description>Test</description></overlay>", "", 1, 0); + + if (!o) + { + printf("Error creating overlay.\n"); + return 0; + } + + printf("Overlay name = %s, owner email : %s, description : %s, priority : %d, it is %sofficial.\n", overlayName(o), overlayOwnerEmail(o), overlayDescription(o), overlayPriority(o), overlayIsOfficial(o) ? "" : "not "); + + interpreterFinalize(); + + return 0; +} |