diff options
Diffstat (limited to 'source4/scripting/swig/Ldb.py')
-rw-r--r-- | source4/scripting/swig/Ldb.py | 61 |
1 files changed, 59 insertions, 2 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) |