Changeset 26f3b5f for protocols/python


Ignore:
Timestamp:
2013-03-19T19:58:17Z (11 years ago)
Author:
Nick Murdoch <nick@…>
Children:
1dcfd1f
Parents:
93302ef
Message:

Create a shell Protocol class for the module.

Can't currently do much with it, including even subclassing.

Location:
protocols/python
Files:
2 edited

Legend:

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

    r93302ef r26f3b5f  
    99print "hello, I'm a test plugin running on Python", sys.version_info
    1010
    11 # plugin name:
    12 name = 'moose'
    13 
     11print bpython.ProtocolPlugin
     12print bpython.ProtocolPlugin()
    1413print bpython.register_protocol
    1514
     15try:
     16    class MyProtocol(bpython.ProtocolPlugin):
     17        pass
     18except Exception, e:
     19    print "error:", e
     20
     21myproto = MyProtocol()
     22print "myproto: ", myproto
     23
    1624bpython.register_protocol('echo "hello"')
  • protocols/python/src/bpython.c

    r93302ef r26f3b5f  
    2727#include "bytesobject.h"
    2828
     29/* Python module classes: */
     30
     31typedef struct {
     32    PyObject_HEAD
     33
     34} bpython_ProtocolObject;
     35
     36static PyTypeObject bpython_ProtocolType = {
     37    PyObject_HEAD_INIT(NULL)
     38    0,                         /*ob_size*/
     39    "bpython.Protocol",  /*tp_name*/
     40    sizeof(bpython_ProtocolObject), /*tp_basicsize*/
     41    0,                         /*tp_itemsize*/
     42    0,                         /*tp_dealloc*/
     43    0,                         /*tp_print*/
     44    0,                         /*tp_getattr*/
     45    0,                         /*tp_setattr*/
     46    0,                         /*tp_compare*/
     47    0,                         /*tp_repr*/
     48    0,                         /*tp_as_number*/
     49    0,                         /*tp_as_sequence*/
     50    0,                         /*tp_as_mapping*/
     51    0,                         /*tp_hash */
     52    0,                         /*tp_call*/
     53    0,                         /*tp_str*/
     54    0,                         /*tp_getattro*/
     55    0,                         /*tp_setattro*/
     56    0,                         /*tp_as_buffer*/
     57    Py_TPFLAGS_DEFAULT,        /*tp_flags*/
     58    "Protocol plugin objects",           /* tp_doc */
     59};
     60
     61/* Python module functions: */
     62
    2963static PyObject * bpython_register_protocol(PyObject *self, PyObject *args)
    3064{
     
    4680PyMODINIT_FUNC initbpython(void)
    4781{
    48     PyImport_AddModule("bpython");
    49     Py_InitModule("bpython", BpythonMethods);
     82    PyObject * m;
     83
     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);
    5093}
    5194
    5295
    5396static void load_pyfile(char * path, PyObject * main_dict) {
    54     FILE * pyfile;
    5597    PyObject * err;
    5698    struct prpl *ret;
    5799    ret = g_new0(struct prpl, 1);
    58100   
    59     printf("Loading python file %s\n", path);
    60     pyfile = fopen(path, "r");
    61    
    62     /* Copy main dict to make sure that separate plugins
    63        run in separate environments */
    64     PyObject * main_dict_copy = PyDict_Copy(main_dict);
    65 
    66     /* Run the python file */
    67     PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
    68    
    69     PyObject * pluginname = PyDict_GetItemString(main_dict_copy, "name");
     101    PyObject * pluginname = PyDict_GetItemString(main_dict, "name");
    70102    if ((err = PyErr_Occurred()) || !pluginname) {
    71103        printf("No plugin name\n");
     
    100132}
    101133
     134/* Bitlbee plugin functions: */
     135
    102136void init_plugin() {
    103137    GDir *dir;
     
    129163           
    130164            if (g_str_has_suffix(path, ".py")) {
    131                 load_pyfile(path, main_dict);
     165                FILE * pyfile;
     166                printf("Loading python file %s\n", path);
     167                pyfile = fopen(path, "r");
     168               
     169                /* Copy main dict to make sure that separate plugins
     170                   run in separate environments */
     171                PyObject * main_dict_copy = PyDict_Copy(main_dict);
     172
     173                /* Run the python file */
     174                PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
     175
    132176            }
    133177
Note: See TracChangeset for help on using the changeset viewer.