diff options
author | Tim Potter <tpot@samba.org> | 2006-04-23 15:30:40 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 14:04:13 -0500 |
commit | 8fe1aa195b247201f01c4f5a32121b2ace132d8c (patch) | |
tree | 7dc7b15ba810595dc739d09f363b70cfabfb2ee5 /source4/lib | |
parent | 5ea77ad66b5fc092b697e705571591b3041f0ffe (diff) | |
download | samba-8fe1aa195b247201f01c4f5a32121b2ace132d8c.tar.gz samba-8fe1aa195b247201f01c4f5a32121b2ace132d8c.tar.bz2 samba-8fe1aa195b247201f01c4f5a32121b2ace132d8c.zip |
r15178: Fix ldb function calls after big swig rename.
Add close() methods to Ldb and LdbMessage classes.
Start adding dict interface to LdbMessage.
(This used to be commit 8be846a3c3e963a3c1a1fc819bd5c0b1db5e6865)
Diffstat (limited to 'source4/lib')
-rw-r--r-- | source4/lib/ldb/swig/Ldb.py | 80 |
1 files changed, 55 insertions, 25 deletions
diff --git a/source4/lib/ldb/swig/Ldb.py b/source4/lib/ldb/swig/Ldb.py index 315a393b31..099fbcd6e0 100644 --- a/source4/lib/ldb/swig/Ldb.py +++ b/source4/lib/ldb/swig/Ldb.py @@ -22,7 +22,12 @@ from ldb import * -ldb_global_init() +# Global initialisation + +result = ldb_global_init() + +if result != 0: + raise LdbError, (result, 'ldb_global_init failed') class LdbError(Exception): """An exception raised when a ldb error occurs.""" @@ -31,29 +36,48 @@ class LdbError(Exception): class LdbMessage: """A class representing a ldb message as a Python dictionary.""" - def __init__(self, msg = None): - - self._msg = msg - if self._msg is None: - self._msg = ldb_msg_new(None) + def __init__(self): + self.mem_ctx = talloc_init(None) + self.msg = ldb_msg_new(self.mem_ctx) def __del__(self): - talloc_free(self._msg) - + self.close() + + def close(self): + if self.mem_ctx is not None: + talloc_free(self.mem_ctx) + self.mem_ctx = None + self.msg = None + + def __getattr__(self, attr): + if attr == 'dn': + return ldb_dn_linearize(None, self.msg.dn) + return self.__dict__[attr] + + def __setattr__(self, attr, value): + if attr == 'dn': + self.msg.dn = ldb_dn_explode(self.msg, value) + return + self.__dict__[attr] = value + def len(self): - return self._msg.num_elements + return self.msg.num_elements def __getitem__(self, key): - elt = ldb_msg_find_element(self._msg, key) + + elt = ldb_msg_find_element(self.msg, key) + if elt is None: raise KeyError, "No such attribute '%s'" % key + return [ldb_val_array_getitem(elt.values, i) for i in range(elt.num_values)] def __setitem__(self, key, value): - result = ldb_msg_add_value(self._msg, key, str(value)) - if result != LDB_SUCCESS: - raise LdbError, (result, ldb.strerror(result)) + if type(value) in (list, tuple): + [ldb_msg_add_value(self.msg, key, v) for v in value] + else: + ldb_msg_add_value(self.msg, key, value) class Ldb: """A class representing a binding to a ldb file.""" @@ -67,12 +91,18 @@ class Ldb: result = ldb_connect(self.ldb_ctx, url, flags, None) if result != LDB_SUCCESS: - raise ldbError, (result, ldb.strerror(result)) + raise LdbError, (result, ldb_strerror(result)) def __del__(self): - ldb.talloc_free(self.mem_ctx) - self.mem_ctx = None - self.ldb_ctx = None + """Called when the object is to be garbage collected.""" + self.close() + + def close(self): + """Close down a ldb.""" + if self.mem_ctx is not None: + talloc_free(self.mem_ctx) + self.mem_ctx = None + self.ldb_ctx = None def _ldb_call(self, fn, *args): """Call a ldb function with args. Raise a LdbError exception @@ -80,16 +110,16 @@ class Ldb: result = fn(*args) - if result != ldb.LDB_SUCCESS: - raise LdbError, (result, ldb.strerror(result)) + if result != LDB_SUCCESS: + raise LdbError, (result, ldb_strerror(result)) def search(self, expression): """Search a ldb for a given expression.""" - self._ldb_call(ldb.search, self.ldb_ctx, None, ldb.LDB_SCOPE_DEFAULT, + self._ldb_call(ldb_search, self.ldb_ctx, None, LDB_SCOPE_DEFAULT, expression, None); - return [LdbMessage(ldb.ldb_message_ptr_array_getitem(result.msgs, ndx)) + return [LdbMessage(ldb_message_ptr_array_getitem(result.msgs, ndx)) for ndx in range(result.count)] def delete(self, dn): @@ -97,7 +127,7 @@ class Ldb: _dn = ldb_dn_explode(self.ldb_ctx, dn) - self._ldb_call(ldb.delete, self.ldb_ctx, _dn) + self._ldb_call(ldb_delete, self.ldb_ctx, _dn) def rename(self, olddn, newdn): """Rename a dn.""" @@ -105,7 +135,7 @@ class Ldb: _olddn = ldb_dn_explode(self.ldb_ctx, olddn) _newdn = ldb_dn_explode(self.ldb_ctx, newdn) - self._ldb_call(ldb.rename, self.ldb_ctx, _olddn, _newdn) + self._ldb_call(ldb_rename, self.ldb_ctx, _olddn, _newdn) - def add(self, msg): - self._ldb_call(ldb.add, self.ldb_ctx, msg) + def add(self, m): + self._ldb_call(ldb_add, self.ldb_ctx, m.msg) |