From 6459c9b9e7bf16e235b8c5b151f97a224aebb108 Mon Sep 17 00:00:00 2001 From: Detlev Casanova Date: Fri, 9 Jul 2010 11:18:24 +0200 Subject: Tests the layman's overlays API --- src/interpreter.c | 153 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 110 insertions(+), 43 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index 5d5bca5..3653e4f 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -104,6 +104,8 @@ void freeInterpreter(Interpreter *inter) 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 @@ -111,18 +113,17 @@ void freeInterpreter(Interpreter *inter) * @param funcName the function name to call * @param arg_types printf() like list of arguments. See Python documentation * @param ... arguments for the function - * FIXME:why are default arguments necessary ? */ -PyObject *executeFunction(Interpreter *interpreter, const char *module, const char *funcName, const char* format, ...) +PyObject *executeFunction(const char *module, const char *funcName, const char* format, ...) { if (!Py_IsInitialized()) Py_Initialize(); // Make argument list PyObject *args; - if (format == NULL) - args = Py_None; + if (format == NULL || strcmp(format, "") == 0) + args = PyTuple_New(0); else { va_list listArgs; @@ -133,18 +134,20 @@ PyObject *executeFunction(Interpreter *interpreter, const char *module, const ch // Look for the module. PyObject *mod = 0; - if (interpreter->modules) + if (in->modules) { - mod = moduleNamed(module, interpreter->modules); + mod = moduleNamed(module, in->modules); } if (!mod) { mod = PyImport_ImportModule(module); if (!mod) return NULL; - insert(interpreter->modules, mod); + insert(in->modules, mod); } + //printf("Using module named %s\n", PyModule_GetName(mod)); + /*printf("mod: %p ", mod); PyObject_Print(mod, stdout, 0); printf("\n");*/ @@ -159,58 +162,122 @@ PyObject *executeFunction(Interpreter *interpreter, const char *module, const ch PyObject_Print(func, stdout, 0); printf("\n");*/ - PyObject *val = PyObject_Call(func, args, NULL); + //PyObject_Print(args, stdout, 0); + //printf("\n"); - // Add return value object in a local list so it can be DECREF'ed when the interpreter is deleted. - // TODO - Py_DECREF(args); + PyObject *val = PyObject_CallObject(func, args); + + if (args != NULL) + Py_DECREF(args); return val; } + +typedef struct Overlay Overlay; + +struct Overlay +{ + PyObject *object; +}; + /* -PyObject *executeMethod(PyObject *object, const char *methName, const char* format, ...) + * FIXME: should the xml argument be an expat element ? + */ +Overlay *createOverlay(const char *xml, const char *config, int ignore, int quiet) { - if (!Py_IsInitialized()) - Py_Initialize(); + //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; - // Make argument list - PyObject *args; - if (format == NULL) - args = Py_None; - else - { - va_list listArgs; - va_start(listArgs, format); + config = config; + PyObject *cfg = PyDict_New(); + if (!cfg) + return NULL; - args = Py_VaBuildValue(format, listArgs); - } + PyObject *overlay = executeFunction("layman.overlays.overlay", "Overlay", "(OOIb)", elem, cfg, ignore, quiet); + if (!overlay) + return NULL; + Overlay *ret = malloc(sizeof(Overlay)); + ret->object = overlay; - PyObject *ret = PyObject_CallMethod(object, methName, ) -}*/ + return ret; +} -int main(int argc, char *argv[]) +const char *overlayName(Overlay *o) { - Interpreter *in = createInterpreter(); + if (!o || !o->object) + return NULL; - executeFunction(in, "portage.data", "portage_group_warning", NULL); + PyObject *name = PyObject_GetAttrString(o->object, "name"); - PyObject *arg1 = executeFunction(in, "portage", "pkgsplit", "sI", "app-portage/kuroo4-4.2", 1); - PyObject *arg2 = executeFunction(in, "portage", "pkgsplit", "sI", "app-portage/kuroo4-4.1", 1); - PyObject *ret = executeFunction(in, "portage", "pkgcmp", "OO", arg1, arg2); + //TODO:Py_DECREF me ! - if (!ret) - printf("failed :-( \n"); - else - printf("Return %ld\n", PyLong_AsLong(ret)); + 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"); - Py_DECREF(ret); - Py_DECREF(arg1); - Py_DECREF(arg2); + //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("Test", "", 1, 0); + + if (!o) + { + printf("Error creating overlay.\n"); + return 0; + } - PyObject *tbz = executeFunction(in, "portage.output", "ProgressBar", "sIs", "titre", 100, "status"); - ret = PyObject_CallMethod(tbz, "inc", "I", 25); - Py_DECREF(tbz); - Py_DECREF(ret); + 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); -- cgit v1.2.3-1-g7c22