summaryrefslogtreecommitdiffstats
path: root/src/interpreter.c
diff options
context:
space:
mode:
authorDetlev Casanova <detlev.casanova@gmail.com>2010-07-09 12:16:50 +0200
committerDetlev Casanova <detlev.casanova@gmail.com>2010-07-09 12:16:50 +0200
commit97d75ee996220405a15f134924ac380262ec46f7 (patch)
treec870fc41d749b90cd115499c70a6522790baf3d7 /src/interpreter.c
parent6459c9b9e7bf16e235b8c5b151f97a224aebb108 (diff)
downloadlayman-97d75ee996220405a15f134924ac380262ec46f7.tar.gz
layman-97d75ee996220405a15f134924ac380262ec46f7.tar.bz2
layman-97d75ee996220405a15f134924ac380262ec46f7.zip
Restructure classes.
Diffstat (limited to 'src/interpreter.c')
-rw-r--r--src/interpreter.c146
1 files changed, 20 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;
-}