Changeset 26f3b5f
- Timestamp:
- 2013-03-19T19:58:17Z (12 years ago)
- Children:
- 1dcfd1f
- Parents:
- 93302ef
- Location:
- protocols/python
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/python/examples/sampleplugin.py
r93302ef r26f3b5f 9 9 print "hello, I'm a test plugin running on Python", sys.version_info 10 10 11 # plugin name: 12 name = 'moose' 13 11 print bpython.ProtocolPlugin 12 print bpython.ProtocolPlugin() 14 13 print bpython.register_protocol 15 14 15 try: 16 class MyProtocol(bpython.ProtocolPlugin): 17 pass 18 except Exception, e: 19 print "error:", e 20 21 myproto = MyProtocol() 22 print "myproto: ", myproto 23 16 24 bpython.register_protocol('echo "hello"') -
protocols/python/src/bpython.c
r93302ef r26f3b5f 27 27 #include "bytesobject.h" 28 28 29 /* Python module classes: */ 30 31 typedef struct { 32 PyObject_HEAD 33 34 } bpython_ProtocolObject; 35 36 static 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 29 63 static PyObject * bpython_register_protocol(PyObject *self, PyObject *args) 30 64 { … … 46 80 PyMODINIT_FUNC initbpython(void) 47 81 { 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); 50 93 } 51 94 52 95 53 96 static void load_pyfile(char * path, PyObject * main_dict) { 54 FILE * pyfile;55 97 PyObject * err; 56 98 struct prpl *ret; 57 99 ret = g_new0(struct prpl, 1); 58 100 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"); 70 102 if ((err = PyErr_Occurred()) || !pluginname) { 71 103 printf("No plugin name\n"); … … 100 132 } 101 133 134 /* Bitlbee plugin functions: */ 135 102 136 void init_plugin() { 103 137 GDir *dir; … … 129 163 130 164 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 132 176 } 133 177
Note: See TracChangeset
for help on using the changeset viewer.