summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2008-01-11 00:23:33 +0100
committerJelmer Vernooij <jelmer@samba.org>2008-01-11 01:04:25 +0100
commit9269db983d847be7a0cdc9eb2bcc4ebe3066be1c (patch)
tree4d6124d8acf980bccc1e4e7022bd9e0d3fa7a91d
parent4f6177da78d0da64c1cda23bd7a176cfe2e6e140 (diff)
downloadsamba-9269db983d847be7a0cdc9eb2bcc4ebe3066be1c.tar.gz
samba-9269db983d847be7a0cdc9eb2bcc4ebe3066be1c.tar.bz2
samba-9269db983d847be7a0cdc9eb2bcc4ebe3066be1c.zip
python/ldb: Add __getitem__ implementation for LdbMessageElement.
(This used to be commit e6498a0780dd31dfc623a69432004b606aeaccbe)
-rw-r--r--source4/lib/ldb/ldb.i17
-rw-r--r--source4/lib/ldb/ldb.py7
-rw-r--r--source4/lib/ldb/ldb_wrap.c43
-rwxr-xr-xsource4/lib/ldb/tests/python/api.py6
4 files changed, 73 insertions, 0 deletions
diff --git a/source4/lib/ldb/ldb.i b/source4/lib/ldb/ldb.i
index 57cb6b5f47..0ad628fdf5 100644
--- a/source4/lib/ldb/ldb.i
+++ b/source4/lib/ldb/ldb.i
@@ -279,10 +279,27 @@ typedef struct ldb_message_element {
return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
}
#endif
+
+ PyObject *get(int i)
+ {
+ if (i < 0 || i >= $self->num_values)
+ return Py_None;
+
+ return PyString_FromStringAndSize(
+ (const char *)$self->values[i].data,
+ $self->values[i].length);
+ }
+
~ldb_msg_element() { talloc_free($self); }
int compare(ldb_msg_element *);
}
%pythoncode {
+ def __getitem__(self, i):
+ ret = self.get(i)
+ if ret is None:
+ raise KeyError("no such value")
+ return ret
+
def __eq__(self, other):
if (isinstance(other, str) and
len(set(self)) == 1 and
diff --git a/source4/lib/ldb/ldb.py b/source4/lib/ldb/ldb.py
index ebf8f6025a..51a4a0fadb 100644
--- a/source4/lib/ldb/ldb.py
+++ b/source4/lib/ldb/ldb.py
@@ -94,6 +94,12 @@ class ldb_msg_element(object):
def __init__(self): raise AttributeError, "No constructor defined"
__repr__ = _swig_repr
__swig_destroy__ = _ldb.delete_ldb_msg_element
+ def __getitem__(self, i):
+ ret = self.get(i)
+ if ret is None:
+ raise KeyError("no such value")
+ return ret
+
def __eq__(self, other):
if (isinstance(other, str) and
len(set(self)) == 1 and
@@ -104,6 +110,7 @@ class ldb_msg_element(object):
ldb_msg_element.__iter__ = new_instancemethod(_ldb.ldb_msg_element___iter__,None,ldb_msg_element)
ldb_msg_element.__set__ = new_instancemethod(_ldb.ldb_msg_element___set__,None,ldb_msg_element)
+ldb_msg_element.get = new_instancemethod(_ldb.ldb_msg_element_get,None,ldb_msg_element)
ldb_msg_element.__cmp__ = new_instancemethod(_ldb.ldb_msg_element___cmp__,None,ldb_msg_element)
ldb_msg_element_swigregister = _ldb.ldb_msg_element_swigregister
ldb_msg_element_swigregister(ldb_msg_element)
diff --git a/source4/lib/ldb/ldb_wrap.c b/source4/lib/ldb/ldb_wrap.c
index 7368d7f058..6e809b6504 100644
--- a/source4/lib/ldb/ldb_wrap.c
+++ b/source4/lib/ldb/ldb_wrap.c
@@ -2889,6 +2889,14 @@ SWIG_AsVal_int (PyObject * obj, int *val)
SWIGINTERN ldb_msg_element *new_ldb_msg_element(PyObject *set_obj,int flags,char const *name){
return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name);
}
+SWIGINTERN PyObject *ldb_msg_element_get(ldb_msg_element *self,int i){
+ if (i < 0 || i >= self->num_values)
+ return Py_None;
+
+ return PyString_FromStringAndSize(
+ (const char *)self->values[i].data,
+ self->values[i].length);
+ }
SWIGINTERN void delete_ldb_msg_element(ldb_msg_element *self){ talloc_free(self); }
PyObject *ldb_msg_list_elements(ldb_msg *msg)
@@ -3671,6 +3679,40 @@ fail:
}
+SWIGINTERN PyObject *_wrap_ldb_msg_element_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) {
+ PyObject *resultobj = 0;
+ ldb_msg_element *arg1 = (ldb_msg_element *) 0 ;
+ int arg2 ;
+ PyObject *result = 0 ;
+ void *argp1 = 0 ;
+ int res1 = 0 ;
+ int val2 ;
+ int ecode2 = 0 ;
+ PyObject * obj0 = 0 ;
+ PyObject * obj1 = 0 ;
+ char * kwnames[] = {
+ (char *) "self",(char *) "i", NULL
+ };
+
+ if (!PyArg_ParseTupleAndKeywords(args,kwargs,(char *)"OO:ldb_msg_element_get",kwnames,&obj0,&obj1)) SWIG_fail;
+ res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_ldb_message_element, 0 | 0 );
+ if (!SWIG_IsOK(res1)) {
+ SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "ldb_msg_element_get" "', argument " "1"" of type '" "ldb_msg_element *""'");
+ }
+ arg1 = (ldb_msg_element *)(argp1);
+ ecode2 = SWIG_AsVal_int(obj1, &val2);
+ if (!SWIG_IsOK(ecode2)) {
+ SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "ldb_msg_element_get" "', argument " "2"" of type '" "int""'");
+ }
+ arg2 = (int)(val2);
+ result = (PyObject *)ldb_msg_element_get(arg1,arg2);
+ resultobj = result;
+ return resultobj;
+fail:
+ return NULL;
+}
+
+
SWIGINTERN PyObject *_wrap_delete_ldb_msg_element(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
ldb_msg_element *arg1 = (ldb_msg_element *) 0 ;
@@ -5254,6 +5296,7 @@ static PyMethodDef SwigMethods[] = {
{ (char *)"ldb_msg_element___iter__", (PyCFunction)_wrap_ldb_msg_element___iter__, METH_O, NULL},
{ (char *)"ldb_msg_element___set__", (PyCFunction)_wrap_ldb_msg_element___set__, METH_O, NULL},
{ (char *)"new_MessageElement", (PyCFunction) _wrap_new_MessageElement, METH_VARARGS | METH_KEYWORDS, NULL},
+ { (char *)"ldb_msg_element_get", (PyCFunction) _wrap_ldb_msg_element_get, METH_VARARGS | METH_KEYWORDS, NULL},
{ (char *)"delete_ldb_msg_element", (PyCFunction)_wrap_delete_ldb_msg_element, METH_O, NULL},
{ (char *)"ldb_msg_element___cmp__", (PyCFunction) _wrap_ldb_msg_element___cmp__, METH_VARARGS | METH_KEYWORDS, NULL},
{ (char *)"ldb_msg_element_swigregister", ldb_msg_element_swigregister, METH_VARARGS, NULL},
diff --git a/source4/lib/ldb/tests/python/api.py b/source4/lib/ldb/tests/python/api.py
index d9dfce8718..0a5ba035ff 100755
--- a/source4/lib/ldb/tests/python/api.py
+++ b/source4/lib/ldb/tests/python/api.py
@@ -382,6 +382,12 @@ class MessageElementTests(unittest.TestCase):
x = ldb.MessageElement(["foo"])
self.assertEquals(["foo"], list(x))
+ def test_get_item(self):
+ x = ldb.MessageElement(["foo", "bar"])
+ self.assertEquals("foo", x[0])
+ self.assertEquals("bar", x[1])
+ self.assertRaises(KeyError, lambda: x[-1])
+
class ExampleModule:
name = "example"