ldb:pyldb - optimise includes
[metze/samba/wip.git] / source4 / lib / ldb / pyldb.c
index 1f5bd1e58d51c27349cc0be027a5a9c045fc1fd6..44a006ffb305c0aa37cfa6cf22ebc5585b1c1c59 100644 (file)
@@ -26,9 +26,6 @@
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <Python.h>
-#include "replace.h"
-#include "ldb_private.h"
 #include "pyldb.h"
 
 /* There's no Py_ssize_t in 2.4, apparently */
@@ -48,24 +45,24 @@ static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_
                return; /* Python exception should already be set, just keep that */
 
        PyErr_SetObject(error, 
-                                       Py_BuildValue(discard_const_p(char, "(i,s)"), ret, 
-                                 ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
+                       Py_BuildValue(discard_const_p(char, "(i,s)"), ret,
+                                     ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
 }
 
 static PyObject *PyExc_LdbError;
 
-PyAPI_DATA(PyTypeObject) PyLdbMessage;
-PyAPI_DATA(PyTypeObject) PyLdbModule;
-PyAPI_DATA(PyTypeObject) PyLdbDn;
-PyAPI_DATA(PyTypeObject) PyLdb;
-PyAPI_DATA(PyTypeObject) PyLdbMessageElement;
-PyAPI_DATA(PyTypeObject) PyLdbTree;
+extern PyTypeObject PyLdbMessage;
+extern PyTypeObject PyLdbModule;
+extern PyTypeObject PyLdbDn;
+extern PyTypeObject PyLdb;
+extern PyTypeObject PyLdbMessageElement;
+extern PyTypeObject PyLdbTree;
 
 static PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx);
 
 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 
-                                                          struct ldb_message_element *el, 
-                                                          struct ldb_val *val)
+                                      struct ldb_message_element *el,
+                                      struct ldb_val *val)
 {
        struct ldb_val new_val;
        TALLOC_CTX *mem_ctx = talloc_new(NULL);
@@ -95,8 +92,7 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
        } 
        ret = PyList_New(result->count);
        for (i = 0; i < result->count; i++) {
-               PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
-               );
+               PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i]));
        }
        return ret;
 }
@@ -110,7 +106,7 @@ static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
  * @return a ldb_result, or NULL if the conversion failed
  */
 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, 
-                                                                                          PyObject *obj)
+                                              PyObject *obj)
 {
        struct ldb_result *res;
        Py_ssize_t i;
@@ -490,7 +486,7 @@ static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
 }
 
 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 
-                                                                               const char *paramname)
+                                       const char *paramname)
 {
        const char **ret;
        Py_ssize_t i;
@@ -506,7 +502,7 @@ static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list,
                        return NULL;
                }
                ret[i] = talloc_strndup(ret, PyString_AsString(item),
-                                                          PyString_Size(item));
+                                       PyString_Size(item));
        }
        ret[i] = NULL;
        return ret;
@@ -682,21 +678,75 @@ static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
 }
 
 
