summaryrefslogtreecommitdiffstats
path: root/src/laymanapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/laymanapi.c')
-rw-r--r--src/laymanapi.c115
1 files changed, 115 insertions, 0 deletions
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 <Python.h>
+#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)
+}
+