1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00
profanity/src/api/api.c

140 lines
4.0 KiB
C
Raw Normal View History

/*
* api.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Python.h>
#include "ui/ui.h"
2013-08-02 18:36:47 -04:00
static GSList* _get_module_names(void);
2013-07-24 19:54:31 -04:00
// API
static PyObject*
api_cons_show(PyObject *self, PyObject *args)
{
2013-07-24 20:13:41 -04:00
const char *message = NULL;
if (!PyArg_ParseTuple(args, "s", &message)) {
return NULL;
}
cons_show("%s", message);
2013-08-02 18:36:47 -04:00
return Py_BuildValue("");
2013-07-24 19:54:31 -04:00
}
static PyMethodDef apiMethods[] = {
{ "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." },
{ NULL, NULL, 0, NULL }
};
2013-08-02 18:36:47 -04:00
//PyMODINIT_FUNC
//initprof(void)
//{
// Py_InitModule("prof", apiMethods);
//}
void
api_init(void)
{
2013-08-02 18:36:47 -04:00
//PyObject *p_module_name
PyObject *p_module, *p_prof_init, *p_args, *p_prof_on_start;
// PyObject *p_prof_module;
GSList *module_names = _get_module_names();
Py_Initialize();
2013-07-24 19:54:31 -04:00
Py_InitModule("prof", apiMethods);
2013-08-02 18:36:47 -04:00
// p_prof_module = Py_InitModule("prof", apiMethods);
// PyObject *p_prof_module_name = PyString_FromString("prof");
// PyObject *main_module = PyImport_ImportModule("__main__");
// PyObject *main_dict = PyModule_GetDict(main_module);
2013-08-02 18:36:47 -04:00
// PyDict_SetItem(main_dict, p_prof_module_name, p_prof_module);
2013-07-30 18:37:46 -04:00
2013-08-02 18:36:47 -04:00
// TODO change to use XDG spec
PySys_SetPath("$PYTHONPATH:./plugins/");
if (module_names != NULL) {
cons_show("Loading plugins...");
GSList *module_name = module_names;
while (module_name != NULL) {
cons_show("Loading plugin: %s", module_name->data);
//p_module_name = PyString_FromString(module_name->data);
p_module = PyImport_ImportModule(module_name->data);
//Py_XDECREF(p_module_name);
if (p_module != NULL) {
cons_show("LOADED");
p_prof_init = PyObject_GetAttrString(p_module, "prof_init");
if (p_prof_init && PyCallable_Check(p_prof_init)) {
p_args = Py_BuildValue("ss", PACKAGE_VERSION, PACKAGE_STATUS);
PyObject_CallObject(p_prof_init, p_args);
//Py_XDECREF(p_args);
//Py_XDECREF(p_prof_init);
}
p_prof_on_start = PyObject_GetAttrString(p_module, "prof_on_start");
if (p_prof_on_start && PyCallable_Check(p_prof_on_start)) {
PyObject_CallObject(p_prof_on_start, NULL);
//Py_XDECREF(p_prof_on_start);
}
//Py_XDECREF(p_module);
} else {
cons_show("p_module NULL");
if (PyErr_Occurred()) {
PyErr_Print();
}
}
module_name = g_slist_next(module_name);
}
}
Py_Finalize();
2013-07-24 19:54:31 -04:00
return;
}
2013-08-02 18:36:47 -04:00
static GSList *
_get_module_names(void)
{
GSList *result = NULL;
// TODO change to use XDG
GDir *plugins_dir = g_dir_open("./plugins", 0, NULL);
if (plugins_dir != NULL) {
const gchar *file = g_dir_read_name(plugins_dir);
while (file != NULL) {
if (g_str_has_suffix(file, ".py")) {
gchar *module_name = g_strndup(file, strlen(file) - 3);
result = g_slist_append(result, module_name);
}
file = g_dir_read_name(plugins_dir);
}
g_dir_close(plugins_dir);
return result;
} else {
return NULL;
}
}