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

Added notify to python API

This commit is contained in:
James Booth 2013-08-04 16:42:25 +01:00
parent 17e14347e3
commit d638a4825e
6 changed files with 95 additions and 15 deletions

26
plugins/cricket-score.py Normal file
View File

@ -0,0 +1,26 @@
import prof
import urllib2
import json
import time
score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=England"
# hooks
def prof_on_start():
req = urllib2.Request(score_url, None, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
result_json = json.loads(response);
batting1 = result_json['cb1']
batting2 = result_json['cb2']
team1_first = result_json['t1FI']
team2_first = result_json['t2FI']
prof.cons_show(batting1)
prof.cons_show(batting2)
prof.cons_show(team1_first)
prof.cons_show(team2_first)
prof.notify(team2_first, 5000, "Cricket score")

21
plugins/platform-info.py Normal file
View File

@ -0,0 +1,21 @@
import prof
import platform
# hooks
def prof_init(version, status):
prof.register_command("/platform", 0, 0, "/platform", "Output system information.", "Output system information", cmd_platform)
# local functions
def cmd_platform():
result_summary = platform.platform()
# result_machine = plaform.machine()
# result_node = platform,node()
# result_processor = platform.processor()
# result_release = platform.release()
# result_system = platform.system()
# result_version = platform.version()
# prof.cons_show(result_machine + " " + result_node + " " + result_processor + " " + result_release + " " + result_release + " " + result_system + " " + result_version)
prof.cons_show(result_summary)

View File

@ -23,6 +23,7 @@
#include <Python.h> #include <Python.h>
#include "plugins/command.h" #include "plugins/command.h"
#include "ui/notifier.h"
#include "ui/ui.h" #include "ui/ui.h"
static PyObject* static PyObject*
@ -69,9 +70,26 @@ api_register_command(PyObject *self, PyObject *args)
return Py_BuildValue(""); return Py_BuildValue("");
} }
static PyObject*
api_notify(PyObject *self, PyObject *args)
{
const char *message = NULL;
const char *category = NULL;
int timeout_ms = 5000;
if (!PyArg_ParseTuple(args, "sis", &message, &timeout_ms, &category)) {
return NULL;
}
notify(message, timeout_ms, category);
return Py_BuildValue("");
}
static PyMethodDef apiMethods[] = { static PyMethodDef apiMethods[] = {
{ "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." }, { "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." },
{ "register_command", api_register_command, METH_VARARGS, "Register a command." }, { "register_command", api_register_command, METH_VARARGS, "Register a command." },
{ "notify", api_notify, METH_VARARGS, "Send desktop notification." },
{ NULL, NULL, 0, NULL } { NULL, NULL, 0, NULL }
}; };

View File

