1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-02-02 15:08:15 -05:00

Implemented time python callbacks

This commit is contained in:
James Booth 2013-08-04 18:43:46 +01:00
parent 13bef17c0b
commit 084b03691e
8 changed files with 87 additions and 27 deletions

View File

@ -24,7 +24,7 @@ profanity_SOURCES = \
src/config/theme.c src/config/theme.h \
src/plugins/plugins.h src/plugins/plugins.c \
src/plugins/api.h src/plugins/api.c \
src/plugins/command.h src/plugins/command.c
src/plugins/callbacks.h src/plugins/callbacks.c
TESTS = tests/testsuite
check_PROGRAMS = tests/testsuite
@ -53,7 +53,7 @@ tests_testsuite_SOURCES = \
src/config/theme.c src/config/theme.h \
src/plugins/plugins.h src/plugins/plugins.c \
src/plugins/api.h src/plugins/api.c \
src/plugins/command.h src/plugins/command.c \
src/plugins/callbacks.h src/plugins/callbacks.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

View File

@ -7,20 +7,36 @@ score_url = "http://api.scorescard.com/?type=score&teamone=Australia&teamtwo=Eng
# hooks
def prof_on_start():
def prof_init(version, status):
prof.register_timed(get_scores, 10)
def get_scores():
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']
summary = None
prof.cons_show(batting1)
prof.cons_show(batting2)
prof.cons_show(team1_first)
prof.cons_show(team2_first)
if 't1FI' in result_json.keys():
summary = result_json['t1FI']
prof.cons_show(result_json['t1FI'])
prof.notify(team2_first, 5000, "Cricket score")
if 't2FI' in result_json.keys():
summary += "\n" + result_json['t2FI']
prof.cons_show(result_json['t2FI'])
if 't1SI' in result_json.keys():
summary += "\n" + result_json['t1SI']
prof.cons_show(result_json['t1SI'])
if 't2SI' in result_json.keys():
summary += "\n" + result_json['t2SI']
prof.cons_show(result_json['t2SI'])
if 'ms' in result_json.keys():
summary += "\n\n" + result_json['ms']
prof.cons_show("")
prof.cons_show(result_json['ms'])
prof.notify(summary, 5000, "Cricket score")

View File

@ -16,6 +16,6 @@ def cmd_platform():
# 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

@ -22,7 +22,9 @@
#include <Python.h>
#include "plugins/command.h"
#include <glib.h>
#include "plugins/callbacks.h"
#include "ui/notifier.h"
#include "ui/ui.h"
@ -64,7 +66,7 @@ api_register_command(PyObject *self, PyObject *args)
command->long_help = long_help;
command->p_callback = p_callback;
add_command(command);
callbacks_add_command(command);
}
return Py_BuildValue("");
@ -74,12 +76,21 @@ static PyObject *
api_register_timed(PyObject *self, PyObject *args)
{
PyObject *p_callback = NULL;
int interval_ms = 0;
int interval_seconds = 0;
if (!PyArg_ParseTuple(args, "Oi", &p_callback, &interval_ms)) {
if (!PyArg_ParseTuple(args, "Oi", &p_callback, &interval_seconds)) {
return NULL;
}
if (p_callback && PyCallable_Check(p_callback)) {
PluginTimedFunction *timed_function = malloc(sizeof(PluginTimedFunction));
timed_function->p_callback = p_callback;
timed_function->interval_seconds = interval_seconds;
timed_function->timer = g_timer_new();
callbacks_add_timed(timed_function);
}
return Py_BuildValue("");
}

View File

@ -1,5 +1,5 @@
/*
* command.c
* callbacks.c
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
@ -21,23 +21,28 @@
*/
#include "command/command.h"
#include "plugins/command.h"
#include "plugins/callbacks.h"
#include "plugins/plugins.h"
#include "tools/autocomplete.h"
#include "ui/ui.h"
// API functions
static GSList *p_commands = NULL;
static GSList *p_timed_functions = NULL;
void
add_command(PluginCommand *command)
callbacks_add_command(PluginCommand *command)
{
p_commands = g_slist_append(p_commands, command);
cmd_autocomplete_add(command->command_name);
}
void
callbacks_add_timed(PluginTimedFunction *timed_function)
{
p_timed_functions = g_slist_append(p_timed_functions, timed_function);
}
gboolean
plugins_command_run(const char * const cmd)
{
@ -53,3 +58,22 @@ plugins_command_run(const char * const cmd)
}
return FALSE;
}
void
plugins_run_timed(void)
{
GSList *p_timed_function = p_timed_functions;
while (p_timed_function != NULL) {
PluginTimedFunction *timed_function = p_timed_function->data;
gdouble elapsed = g_timer_elapsed(timed_function->timer, NULL);
if (timed_function->interval_seconds > 0 && elapsed >= timed_function->interval_seconds) {
PyObject_CallObject(timed_function->p_callback, NULL);
g_timer_start(timed_function->timer);
}
p_timed_function = g_slist_next(p_timed_function);
}
return;
}

View File

@ -1,5 +1,5 @@
/*
* command.h
* callbacks.h
*
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
@ -20,8 +20,8 @@
*
*/
#ifndef PLUGIN_COMMAND_H
#define PLUGIN_COMMAND_H
#ifndef CALLBACKS_H
#define CALLBACKS_H
#include <Python.h>
@ -37,6 +37,13 @@ typedef struct p_command {
PyObject *p_callback;
} PluginCommand;
void add_command(PluginCommand *command);
typedef struct p_timed_function {
PyObject *p_callback;
int interval_seconds;
GTimer *timer;
} PluginTimedFunction;
void callbacks_add_command(PluginCommand *command);
void callbacks_add_timed(PluginTimedFunction *timed_function);
#endif

View File

@ -23,7 +23,7 @@
#include <Python.h>
#include "plugins/api.h"
#include "plugins/command.h"
#include "plugins/callbacks.h"
#include "plugins/plugins.h"
#include "ui/ui.h"

View File

@ -85,6 +85,8 @@ prof_run(const int disable_tls, char *log_level)
g_timer_start(timer);
}
plugins_run_timed();
ui_handle_special_keys(&ch, inp, size);
ui_refresh();
jabber_process_events();