+/**
+ * Obtain a ldb message from a Python Dictionary object.
+ *
+ * @param mem_ctx Memory context
+ * @param py_obj Python Dictionary object
+ * @param ldb_ctx LDB context
+ * @param mod_flags Flags to be set on every message element
+ * @return ldb_message on success or NULL on failure
+ */
+static struct ldb_message *PyDict_AsMessage(TALLOC_CTX *mem_ctx,
+                                           PyObject *py_obj,
+                                           struct ldb_context *ldb_ctx,
+                                           unsigned int mod_flags)
+{
+       struct ldb_message *msg;
+       unsigned int msg_pos = 0;
+       Py_ssize_t dict_pos = 0;
+       PyObject *key, *value;
+       struct ldb_message_element *msg_el;
+       PyObject *dn_value = PyDict_GetItemString(py_obj, "dn");
+
+       msg = ldb_msg_new(mem_ctx);
+       msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_obj));
+
+       if (dn_value) {
+               if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
+                       PyErr_SetString(PyExc_TypeError, "unable to import dn object");
+                       return NULL;
+               }
+               if (msg->dn == NULL) {
+                       PyErr_SetString(PyExc_TypeError, "dn set but not found");
+                       return NULL;
+               }
+       } else {
+               PyErr_SetString(PyExc_TypeError, "no dn set");
+               return NULL;
+       }
+
+       while (PyDict_Next(py_obj, &dict_pos, &key, &value)) {
+               char *key_str = PyString_AsString(key);
+               if (strcmp(key_str, "dn") != 0) {
+                       msg_el = PyObject_AsMessageElement(msg->elements, value,
+                                                          mod_flags, key_str);
+                       if (msg_el == NULL) {
+                               PyErr_SetString(PyExc_TypeError, "unable to import element");
+                               return NULL;
+                       }
+                       memcpy(&msg->elements[msg_pos], msg_el, sizeof(*msg_el));
+                       msg_pos++;
+               }
+       }
+
+       msg->num_elements = msg_pos;
+
+       return msg;
+}
+
 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
 {
-       PyObject *py_msg;
+       PyObject *py_obj;
        int ret;
-       Py_ssize_t dict_pos, msg_pos;
-       struct ldb_message_element *msgel;
-       struct ldb_message *msg;
        struct ldb_context *ldb_ctx;
        struct ldb_request *req;
-       PyObject *key, *value;
+       struct ldb_message *msg = NULL;
        PyObject *py_controls = Py_None;
        TALLOC_CTX *mem_ctx;
        struct ldb_control **parsed_controls;
 
-       if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
+       if (!PyArg_ParseTuple(args, "O|O", &py_obj, &py_controls ))
                return NULL;
 
        mem_ctx = talloc_new(NULL);
@@ -713,49 +763,22 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
                parsed_controls = ldb_parse_control_strings(ldb_ctx, mem_ctx, controls);
                talloc_free(controls);
        }
-       if (PyDict_Check(py_msg)) {
-               PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
-               msg = ldb_msg_new(mem_ctx);
-               msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
-               msg_pos = dict_pos = 0;
-               if (dn_value) {
-                       if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
-                               PyErr_SetString(PyExc_TypeError, "unable to import dn object");
-                               talloc_free(mem_ctx);
-                               return NULL;
-                       }
-                       if (msg->dn == NULL) {
-                               PyErr_SetString(PyExc_TypeError, "dn set but not found");
-                               talloc_free(mem_ctx);
-                               return NULL;
-                       }
-               }
 
-               while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
-                       char *key_str = PyString_AsString(key);
-                       if (strcmp(key_str, "dn") != 0) {
-                               msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
-                               if (msgel == NULL) {
-                                       PyErr_SetString(PyExc_TypeError, "unable to import element");
-                                       talloc_free(mem_ctx);
-                                       return NULL;
-                               }
-                               memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
-                               msg_pos++;
-                       }
-               }
-
-               if (msg->dn == NULL) {
-                       PyErr_SetString(PyExc_TypeError, "no dn set");
-                       talloc_free(mem_ctx);
-                       return NULL;
-               }
-
-               msg->num_elements = msg_pos;
+       if (PyLdbMessage_Check(py_obj)) {
+               msg = PyLdbMessage_AsMessage(py_obj);
+       } else if (PyDict_Check(py_obj)) {
+               msg = PyDict_AsMessage(mem_ctx, py_obj, ldb_ctx, LDB_FLAG_MOD_ADD);
        } else {
-               msg = PyLdbMessage_AsMessage(py_msg);
+               PyErr_SetString(PyExc_TypeError,
+                               "Dictionary or LdbMessage object expected!");
+       }
+
+       if (!msg) {
+               /* we should have a PyErr already set */
+               talloc_free(mem_ctx);
+               return NULL;
        }
-        
+
        ret = ldb_msg_sanity_check(ldb_ctx, msg);
         if (ret != LDB_SUCCESS) {
                PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx);
@@ -994,7 +1017,7 @@ static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
 }
 
 
-static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args)
+static PyObject *py_ldb_write_ldif(PyLdbObject *self, PyObject *args)
 {
        int changetype;
        PyObject *py_msg;
@@ -1977,6 +2000,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;
@@ -2064,6 +2126,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 },