From a403d7777e94ffdcdb57045e8a4755aced0e8484 Mon Sep 17 00:00:00 2001 From: Kamen Mazdrashki Date: Thu, 18 Nov 2010 22:11:30 +0200 Subject: s4-pyldb: ldb.Message.from_dict class method to create LdbMessage object from dictionary --- source4/lib/ldb/pyldb.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'source4') diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c index ac19c4f739..d4b63008f8 100644 --- a/source4/lib/ldb/pyldb.c +++ b/source4/lib/ldb/pyldb.c @@ -2003,6 +2003,45 @@ PyTypeObject PyLdbMessageElement = { .tp_flags = Py_TPFLAGS_DEFAULT, }; + +static PyObject *py_ldb_msg_from_dict(PyTypeObject *type, PyObject *args) +{ + PyObject *py_ldb; + PyObject *py_dict; + PyObject *py_ret; + struct ldb_message *msg; + struct ldb_context *ldb_ctx; + unsigned int mod_flags = LDB_FLAG_MOD_REPLACE; + + if (!PyArg_ParseTuple(args, "O!O!|I", + &PyLdb, &py_ldb, &PyDict_Type, &py_dict, + &mod_flags)) { + return NULL; + } + + /* mask only flags we are going to use */ + mod_flags = LDB_FLAG_MOD_TYPE(mod_flags); + if (!mod_flags) { + PyErr_SetString(PyExc_ValueError, + "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE" + " expected as mod_flag value"); + return NULL; + } + + ldb_ctx = PyLdb_AsLdbContext(py_ldb); + + msg = PyDict_AsMessage(ldb_ctx, py_dict, ldb_ctx, mod_flags); + if (!msg) { + return NULL; + } + + py_ret = PyLdbMessage_FromMessage(msg); + + talloc_unlink(ldb_ctx, msg); + + return py_ret; +} + static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args) { char *name; @@ -2090,6 +2129,11 @@ static PyObject *py_ldb_msg_items(PyLdbMessageObject *self) } static PyMethodDef py_ldb_msg_methods[] = { + { "from_dict", (PyCFunction)py_ldb_msg_from_dict, METH_CLASS | METH_VARARGS, + "Message.from_dict(ldb, dict, mod_flag) -> ldb.Message\n" + "Class method to create ldb.Message object from Dictionary.\n" + "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE.\n" + "mod_flag defaults to FLAG_MOD_REPLACE"}, { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL }, { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL }, { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL }, -- cgit