From 74ee4280886f8adc28b16404c469f63f6aa83e55 Mon Sep 17 00:00:00 2001 From: Detlev Casanova Date: Sat, 10 Jul 2010 18:09:17 +0200 Subject: Add LayamnAPI, Message, Config, StringList, update Config. --- src/laymanapi.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/laymanapi.h | 47 +++++++++++++++++++++++ src/message.c | 71 ++++++++++++++++++++++++++++++++++ src/message.h | 15 ++++++++ src/stringlist.c | 59 ++++++++++++++++++++++++++++ src/stringlist.h | 12 ++++++ 6 files changed, 319 insertions(+) create mode 100644 src/laymanapi.c create mode 100644 src/laymanapi.h create mode 100644 src/message.c create mode 100644 src/message.h create mode 100644 src/stringlist.c create mode 100644 src/stringlist.h diff --git a/src/laymanapi.c b/src/laymanapi.c new file mode 100644 index 0000000..ae4b2eb --- /dev/null +++ b/src/laymanapi.c @@ -0,0 +1,115 @@ +#include +#include "interpreter.h" +#include "laymanapi.h" + +struct LaymanAPI +{ + PyObject *object; +}; + +OverlayInfo strToInfo(const char* str) +{ + OverlayInfo ret; + return ret; +} + +LaymanAPI* laymanAPICreate(Config* config, int report_error, int output) +{ + PyObject *obj = executeFunction("layman.api", "LaymanAPI", "OII", _configObject(config), report_error, output); + if (!obj) + return NULL; + + LaymanAPI *ret = malloc(sizeof(LaymanAPI)); + ret->object = obj; + + return ret; +} + +StringList* laymanAPIGetAvailable(LaymanAPI* l) +{ + if (!l || !l->object) + return NULL; + + PyObject *obj = PyObject_CallMethod(l->object, "get_available", NULL); + StringList* ret = listToCList(obj); + Py_DECREF(obj); + + return ret; +} + +StringList* laymanAPIGetInstalled(LaymanAPI* l) +{ + if (!l || !l->object) + return NULL; + + PyObject *obj = PyObject_CallMethod(l->object, "get_installed", NULL); + StringList* ret = listToCList(obj); + Py_DECREF(obj); + + return ret; +} + +int laymanAPISync(LaymanAPI* l, const char* overlay) +{ + if (!l || !l->object) + return EXIT_FAILURE; + + PyObject *list = PyList_New(1); + PyList_Insert(list, 0, PyBytes_FromString(overlay)); + + PyObject *obj = PyObject_CallMethod(l->object, "sync", "O", list); + Py_DECREF(list); + + if (!obj) + return EXIT_FAILURE; + + PyObject *success = PyList_GetItem(obj, 1); + if (success == Py_None) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + +int laymanAPIFetchRemoteList(LaymanAPI* l) +{ + if (!l || !l->object) + return EXIT_FAILURE; + + PyObject *obj = PyObject_CallMethod(l->object, "fetch_remote_list", NULL); + + int ret; + + if (PyObject_IsTrue(obj)) + ret = EXIT_SUCCESS; + else + ret = EXIT_FAILURE; + + Py_DECREF(obj); + + return ret; +} + +const char* laymanAPIGetInfo(LaymanAPI* l, const char* overlay) +{ + if (!l || !l->object) + return NULL; + + PyObject *list = PyList_New(1); + PyList_Insert(list, 0, PyBytes_FromString(overlay)); + + PyObject *obj = PyObject_CallMethod(l->object, "get_info", "O", list); + Py_DECREF(list); + + if (!obj) + return NULL; + + PyObject *result = PyList_GetItem(obj, 0); + char* tmp = PyBytes_AsString(result); + char* ret = malloc((strlen(tmp) + 1) * sizeof(char)); + strcpy(ret, tmp); + Py_DECREF(result); + + return ret; + //TODO:also return 'official' an 'supported' (see laymanapi.h) +} + diff --git a/src/laymanapi.h b/src/laymanapi.h new file mode 100644 index 0000000..42c0058 --- /dev/null +++ b/src/laymanapi.h @@ -0,0 +1,47 @@ +#ifndef LAYMAN_API_H +#define LAYMAN_API_H + +#include "config.h" +#include "stringlist.h" + +typedef struct LaymanAPI LaymanAPI; + +typedef enum OverlayType {Svn = 0, Git, Bzr} OverlayType; +typedef enum OverlayQuality {Experimental = 0, Stable, Testing} OverlayQuality; +typedef struct OverlayInfo +{ + char *name; + char *source; + char *contact; + OverlayType type; + int priority; + OverlayQuality quality; + char *description; + char *link; + char *feed; + int official; + int supported; +} OverlayInfo; + + +LaymanAPI* laymanAPICreate(Config*, int, int); +StringList* laymanAPIGetAvailable(LaymanAPI*); +StringList* laymanAPIGetInstalled(LaymanAPI*); + +/* + * The Python API returns a list of warnings/sucesses/errors + * In here, a boolean value is returned. + * Warnings can be retreived with + * laymanAPIWarnings() + * laymanAPIErrors() + * As there's only one argument here, there's need to have success results. + * + * The reason it's done this way is that the Python way of doing things is not the same as the Python way. + * + * FIXME:is it a good idea to have different APIs for different languages ? + */ +int laymanAPISync(LaymanAPI*, const char*); +int laymanAPIFetchRemoteList(LaymanAPI*); +const char* laymanAPIGetInfo(LaymanAPI*, const char*); + +#endif diff --git a/src/message.c b/src/message.c new file mode 100644 index 0000000..bc0ee56 --- /dev/null +++ b/src/message.c @@ -0,0 +1,71 @@ +#include +#include "message.h" +#include "interpreter.h" + +struct Message +{ + PyObject *object; +}; + +Message *messageCreate(const char* module, + FILE* out, + FILE* err, + FILE* dbg, + int debug_level, + int debug_verbosity, + int info_level, + int warn_level, + int col, + StringList* mth, + StringList* obj, + StringList* var) +{ + PyObject *pyout, *pyerr, *pydbg, *pymth, *pyobj, *pyvar; + + pyout = PyFile_FromFile((fileno(out) <= 0 ? stdout : out), + NULL, "w", 0); + pyerr = PyFile_FromFile((fileno(err) <= 0 ? stderr : err), + NULL, "w", 0); + pydbg = PyFile_FromFile((fileno(dbg) <= 0 ? stderr : dbg), + NULL, "w", 0); + + + pymth = cListToPyList(mth); + pyobj = cListToPyList(obj); + pyvar = cListToPyList(var); + + PyObject *object = executeFunction("layman.debug", "Message", + "(sOOOIIIIIOOO)", + module, + pyout, + pyerr, + pydbg, + debug_level, + debug_verbosity, + info_level, + warn_level, + col, + pymth, + pyobj, + pyvar); + + Py_DECREF(pyout); + Py_DECREF(pyerr); + Py_DECREF(pydbg); + Py_DECREF(pymth); + Py_DECREF(pyobj); + Py_DECREF(pyvar); + + if (!object) + return NULL; + + Message *ret = malloc(sizeof(Message)); + ret->object = object; + + return ret; +} + +PyObject *_messageObject(Message* m) +{ + return m ? m->object : NULL; +} diff --git a/src/message.h b/src/message.h new file mode 100644 index 0000000..15f3e15 --- /dev/null +++ b/src/message.h @@ -0,0 +1,15 @@ +#ifndef MESSAGE_H +#define MESSAGE_H + +#include +#include "stringlist.h" + +typedef struct Message Message; + +/* + * arguments : module (String), stdout (fd), stderr (fd), stderr (fd), debug_level, debug_verbosity, info_level, warn_level, ?, ?, ?, ? + */ +Message *messageCreate(const char*, FILE*, FILE*, FILE*, int, int, int, int, int, StringList*, StringList*, StringList*); +PyObject *_messageObject(Message* m); + +#endif diff --git a/src/stringlist.c b/src/stringlist.c new file mode 100644 index 0000000..dc2a45d --- /dev/null +++ b/src/stringlist.c @@ -0,0 +1,59 @@ +#include "stringlist.h" + +struct StringList +{ + char **list; + int count; +}; + +StringList* stringListCreate(size_t count) +{ + return NULL; +} + +StringList* listToCList(PyObject* list) +{ + if (!list || !PyList_Check(list)) + return NULL; + + int len = PyList_Size(list); + StringList *ret = malloc(sizeof(StringList)); + ret->count = len; + ret->list = malloc(sizeof(char*) * len); + + for (int i = 0; i < len; i++) + { + PyObject *elem = PyList_GetItem(list, i); + ret->list[i] = malloc(sizeof(char) * (PyBytes_Size(elem) + 1)); + strcpy(ret->list[i], PyBytes_AsString(elem)); + } + + return ret; +} + +PyObject* cListToPyList(StringList* list) +{ + if (!list) + return NULL; + + PyObject *ret = PyList_New(list->count); + for(int i = 0; i < list->count; i++) + { + PyList_Append(ret, PyBytes_FromString(list->list[i])); + } + + return ret; +} + +void stringListFree(StringList* list) +{ + if (!list) + return; + + for(int i = 0; i < list->count; i++) + { + free(list->list[i]); + } + + free(list); +} diff --git a/src/stringlist.h b/src/stringlist.h new file mode 100644 index 0000000..cf1c33a --- /dev/null +++ b/src/stringlist.h @@ -0,0 +1,12 @@ +#ifndef STRINGLIST_H +#define STRINGLIST_H + +#include + +typedef struct StringList StringList; + +StringList* stringListCreate(size_t); +StringList* listToCList(PyObject* list); +PyObject* cListToPyList(StringList*); + +#endif -- cgit v1.2.3-1-g7c22