@ -33,6 +33,16 @@ static void _on_start(void);
static void _run_plugins(const char * const function, PyObject *p_args); static void _run_plugins(const char * const function, PyObject *p_args);
static GSList* plugins; static GSList* plugins;
static void
_check_error(void)
{
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
void void
plugins_init(void) plugins_init(void)
{ {
@ -42,12 +52,15 @@ plugins_init(void)
GSList *module_names = _get_module_names(); GSList *module_names = _get_module_names();
Py_Initialize(); Py_Initialize();
_check_error();
api_init(); api_init();
_check_error();
// TODO change to use XDG spec // TODO change to use XDG spec
GString *path = g_string_new(Py_GetPath()); GString *path = g_string_new(Py_GetPath());
g_string_append(path, ":./plugins/"); g_string_append(path, ":./plugins/");
PySys_SetPath(path->str); PySys_SetPath(path->str);
_check_error();
g_string_free(path, TRUE); g_string_free(path, TRUE);
if (module_names != NULL) { if (module_names != NULL) {
@ -57,6 +70,7 @@ plugins_init(void)
while (module_name != NULL) { while (module_name != NULL) {
p_module = PyImport_ImportModule(module_name->data); p_module = PyImport_ImportModule(module_name->data);
_check_error();
if (p_module != NULL) { if (p_module != NULL) {
cons_show("Loaded plugin: %s", module_name->data); cons_show("Loaded plugin: %s", module_name->data);
plugins = g_slist_append(plugins, p_module); plugins = g_slist_append(plugins, p_module);
@ -68,11 +82,9 @@ plugins_init(void)
cons_show(""); cons_show("");
_init(); _init();
_check_error();
_on_start(); _on_start();
if (PyErr_Occurred()) { _check_error();
PyErr_Print();
PyErr_Clear();
}
} }
return; return;
} }
@ -137,8 +149,10 @@ _run_plugins(const char * const function, PyObject *p_args)
PyObject *p_module = plugin->data; PyObject *p_module = plugin->data;
if (PyObject_HasAttrString(p_module, function)) { if (PyObject_HasAttrString(p_module, function)) {
p_function = PyObject_GetAttrString(p_module, function); p_function = PyObject_GetAttrString(p_module, function);
_check_error();
if (p_function && PyCallable_Check(p_function)) { if (p_function && PyCallable_Check(p_function)) {
PyObject_CallObject(p_function, p_args); PyObject_CallObject(p_function, p_args);
_check_error();
Py_XDECREF(p_function); Py_XDECREF(p_function);
} }
} }

View File

@ -34,11 +34,9 @@
#include "log.h" #include "log.h"
#include "muc.h" #include "muc.h"
#include "ui/notifier.h"
#include "ui/ui.h" #include "ui/ui.h"
static void _notify(const char * const message, int timeout,
const char * const category);
void void
notifier_init(void) notifier_init(void)
{ {
@ -63,7 +61,7 @@ notify_typing(const char * const handle)
char message[strlen(handle) + 1 + 11]; char message[strlen(handle) + 1 + 11];
sprintf(message, "%s: typing...", handle); sprintf(message, "%s: typing...", handle);
_notify(message, 10000, "Incoming message"); notify(message, 10000, "Incoming message");
} }
void void
@ -78,7 +76,7 @@ notify_invite(const char * const from, const char * const room,
g_string_append_printf(message, "\n\"%s\"", reason); g_string_append_printf(message, "\n\"%s\"", reason);
} }
_notify(message->str, 10000, "Incoming message"); notify(message->str, 10000, "Incoming message");
g_string_free(message, FALSE); g_string_free(message, FALSE);
} }
@ -89,7 +87,7 @@ notify_message(const char * const handle, int win)
char message[strlen(handle) + 1 + 14]; char message[strlen(handle) + 1 + 14];
sprintf(message, "%s: message (%d).", handle, win); sprintf(message, "%s: message (%d).", handle, win);
_notify(message, 10000, "incoming message"); notify(message, 10000, "incoming message");
} }
void void
@ -100,7 +98,7 @@ notify_room_message(const char * const handle, const char * const room, int win)
g_string_append_printf(text, "Room: %s\n", room); g_string_append_printf(text, "Room: %s\n", room);
g_string_append_printf(text, "%s: message (%d).", handle, win); g_string_append_printf(text, "%s: message (%d).", handle, win);
_notify(text->str, 10000, "incoming message"); notify(text->str, 10000, "incoming message");
g_string_free(text, FALSE); g_string_free(text, FALSE);
} }
@ -110,7 +108,7 @@ notify_subscription(const char * const from)
{ {
GString *message = g_string_new("Subscription request: \n"); GString *message = g_string_new("Subscription request: \n");
g_string_append(message, from); g_string_append(message, from);
_notify(message->str, 10000, "Incomming message"); notify(message->str, 10000, "Incomming message");
g_string_free(message, FALSE); g_string_free(message, FALSE);
} }
@ -153,14 +151,14 @@ notify_remind(void)
} }
if ((unread > 0) || (open > 0) || (subs > 0)) { if ((unread > 0) || (open > 0) || (subs > 0)) {
_notify(text->str, 5000, "Incoming message"); notify(text->str, 5000, "Incoming message");
} }
g_string_free(text, TRUE); g_string_free(text, TRUE);
} }
static void void
_notify(const char * const message, int timeout, notify(const char * const message, int timeout,
const char * const category) const char * const category)
{ {
#ifdef HAVE_LIBNOTIFY #ifdef HAVE_LIBNOTIFY

View File

@ -31,3 +31,6 @@ void notify_remind(void);
void notify_invite(const char * const from, const char * const room, void notify_invite(const char * const from, const char * const room,
const char * const reason); const char * const reason);
void notify_subscription(const char * const from); void notify_subscription(const char * const from);
void notify(const char * const message, int timeout,
const char * const category);