r14968: Move tdb and ldb swig wrappers in to lib/tdb and lib/ldb directories.
[ira/wip.git] / source4 / lib / ldb / swig / Ldb.py
1 """Provide a more Pythonic and object-oriented interface to ldb."""
2
3 #
4 # Swig interface to Samba
5 #
6 # Copyright (C) Tim Potter 2006
7 #
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.
12 #   
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.
17 #   
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.
21 #
22
23 import ldb
24
25 class LdbElement:
26     """A class representing a ldb element as an array of values."""
27     
28     def __init__(self, elt):
29         self.name = elt.name
30         self.flags = elt.flags
31         self.values = [ldb.ldb_val_array_getitem(elt.values, x)
32                        for x in range(elt.num_values)]
33
34     def __repr__(self):
35         return '<%s(name=%s) instance at 0x%x' % (self.__class__.__name__,
36                                                   `self.name`, id(self))
37
38     def __len__(self):
39         return self.values.len()
40
41     def __getitem__(self, key):
42         return self.values[key]
43
44 class LdbMessage:
45     """A class representing a ldb message as a dict of ldb elements."""
46     
47     def __init__(self, msg = None):
48
49         self.dn = None
50         self.private_data = None
51         self.elements = []
52
53         if msg is not None:
54             self.dn = msg.dn
55             self.private_data = msg.private_data
56             eltlist = \
57                 [LdbElement(ldb.ldb_message_element_array_getitem(
58                             msg.elements, x))
59                  for x in range(msg.num_elements)]
60             self.elements = dict([(x.name, x) for x in eltlist])
61
62     def __repr__(self):
63         return '<%s(dn=%s) instance at 0x%x>' % (self.__class__.__name__,
64                                                `self.dn`, id(self))
65
66     def __getitem__(self, key):
67         return self.elements[key]
68
69     def keys(self):
70         return self.elements.keys()
71
72 class Ldb:
73     """A class representing a binding to a ldb file."""
74
75     def __init__(self):
76         self.mem_ctx = ldb.talloc_init('python ldb')
77         self.ldb_ctx = ldb.init(self.mem_ctx)
78         
79     def __del__(self):
80         ldb.talloc_free(self.mem_ctx)
81
82     def connect(self, url, flags = 0):
83         ldb.connect(self.ldb_ctx, url, flags, None)
84
85     def search(self, expression):
86
87         result = ldb.search(self.ldb_ctx, None, ldb.LDB_SCOPE_DEFAULT,
88                             expression, None);
89
90         return [LdbMessage(ldb.ldb_message_ptr_array_getitem(result.msgs, ndx))
91                 for ndx in range(result.count)]
92
93     def delete(self, dn):
94         if ldb.delete(self.ldb_ctx, dn) != 0:
95             raise IOError, ldb.errstring(self.ldb_ctx)
96
97     def rename(self, olddn, newdn):
98         if ldb.rename(self.ldb_ctx, olddn, newdn) != 0:
99             raise IOError, ldb.errstring(self.ldb_ctx)
100
101     def add(self, msg):
102         ldb.add(self.ldb_ctx, msg)