r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / ldb / swig / Ldb.py
blobc7e6191c8a6202ccebd156c52701129dda5da87b
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.
24 # Interface notes:
26 # - should an empty dn be represented as None, or an empty string?
28 # - should single-valued attributes be a string, or a list with one
29 # element?
32 from ldb import *
34 # Global initialisation
36 result = ldb_global_init()
38 if result != 0:
39 raise LdbError, (result, 'ldb_global_init failed')
41 # Ldb exceptions
43 class LdbError(Exception):
44 """An exception raised when a ldb error occurs.
45 The exception data is a tuple consisting of the ldb number and a
46 string description of the error."""
47 pass
49 # Ldb classes
51 class LdbMessage:
52 """A class representing a ldb message as a Python dictionary."""
54 def __init__(self):
55 self.mem_ctx = talloc_init(None)
56 self.msg = ldb_msg_new(self.mem_ctx)
58 def __del__(self):
59 if self.mem_ctx is not None:
60 talloc_free(self.mem_ctx)
61 self.mem_ctx = None
62 self.msg = None
64 # Make the dn attribute of the object dynamic
66 def __getattr__(self, attr):
67 if attr == 'dn':
68 return ldb_dn_linearize(None, self.msg.dn)
69 return self.__dict__[attr]
71 def __setattr__(self, attr, value):
72 if attr == 'dn':
73 self.msg.dn = ldb_dn_explode(self.msg, value)
74 if self.msg.dn == None:
75 err = LDB_ERR_INVALID_DN_SYNTAX
76 raise LdbError(err, ldb_strerror(err))
77 return
78 self.__dict__[attr] = value
80 # Get and set individual elements
82 def __getitem__(self, key):
84 elt = ldb_msg_find_element(self.msg, key)
86 if elt is None:
87 raise KeyError, "No such attribute '%s'" % key
89 return [ldb_val_array_getitem(elt.values, i)
90 for i in range(elt.num_values)]
92 def __setitem__(self, key, value):
93 ldb_msg_remove_attr(self.msg, key)
94 if type(value) in (list, tuple):
95 [ldb_msg_add_value(self.msg, key, v) for v in value]
96 else:
97 ldb_msg_add_value(self.msg, key, value)
99 # Dictionary interface
100 # TODO: move to iterator based interface
102 def len(self):
103 return self.msg.num_elements
105 def keys(self):
106 return [ldb_message_element_array_getitem(self.msg.elements, i).name
107 for i in range(self.msg.num_elements)]
109 def values(self):
110 return [self[k] for k in self.keys()]
112 def items(self):
113 return [(k, self[k]) for k in self.keys()]
115 # Misc stuff
117 def sanity_check(self):
118 return ldb_msg_sanity_check(self.msg)
120 class Ldb:
121 """A class representing a binding to a ldb file."""
123 def __init__(self, url, flags = 0):
124 """Initialise underlying ldb."""
126 self.mem_ctx = talloc_init('mem_ctx for ldb 0x%x' % id(self))
127 self.ldb_ctx = ldb_init(self.mem_ctx)
129 result = ldb_connect(self.ldb_ctx, url, flags, None)
131 if result != LDB_SUCCESS:
132 raise LdbError, (result, ldb_strerror(result))
134 def __del__(self):
135 """Called when the object is to be garbage collected."""
136 self.close()
138 def close(self):
139 """Close down a ldb."""
140 if self.mem_ctx is not None:
141 talloc_free(self.mem_ctx)
142 self.mem_ctx = None
143 self.ldb_ctx = None
145 def _ldb_call(self, fn, *args):
146 """Call a ldb function with args. Raise a LdbError exception
147 if the function returns a non-zero return value."""
149 result = fn(*args)
151 if result != LDB_SUCCESS:
152 raise LdbError, (result, ldb_strerror(result))
154 def search(self, expression):
155 """Search a ldb for a given expression."""
157 self._ldb_call(ldb_search, self.ldb_ctx, None, LDB_SCOPE_DEFAULT,
158 expression, None);
160 return [LdbMessage(ldb_message_ptr_array_getitem(result.msgs, ndx))
161 for ndx in range(result.count)]
163 def delete(self, dn):
164 """Delete a dn."""
166 _dn = ldb_dn_explode(self.ldb_ctx, dn)
168 self._ldb_call(ldb_delete, self.ldb_ctx, _dn)
170 def rename(self, olddn, newdn):
171 """Rename a dn."""
173 _olddn = ldb_dn_explode(self.ldb_ctx, olddn)
174 _newdn = ldb_dn_explode(self.ldb_ctx, newdn)
176 self._ldb_call(ldb_rename, self.ldb_ctx, _olddn, _newdn)
178 def add(self, m):
179 self._ldb_call(ldb_add, self.ldb_ctx, m.msg)