From dded9e954f5ec5bc462a9477af53951497a4d3bb Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 3 Aug 2013 00:58:04 +0100 Subject: [PATCH] Separated python api and plugins code --- Makefile.am | 4 +++- src/plugins/api.c | 49 +++++++++++++++++++++++++++++++++++++++++++ src/plugins/api.h | 28 +++++++++++++++++++++++++ src/plugins/plugins.c | 36 ++++++++----------------------- 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 src/plugins/api.c create mode 100644 src/plugins/api.h diff --git a/Makefile.am b/Makefile.am index 25f55a2a..2ccc473f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -22,7 +22,8 @@ profanity_SOURCES = \ src/config/accounts.c src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ - src/plugins/plugins.h src/plugins/plugins.c + src/plugins/plugins.h src/plugins/plugins.c \ + src/plugins/api.h src/plugins/api.c TESTS = tests/testsuite check_PROGRAMS = tests/testsuite @@ -50,6 +51,7 @@ tests_testsuite_SOURCES = \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ src/plugins/plugins.h src/plugins/plugins.c \ + src/plugins/api.h src/plugins/api.c \ tests/test_roster.c tests/test_common.c tests/test_history.c \ tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \ tests/test_jid.c diff --git a/src/plugins/api.c b/src/plugins/api.c new file mode 100644 index 00000000..e57056ad --- /dev/null +++ b/src/plugins/api.c @@ -0,0 +1,49 @@ +/* + * api.c + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#include + +#include "ui/ui.h" + +// API functions + +static PyObject* +api_cons_show(PyObject *self, PyObject *args) +{ + const char *message = NULL; + if (!PyArg_ParseTuple(args, "s", &message)) { + return NULL; + } + cons_show("%s", message); + return Py_BuildValue(""); +} + +static PyMethodDef apiMethods[] = { + { "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." }, + { NULL, NULL, 0, NULL } +}; + +void +api_init(void) +{ + Py_InitModule("prof", apiMethods); +} diff --git a/src/plugins/api.h b/src/plugins/api.h new file mode 100644 index 00000000..38f77dfe --- /dev/null +++ b/src/plugins/api.h @@ -0,0 +1,28 @@ +/* + * api.h + * + * Copyright (C) 2012, 2013 James Booth + * + * 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 . + * + */ + +#ifndef API_H +#define API_H + +void api_init(void); + +#endif diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index a8d4fe81..416659f2 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -1,5 +1,5 @@ /* - * api.c + * plugins.c * * Copyright (C) 2012, 2013 James Booth * @@ -22,6 +22,7 @@ #include +#include "plugins/api.h" #include "plugins/plugins.h" #include "ui/ui.h" @@ -31,25 +32,6 @@ static void _on_start(void); static void _run_plugins(const char * const function, PyObject *p_args); static GSList* plugins; -static PyObject *prof_module; -// API - -static PyObject* -api_cons_show(PyObject *self, PyObject *args) -{ - const char *message = NULL; - if (!PyArg_ParseTuple(args, "s", &message)) { - return NULL; - } - cons_show("%s", message); - return Py_BuildValue(""); -} - -static PyMethodDef apiMethods[] = { - { "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." }, - { NULL, NULL, 0, NULL } -}; - void plugins_init(void) { @@ -59,7 +41,7 @@ plugins_init(void) GSList *module_names = _get_module_names(); Py_Initialize(); - prof_module = Py_InitModule("prof", apiMethods); + api_init(); // TODO change to use XDG spec PySys_SetPath("$PYTHONPATH:./plugins/"); @@ -93,6 +75,12 @@ plugins_shutdown(void) Py_Finalize(); } +void +plugins_on_connect(void) +{ + _run_plugins("prof_on_connect", NULL); +} + static GSList * _get_module_names(void) { @@ -131,12 +119,6 @@ _on_start(void) _run_plugins("prof_on_start", NULL); } -void -plugins_on_connect(void) -{ - _run_plugins("prof_on_connect", NULL); -} - static void _run_plugins(const char * const function, PyObject *p_args) {