1 """Provide a more Pythonic and object-oriented interface to ldb."""
4 # Swig interface to Samba
6 # Copyright (C) Tim Potter 2006
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 """A class representing a ldb element as an array of values."""
28 def __init__(self, elt):
30 self.flags = elt.flags
31 self.values = [ldb.ldb_val_array_getitem(elt.values, x)
32 for x in range(elt.num_values)]
35 return '<%s(name=%s) instance at 0x%x' % (self.__class__.__name__,
36 `self.name`, id(self))
39 return self.values.len()
41 def __getitem__(self, key):
42 return self.values[key]
45 """A class representing a ldb message as a dict of ldb elements."""
47 def __init__(self, msg = None):
50 self.private_data = None
55 self.private_data = msg.private_data
57 [LdbElement(ldb.ldb_message_element_array_getitem(
59 for x in range(msg.num_elements)]
60 self.elements = dict([(x.name, x) for x in eltlist])
63 return '<%s(dn=%s) instance at 0x%x>' % (self.__class__.__name__,
66 def __getitem__(self, key):
67 return self.elements[key]
70 return self.elements.keys()
73 """A class representing a binding to a ldb file."""
76 self.mem_ctx = ldb.talloc_init('python ldb')
77 self.ldb_ctx = ldb.init(self.mem_ctx)
80 ldb.talloc_free(self.mem_ctx)
82 def connect(self, url, flags = 0):
83 ldb.connect(self.ldb_ctx, url, flags, None)
85 def search(self, expression):
87 result = ldb.search(self.ldb_ctx, None, ldb.LDB_SCOPE_DEFAULT,
90 return [LdbMessage(ldb.ldb_message_ptr_array_getitem(result.msgs, ndx))
91 for ndx in range(result.count)]
94 if ldb.delete(self.ldb_ctx, dn) != 0:
95 raise IOError, ldb.errstring(self.ldb_ctx)
97 def rename(self, olddn, newdn):
98 if ldb.rename(self.ldb_ctx, olddn, newdn) != 0:
99 raise IOError, ldb.errstring(self.ldb_ctx)
102 ldb.add(self.ldb_ctx, msg)