diff options
author | Christopher Davis <loafier@gmail.com> | 2006-06-21 23:37:27 +0000 |
---|---|---|
committer | Christopher Davis <loafier@gmail.com> | 2006-06-21 23:37:27 +0000 |
commit | c76f11d4ead827d8e87e265131a3dee534bc0792 (patch) | |
tree | 1eb5048fccff554ead3907e751b58d5b96215144 | |
parent | 7ddcc3b268c8c55b2d6fe80e87e090181fbc1bf7 (diff) | |
download | irssi-python-c76f11d4ead827d8e87e265131a3dee534bc0792.tar.gz irssi-python-c76f11d4ead827d8e87e265131a3dee534bc0792.tar.xz irssi-python-c76f11d4ead827d8e87e265131a3dee534bc0792.zip |
added some objects
git-svn-id: http://svn.irssi.org/repos/irssi-python@4288 dbcabf3a-b0e7-0310-adc4-f8d773084564
-rw-r--r-- | objects/Makefile | 2 | ||||
-rw-r--r-- | objects/command-object.c | 145 | ||||
-rw-r--r-- | objects/command-object.h | 18 | ||||
-rw-r--r-- | objects/factory.c | 3 | ||||
-rw-r--r-- | objects/factory.h | 1 | ||||
-rw-r--r-- | objects/pyscript-object.c | 47 |
6 files changed, 191 insertions, 25 deletions
diff --git a/objects/Makefile b/objects/Makefile index ae14e94..8ebf7c1 100644 --- a/objects/Makefile +++ b/objects/Makefile @@ -15,7 +15,7 @@ chatnet-object.o reconnect-object.o window-object.o textdest-object.o \ rawlog-object.o log-object.o logitem-object.o ignore-object.o \ dcc-object.o dcc-chat-object.o dcc-get-object.o dcc-send-object.o \ netsplit-object.o netsplit-server-object.o netsplit-channel-object.o \ -notifylist-object.o process-object.o factory.o +notifylist-object.o process-object.o command-object.o factory.o pyobjects.a: $(OBJ) ar r pyobjects.a $(OBJ) diff --git a/objects/command-object.c b/objects/command-object.c new file mode 100644 index 0000000..74f9fc2 --- /dev/null +++ b/objects/command-object.c @@ -0,0 +1,145 @@ +#include <Python.h> +#include "pyirssi.h" +#include "pymodule.h" +#include "command-object.h" +#include "pycore.h" + +#define COMMAND(cmd) ((COMMAND_REC *)cmd) + +/* monitor "commandlist remove" signal */ +static void command_cleanup(COMMAND_REC *command) +{ + PyCommand *pycommand = signal_get_user_data(); + + if (command == pycommand->data) + { + pycommand->data = NULL; + pycommand->cleanup_installed = 0; + signal_remove_data("commandlist remove", command_cleanup, pycommand); + } +} + +static void PyCommand_dealloc(PyCommand *self) +{ + if (self->cleanup_installed) + signal_remove_data("commandlist remove", command_cleanup, self); + + self->ob_type->tp_free((PyObject*)self); +} + +static PyObject *PyCommand_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyCommand *self; + + self = (PyCommand *)type->tp_alloc(type, 0); + if (!self) + return NULL; + + return (PyObject *)self; +} + +/* Getter */ +PyDoc_STRVAR(PyCommand_cmd_doc, + "Command name" +); +static PyObject *PyCommand_cmd_get(PyCommand *self, void *closure) +{ + RET_NULL_IF_INVALID(self->data); + RET_AS_STRING_OR_NONE(COMMAND(self->data)->cmd); +} + +PyDoc_STRVAR(PyCommand_category_doc, + "Category" +); +static PyObject *PyCommand_category_get(PyCommand *self, void *closure) +{ + RET_NULL_IF_INVALID(self->data); + RET_AS_STRING_OR_NONE(COMMAND(self->data)->category); +} + +/* specialized getters/setters */ +static PyGetSetDef PyCommand_getseters[] = { + {"cmd", (getter)PyCommand_cmd_get, NULL, + PyCommand_cmd_doc, NULL}, + {"category", (getter)PyCommand_category_get, NULL, + PyCommand_category_doc, NULL}, + {NULL} +}; + +/* Methods */ +/* Methods for object */ +static PyMethodDef PyCommand_methods[] = { + {NULL} /* Sentinel */ +}; + +PyTypeObject PyCommandType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "Command", /*tp_name*/ + sizeof(PyCommand), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)PyCommand_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "PyCommand objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + PyCommand_methods, /* tp_methods */ + 0, /* tp_members */ + PyCommand_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyCommand_new, /* tp_new */ +}; + + +/* command factory function */ +PyObject *pycommand_new(void *command) +{ + PyCommand *pycommand; + + pycommand = py_inst(PyCommand, PyCommandType); + if (!pycommand) + return NULL; + + pycommand->data = command; + pycommand->cleanup_installed = 1; + signal_add_last_data("commandlist remove", command_cleanup, pycommand); + + return (PyObject *)pycommand; +} + +int command_object_init(void) +{ + g_return_val_if_fail(py_module != NULL, 0); + + if (PyType_Ready(&PyCommandType) < 0) + return 0; + + Py_INCREF(&PyCommandType); + PyModule_AddObject(py_module, "Command", (PyObject *)&PyCommandType); + + return 1; +} diff --git a/objects/command-object.h b/objects/command-object.h new file mode 100644 index 0000000..e6402cf --- /dev/null +++ b/objects/command-object.h @@ -0,0 +1,18 @@ +#ifndef _COMMAND_OBJECT_H_ +#define _COMMAND_OBJECT_H_ + +#include <Python.h> +#include "base-objects.h" + +typedef struct +{ + PyIrssiFinal_HEAD(void) +} PyCommand; + +extern PyTypeObject PyCommandType; + +int command_object_init(void); +PyObject *pycommand_new(void *command); +#define pycommand_check(op) PyObject_TypeCheck(op, &PyCommandType) + +#endif diff --git a/objects/factory.c b/objects/factory.c index 3e08074..5b77427 100644 --- a/objects/factory.c +++ b/objects/factory.c @@ -116,6 +116,9 @@ static int init_objects(void) if (!process_object_init()) return 0; + if (!command_object_init()) + return 0; + return 1; } diff --git a/objects/factory.h b/objects/factory.h index c310951..14a3b7c 100644 --- a/objects/factory.h +++ b/objects/factory.h @@ -31,6 +31,7 @@ #include "netsplit-channel-object.h" #include "notifylist-object.h" #include "process-object.h" +#include "command-object.h" int factory_init(void); void factory_deinit(void); diff --git a/objects/pyscript-object.c b/objects/pyscript-object.c index a874b1b..f74861f 100644 --- a/objects/pyscript-object.c +++ b/objects/pyscript-object.c @@ -37,28 +37,32 @@ static void PyScript_dealloc(PyScript* self) static PyObject *PyScript_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyScript *self; + PyScript *self; + PyObject *argv, *modules; + + argv = PyList_New(0); + if (!argv) + return NULL; + + modules = PyDict_New(); + if (!modules) + { + Py_DECREF(argv); + return NULL; + } self = (PyScript *)type->tp_alloc(type, 0); if (!self) + { + Py_DECREF(argv); + Py_DECREF(modules); return NULL; - - self->argv = PyList_New(0); - if (!self->argv) - goto error; - - self->modules = PyDict_New(); - if (!self->modules) - goto error; + } + + self->argv = argv; + self->modules = modules; return (PyObject *)self; - -error: - Py_XDECREF(self->argv); - Py_XDECREF(self->modules); - Py_DECREF(self); - - return NULL; } static PyObject *PyScript_command_bind(PyScript *self, PyObject *args, PyObject *kwds) @@ -67,7 +71,7 @@ static PyObject *PyScript_command_bind(PyScript *self, PyObject *args, PyObject char *cmd; PyObject *func; char *category = NULL; - PY_COMMAND_REC *crec; + PY_SIGNAL_REC *srec; if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO|s", kwlist, &cmd, &func, &category)) return NULL; @@ -75,12 +79,7 @@ static PyObject *PyScript_command_bind(PyScript *self, PyObject *args, PyObject if (!PyCallable_Check(func)) return PyErr_Format(PyExc_TypeError, "func must be callable"); - crec = g_new(PY_COMMAND_REC, 1); - crec->name = g_strdup(cmd); - crec->handler = func; - Py_INCREF(func); - - py_command_bind(category, crec); + srec = py_command_bind(cmd, func, category); /* add record to internal list*/ self->signals = g_slist_append(self->signals, crec); @@ -189,7 +188,7 @@ void pyscript_remove_signals(PyObject *script) for (node = self->signals; node != NULL; node = node->next) { - PY_COMMAND_REC *crec = node->data; + PY_SIGNAL_REC *crec = node->data; py_command_unbind(crec); g_free(crec->name); |