From c9d0f4f61a49fcf12b1ca462cc71d6b1a78ffdfe Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 28 Aug 2018 21:19:45 +1200 Subject: python: Use keyword arguments --- inc/pyalarmd_proto.h | 12 ++++++------ lib/python/alarms.c | 47 ++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/inc/pyalarmd_proto.h b/inc/pyalarmd_proto.h index 4370bdd..5bdfdf6 100644 --- a/inc/pyalarmd_proto.h +++ b/inc/pyalarmd_proto.h @@ -1,11 +1,11 @@ #ifndef PYALARMD_PROTO_H #define PYALARMD_PROTO_H -PyObject* pyalarmd_connect(PyObject *self, PyObject *args); -PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args); -PyObject* pyalarmd_register(PyObject *self, PyObject *args); -PyObject* pyalarmd_deregister(PyObject *self, PyObject *args); -PyObject* pyalarmd_raise(PyObject *self, PyObject *args); -PyObject* pyalarmd_clear(PyObject *self, PyObject *args); +PyObject* pyalarmd_connect(PyObject *, PyObject *, PyObject *); +PyObject* pyalarmd_disconnect(PyObject *, PyObject *, PyObject *); +PyObject* pyalarmd_register(PyObject *, PyObject *, PyObject *); +PyObject* pyalarmd_deregister(PyObject *, PyObject *, PyObject *); +PyObject* pyalarmd_raise(PyObject *, PyObject *, PyObject *); +PyObject* pyalarmd_clear(PyObject *, PyObject *, PyObject *); #endif /* PYALARMD_PROTO_H */ diff --git a/lib/python/alarms.c b/lib/python/alarms.c index 2c42d52..2673bed 100644 --- a/lib/python/alarms.c +++ b/lib/python/alarms.c @@ -14,12 +14,12 @@ #include "pyalarmd_proto.h" static PyMethodDef ExportedMethods[] = { - {"connect" , pyalarmd_connect , METH_VARARGS, "Open a connection to the named socket at the given path, returning the fd associated with it."}, - {"disconnect" , pyalarmd_disconnect, METH_VARARGS, "Close a socket fd previously returned by connect(path)."}, - {"register_alarm" , pyalarmd_register , METH_VARARGS, "Send a request over the given socket to register an alarm with the given name. Returns the UUID of the new alarm as a string"}, - {"deregister_alarm", pyalarmd_deregister, METH_VARARGS, "Send a request over the given socket to deregister an alarm with the given UUID."}, - {"raise_alarm" , pyalarmd_raise , METH_VARARGS, "Send a request over the given socket to raise the alarm with the given UUID."}, - {"clear_alarm" , pyalarmd_clear , METH_VARARGS, "Send a request over the given socket to clear/lower the alarm with the given UUID."}, + {"connect" , (PyCFunction)pyalarmd_connect , METH_VARARGS|METH_KEYWORDS, "Open a connection to the named socket at the given path, returning the fd associated with it."}, + {"disconnect" , (PyCFunction)pyalarmd_disconnect, METH_VARARGS|METH_KEYWORDS, "Close a socket fd previously returned by connect(path)."}, + {"register_alarm" , (PyCFunction)pyalarmd_register , METH_VARARGS|METH_KEYWORDS, "Send a request over the given socket to register an alarm with the given name. Returns the UUID of the new alarm as a string"}, + {"deregister_alarm", (PyCFunction)pyalarmd_deregister, METH_VARARGS|METH_KEYWORDS, "Send a request over the given socket to deregister an alarm with the given UUID."}, + {"raise_alarm" , (PyCFunction)pyalarmd_raise , METH_VARARGS|METH_KEYWORDS, "Send a request over the given socket to raise the alarm with the given UUID."}, + {"clear_alarm" , (PyCFunction)pyalarmd_clear , METH_VARARGS|METH_KEYWORDS, "Send a request over the given socket to clear/lower the alarm with the given UUID."}, {NULL , NULL , 0 , NULL } }; @@ -34,7 +34,8 @@ static struct PyModuleDef alarm_python = { static PyObject *alarmd_error; -PyObject* PyInit_alarms(void) { +PyObject* PyInit_alarms(void) +{ PyObject *module = NULL; module = PyModule_Create(&alarm_python); @@ -44,12 +45,14 @@ PyObject* PyInit_alarms(void) { return module; } -PyObject* pyalarmd_connect(PyObject *self, PyObject *args) { +PyObject* pyalarmd_connect(PyObject *self, PyObject *args, PyObject *keywds) +{ + char *kwlist[] = {"path", NULL}; const char *path = NULL; int sock = 0; struct sockaddr_un server; - if (!PyArg_ParseTuple(args, "s", &path)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist, &path)) { return NULL; } @@ -68,10 +71,12 @@ PyObject* pyalarmd_connect(PyObject *self, PyObject *args) { return PyLong_FromLong(sock); } -PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args) { +PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args, PyObject *keywds) +{ + char *kwlist[] = {"socket", NULL}; int sock = 0; - if (!PyArg_ParseTuple(args, "i", &sock)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "i", kwlist, &sock)) { return NULL; } @@ -80,14 +85,15 @@ PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args) { Py_RETURN_NONE; } -PyObject* pyalarmd_register(PyObject *self, PyObject *args) +PyObject* pyalarmd_register(PyObject *self, PyObject *args, PyObject *keywds) { + char *kwlist[] = {"socket", "description", NULL}; int sock = 0; char *desc; char uuid_str[37]; uuid_t uuid; - if (!PyArg_ParseTuple(args, "is", &sock, &desc)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "is", kwlist, &sock, &desc)) { return NULL; } @@ -101,13 +107,14 @@ PyObject* pyalarmd_register(PyObject *self, PyObject *args) return Py_BuildValue("s", uuid_str); } -PyObject* pyalarmd_deregister(PyObject *self, PyObject *args) +PyObject* pyalarmd_deregister(PyObject *self, PyObject *args, PyObject *keywds) { + char *kwlist[] = {"socket", "uuid", NULL}; int sock = 0; char *uuid_str = NULL; uuid_t uuid; - if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "is", kwlist, &sock, &uuid_str)) { return NULL; } @@ -124,13 +131,14 @@ PyObject* pyalarmd_deregister(PyObject *self, PyObject *args) Py_RETURN_NONE; } -PyObject* pyalarmd_raise(PyObject *self, PyObject *args) +PyObject* pyalarmd_raise(PyObject *self, PyObject *args, PyObject *keywds) { + char *kwlist[] = {"socket", "uuid", NULL}; int sock = 0; char *uuid_str = NULL; uuid_t uuid; - if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "is", kwlist, &sock, &uuid_str)) { return NULL; } @@ -147,13 +155,14 @@ PyObject* pyalarmd_raise(PyObject *self, PyObject *args) Py_RETURN_NONE; } -PyObject* pyalarmd_clear(PyObject *self, PyObject *args) +PyObject* pyalarmd_clear(PyObject *self, PyObject *args, PyObject *keywds) { + char *kwlist[] = {"socket", "uuid", NULL}; int sock = 0; char *uuid_str = NULL; uuid_t uuid; - if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) { + if (!PyArg_ParseTupleAndKeywords(args, keywds, "is", kwlist, &sock, &uuid_str)) { return NULL; } -- cgit v1.1