Changeset 1dcfd1f


Ignore:
Timestamp:
2013-03-19T20:30:24Z (11 years ago)
Author:
Nick Murdoch <nick@…>
Children:
76ca5cb
Parents:
26f3b5f
Message:

Protocol class now has a name attribute and can be subclassed.

Location:
protocols/python
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • protocols/python/examples/sampleplugin.py

    r26f3b5f r1dcfd1f  
    99print "hello, I'm a test plugin running on Python", sys.version_info
    1010
    11 print bpython.ProtocolPlugin
    12 print bpython.ProtocolPlugin()
     11print bpython.Protocol
     12print bpython.Protocol()
    1313print bpython.register_protocol
    1414
    1515try:
    16     class MyProtocol(bpython.ProtocolPlugin):
     16    class MyProtocol(bpython.Protocol):
    1717        pass
    1818except Exception, e:
  • protocols/python/src/bpython.c

    r26f3b5f r1dcfd1f  
    2323 */
    2424
    25 #include <bitlbee.h>
    2625#include <Python.h>
    2726#include "bytesobject.h"
     27#include "structmember.h"
     28
     29#include <bitlbee.h>
    2830
    2931/* Python module classes: */
     
    3133typedef struct {
    3234    PyObject_HEAD
    33 
    34 } bpython_ProtocolObject;
    35 
    36 static PyTypeObject bpython_ProtocolType = {
     35    PyObject * name; /* protocol name */
     36} Protocol;
     37
     38static void Protocol_dealloc(Protocol * self) {
     39    Py_XDECREF(self->name);
     40    self->ob_type->tp_free((PyObject*)self);
     41}
     42
     43static PyObject * Protocol_new(PyTypeObject * type, PyObject * args, PyObject * kwds) {
     44    Protocol * self;
     45
     46    self = (Protocol *)type->tp_alloc(type, 0);
     47    if (self != NULL) {
     48        self->name = PyString_FromString("unnamed");
     49        if (self->name == NULL) {
     50            Py_DECREF(self);
     51            return NULL;
     52        }
     53    }
     54
     55    return (PyObject *)self;
     56}
     57
     58static int Protocol_init(Protocol *self, PyObject *args, PyObject *kwds)
     59{
     60    PyObject * name;
     61    PyObject * tmp;
     62
     63    static char *kwlist[] = {"name", NULL};
     64
     65    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist,
     66                                      &name))
     67        return -1;
     68
     69    if (name) {
     70        tmp = self->name;
     71        Py_INCREF(name);
     72        self->name = name;
     73        Py_XDECREF(tmp);
     74    }
     75
     76    return 0;
     77}
     78
     79static PyMemberDef Protocol_members[] = {
     80    {"name", T_OBJECT_EX, offsetof(Protocol, name), 0,
     81     "protocol name (used in 'account add')"},
     82    {NULL}  /* Sentinel */
     83};
     84
     85
     86static PyMethodDef Protocol_methods[] = {
     87    //{"name", (PyCFunction)Noddy_name, METH_NOARGS,
     88    // "Return the name, combining the first and last name"
     89    //},
     90    {NULL}  /* Sentinel */
     91};
     92
     93
     94static PyTypeObject ProtocolType = {
    3795    PyObject_HEAD_INIT(NULL)
    3896    0,                         /*ob_size*/
    3997    "bpython.Protocol",  /*tp_name*/
    40     sizeof(bpython_ProtocolObject), /*tp_basicsize*/
     98    sizeof(Protocol), /*tp_basicsize*/
    4199    0,                         /*tp_itemsize*/
    42     0,                        /*tp_dealloc*/
     100    (destructor)Protocol_dealloc, /*tp_dealloc*/
    43101    0,                         /*tp_print*/
    44102    0,                         /*tp_getattr*/
     
    55113    0,                         /*tp_setattro*/
    56114    0,                         /*tp_as_buffer*/
    57     Py_TPFLAGS_DEFAULT,        /*tp_flags*/
     115    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,        /*tp_flags*/
    58116    "Protocol plugin objects",           /* tp_doc */
     117    0,                         /* tp_traverse */
     118    0,                         /* tp_clear */
     119    0,                         /* tp_richcompare */
     120    0,                         /* tp_weaklistoffset */
     121    0,                         /* tp_iter */
     122    0,                         /* tp_iternext */
     123    Protocol_methods,             /* tp_methods */
     124    Protocol_members,             /* tp_members */
     125    0,                         /* tp_getset */
     126    0,                         /* tp_base */
     127    0,                         /* tp_dict */
     128    0,                         /* tp_descr_get */
     129    0,                         /* tp_descr_set */
     130    0,                         /* tp_dictoffset */
     131    (initproc)Protocol_init,      /* tp_init */
     132    0,                         /* tp_alloc */
     133    Protocol_new,                 /* tp_new */
    59134};
    60135
     
    72147}
    73148
    74 static PyMethodDef BpythonMethods[] = {
     149static PyMethodDef bpython_methods[] = {
    75150    {"register_protocol",  bpython_register_protocol, METH_VARARGS,
    76151     "Register a protocol."},
     
    82157    PyObject * m;
    83158
    84     bpython_ProtocolType.tp_new = PyType_GenericNew;
    85     if (PyType_Ready(&bpython_ProtocolType) < 0) {
    86         return;
    87     }
    88 
    89     m = Py_InitModule3("bpython", BpythonMethods, "Bitlbee Plugin module");
    90 
    91     Py_INCREF(&bpython_ProtocolType);
    92     PyModule_AddObject(m, "Protocol", (PyObject *)&bpython_ProtocolType);
     159    ProtocolType.tp_new = PyType_GenericNew;
     160    if (PyType_Ready(&ProtocolType) < 0) {
     161        return;
     162    }
     163
     164    m = Py_InitModule3("bpython", bpython_methods, "Bitlbee Plugin module");
     165
     166    Py_INCREF(&ProtocolType);
     167    PyModule_AddObject(m, "Protocol", (PyObject *)&ProtocolType);
    93168}
    94169
Note: See TracChangeset for help on using the changeset viewer.