diff options
-rw-r--r-- | source4/scripting/swig/Ldb.py | 61 | ||||
-rw-r--r-- | source4/scripting/swig/ldb.i | 45 |
2 files changed, 97 insertions, 9 deletions
diff --git a/source4/scripting/swig/Ldb.py b/source4/scripting/swig/Ldb.py index 705a790fef..995f421beb 100644 --- a/source4/scripting/swig/Ldb.py +++ b/source4/scripting/swig/Ldb.py @@ -22,7 +22,49 @@ import ldb +class LdbElement: + """A class representing a ldb element as an array of values.""" + + def __init__(self, elt): + self.name = elt.name + self.flags = elt.flags + self.values = [ldb.ldb_val_array_getitem(elt.values, x) + for x in range(elt.num_values)] + + def __repr__(self): + return '<%s(name=%s) instance at 0x%x' % (self.__class__.__name__, + `self.name`, id(self)) + + def __len__(self): + return self.values.len() + + def __getitem__(self, key): + return self.values[key] + +class LdbMessage: + """A class representing a ldb message as a dict of ldb elements.""" + + def __init__(self, msg): + self.dn = msg.dn + self.private_data = msg.private_data + eltlist = \ + [LdbElement(ldb.ldb_message_element_array_getitem(msg.elements, x)) + for x in range(msg.num_elements)] + self.elements = \ + dict([(x.name, x) for x in eltlist]) + + def __repr__(self): + return '<%s(dn=%s) instance at 0x%x>' % (self.__class__.__name__, + `self.dn`, id(self)) + + def __getitem__(self, key): + return self.elements[key] + + def keys(self): + return self.elements.keys() + class Ldb: + """A class representing a binding to a ldb file.""" def __init__(self): self.mem_ctx = ldb.talloc_init('python ldb') @@ -35,5 +77,20 @@ class Ldb: ldb.connect(self.ldb_ctx, url, flags, None) def search(self, expression): - return ldb.search(self.ldb_ctx, None, ldb.LDB_SCOPE_DEFAULT, - expression, None); + + result = ldb.search(self.ldb_ctx, None, ldb.LDB_SCOPE_DEFAULT, + expression, None); + + return [LdbMessage(ldb.ldb_message_ptr_array_getitem(result.msgs, ndx)) + for ndx in range(result.count)] + + def delete(self, dn): + if ldb.delete(self.ldb_ctx, dn) != 0: + raise IOError, ldb.errstring(self.ldb_ctx) + + def rename(self, olddn, newdn): + if ldb.rename(self.ldb_ctx, olddn, newdn) != 0: + raise IOError, ldb.errstring(self.ldb_ctx) + + def add(self, msg): + ldb.add(self.ldb_ctx, msg) diff --git a/source4/scripting/swig/ldb.i b/source4/scripting/swig/ldb.i index 386c414450..6930ea4c9b 100644 --- a/source4/scripting/swig/ldb.i +++ b/source4/scripting/swig/ldb.i @@ -103,15 +103,10 @@ enum ldb_scope {LDB_SCOPE_DEFAULT=-1, } %typemap(argout) struct ldb_result ** { - unsigned int i; - /* XXX: Handle resultobj by throwing an exception if required */ + /* XXX: Check result for error and throw exception if necessary */ - resultobj = PyList_New((*$1)->count); - - for (i = 0; i < (*$1)->count; i++) { - PyList_SetItem(resultobj, i, SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0)); - } + resultobj = SWIG_NewPointerObj(*$1, SWIGTYPE_p_ldb_result, 0); } %types(struct ldb_result *); @@ -120,6 +115,17 @@ enum ldb_scope {LDB_SCOPE_DEFAULT=-1, * Wrap struct ldb_dn */ +%typemap(in) struct ldb_dn * { + if ($input == Py_None) { + $1 = NULL; + } else if (!PyString_Check($input)) { + PyErr_SetString(PyExc_TypeError, "string arg expected"); + return NULL; + } else { + $1 = ldb_dn_explode(NULL, PyString_AsString($input)); + } +} + %typemap(out) struct ldb_dn * { $result = PyString_FromString(ldb_dn_linearize($1, $1)); } @@ -151,14 +157,39 @@ struct ldb_message { }; /* + * Wrap struct ldb_result + */ + +%array_functions(struct ldb_message *, ldb_message_ptr_array); + +struct ldb_result { + unsigned int count; + struct ldb_message **msgs; + char **refs; + struct ldb_control **controls; +}; + +/* * Wrap ldb functions */ %rename ldb_init init; struct ldb_context *ldb_init(TALLOC_CTX *mem_ctx); +%rename ldb_errstring errstring; +const char *ldb_errstring(struct ldb_context *ldb); + %rename ldb_connect connect; int ldb_connect(struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[]); %rename ldb_search search; int ldb_search(struct ldb_context *ldb, const struct ldb_dn *base, enum ldb_scope scope, const char *expression, const char * const *attrs, struct ldb_result **OUT); + +%rename ldb_delete delete; +int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn); + +%rename ldb_rename rename; +int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn); + +%rename ldb_add add; +int ldb_add(struct ldb_context *ldb, const struct ldb_message *message); |