📄 ldb.i
字号:
/* Unix SMB/CIFS implementation. Swig interface to ldb. Copyright (C) 2005,2006 Tim Potter <tpot@samba.org> Copyright (C) 2006 Simo Sorce <idra@samba.org> Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org> ** NOTE! The following LGPL license applies to the ldb ** library. This does NOT imply that all of Samba is released ** under the LGPL This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, see <http://www.gnu.org/licenses/>.*/%define DOCSTRING"An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server."%enddef%module(docstring=DOCSTRING) ldb%{#include <stdint.h>#include <stdbool.h>#include "talloc.h"#include "ldb.h"#include "ldb_errors.h"#include "ldb_private.h"typedef struct ldb_message ldb_msg;typedef struct ldb_context ldb;typedef struct ldb_dn ldb_dn;typedef struct ldb_ldif ldb_ldif;typedef struct ldb_message_element ldb_message_element;typedef int ldb_error;typedef int ldb_int_error;%}%import "carrays.i"%import "typemaps.i"%include "exception.i"%import "stdint.i"/* Don't expose talloc contexts in Python code. Python does reference counting for us, so just create a new top-level talloc context. */%typemap(in, numinputs=0, noblock=1) TALLOC_CTX * { $1 = NULL;}%constant int SCOPE_DEFAULT = LDB_SCOPE_DEFAULT;%constant int SCOPE_BASE = LDB_SCOPE_BASE;%constant int SCOPE_ONELEVEL = LDB_SCOPE_ONELEVEL;%constant int SCOPE_SUBTREE = LDB_SCOPE_SUBTREE;%constant int CHANGETYPE_NONE = LDB_CHANGETYPE_NONE;%constant int CHANGETYPE_ADD = LDB_CHANGETYPE_ADD;%constant int CHANGETYPE_DELETE = LDB_CHANGETYPE_DELETE;%constant int CHANGETYPE_MODIFY = LDB_CHANGETYPE_MODIFY;/* * Wrap struct ldb_context *//* The ldb functions will crash if a NULL ldb context is passed so catch this before it happens. */%typemap(check,noblock=1) struct ldb_context* { if ($1 == NULL) SWIG_exception(SWIG_ValueError, "ldb context must be non-NULL");}%typemap(check,noblock=1) ldb_msg * { if ($1 == NULL) SWIG_exception(SWIG_ValueError, "Message can not be None");}/* * Wrap struct ldb_val */%typemap(in,noblock=1) struct ldb_val *INPUT (struct ldb_val temp) { $1 = &temp; if (!PyString_Check($input)) { PyErr_SetString(PyExc_TypeError, "string arg expected"); return NULL; } $1->length = PyString_Size($input); $1->data = PyString_AsString($input);}%inline %{PyObject *ldb_val_to_py_object(struct ldb_context *ldb_ctx, struct ldb_message_element *el, struct ldb_val *val){ const struct ldb_schema_attribute *a; struct ldb_val new_val; TALLOC_CTX *mem_ctx = talloc_new(NULL); PyObject *ret; new_val = *val; if (ldb_ctx != NULL) { a = ldb_schema_attribute_by_name(ldb_ctx, el->name); if (a != NULL) { if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) { talloc_free(mem_ctx); return NULL; } } } ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length); talloc_free(mem_ctx); return ret;}%}%typemap(out,noblock=1) struct ldb_val * { $result = PyString_FromStringAndSize((const char *)$1->data, $1->length)}%typemap(out,noblock=1) struct ldb_val { $result = PyString_FromStringAndSize((const char *)$1.data, $1.length)}/* * Wrap struct ldb_result */%typemap(in,noblock=1,numinputs=0) struct ldb_result **OUT (struct ldb_result *temp_ldb_result) { $1 = &temp_ldb_result;}#ifdef SWIGPYTHON%typemap(argout,noblock=1) struct ldb_result ** (int i) { $result = PyList_New((*$1)->count); for (i = 0; i < (*$1)->count; i++) { PyList_SetItem($result, i, SWIG_NewPointerObj((*$1)->msgs[i], SWIGTYPE_p_ldb_message, 0) ); }}%typemap(in,noblock=1,numinputs=1) const char * const *NULL_STR_LIST { if ($input == Py_None) { $1 = NULL; } else if (PySequence_Check($input)) { int i; $1 = talloc_array(NULL, char *, PySequence_Size($input)+1); for(i = 0; i < PySequence_Size($input); i++) $1[i] = PyString_AsString(PySequence_GetItem($input, i)); $1[i] = NULL; } else { SWIG_exception(SWIG_TypeError, "expected sequence"); }}%typemap(freearg,noblock=1) const char * const *NULL_STR_LIST { talloc_free($1);}%apply const char * const *NULL_STR_LIST { const char * const *attrs }%apply const char * const *NULL_STR_LIST { const char * const *control_strings }#endif%types(struct ldb_result *);/* * Wrap struct ldb_dn */%rename(__str__) ldb_dn::get_linearized;%rename(__cmp__) ldb_dn::compare;%rename(__len__) ldb_dn::get_comp_num;%rename(Dn) ldb_dn;%feature("docstring") ldb_dn "A LDB distinguished name.";typedef struct ldb_dn { %extend { %feature("docstring") ldb_dn "S.__init__(ldb, string)\n" \ "Create a new DN."; ldb_dn(ldb *ldb_ctx, const char *str) { ldb_dn *ret = ldb_dn_new(ldb_ctx, ldb_ctx, str); /* ldb_dn_new() doesn't accept NULL as memory context, so we do it this way... */ talloc_steal(NULL, ret); if (ret == NULL) SWIG_exception(SWIG_ValueError, "unable to parse dn string");fail: return ret; } ~ldb_dn() { talloc_free($self); } %feature("docstring") validate "S.validate() -> bool\n" \ "Validate DN is correct."; bool validate(); const char *get_casefold(); const char *get_linearized(); %feature("docstring") parent "S.parent() -> dn\n" \ "Get the parent for this DN."; ldb_dn *parent() { return ldb_dn_get_parent(NULL, $self); } int compare(ldb_dn *other); bool is_valid(); %feature("docstring") is_special "S.is_special() -> bool\n" \ "Check whether this is a special LDB DN."; bool is_special(); %feature("docstring") is_null "S.is_null() -> bool\n" \ "Check whether this is a null DN."; bool is_null(); bool check_special(const char *name); int get_comp_num(); %feature("docstring") add_child "S.add_child(dn) -> None\n" \ "Add a child DN to this DN."; bool add_child(ldb_dn *child); %feature("docstring") add_base "S.add_base(dn) -> None\n" \ "Add a base DN to this DN."; bool add_base(ldb_dn *base); %feature("docstring") canonical_str "S.canonical_str() -> string\n" \ "Canonical version of this DN (like a posix path)."; const char *canonical_str() { return ldb_dn_canonical_string($self, $self); } %feature("docstring") canonical_ex_str "S.canonical_ex_str() -> string\n" \ "Canonical version of this DN (like a posix path, with terminating newline)."; const char *canonical_ex_str() { return ldb_dn_canonical_ex_string($self, $self); }#ifdef SWIGPYTHON char *__repr__(void) { char *dn = ldb_dn_get_linearized($self), *ret; asprintf(&ret, "Dn('%s')", dn); talloc_free(dn); return ret; } ldb_dn *__add__(ldb_dn *other) { ldb_dn *ret = ldb_dn_copy(NULL, $self); ldb_dn_add_child(ret, other); return ret; } /* FIXME: implement __getslice__ */#endif %pythoncode { def __eq__(self, other): if isinstance(other, self.__class__): return self.__cmp__(other) == 0 if isinstance(other, str): return str(self) == other return False } }} ldb_dn;#ifdef SWIGPYTHON%{struct ldb_context *ldb_context_from_py_object(PyObject *py_obj){ struct ldb_context *ldb_ctx; if (SWIG_ConvertPtr(py_obj, (void *)&ldb_ctx, SWIGTYPE_p_ldb_context, 0 | 0 ) < 0) return NULL; return ldb_ctx;}int ldb_dn_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *object, struct ldb_context *ldb_ctx, ldb_dn **dn){ int ret; struct ldb_dn *odn; if (ldb_ctx != NULL && PyString_Check(object)) { odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object)); if (!odn) { return SWIG_ERROR; } *dn = odn; return 0; } ret = SWIG_ConvertPtr(object, (void **)&odn, SWIGTYPE_p_ldb_dn, SWIG_POINTER_EXCEPTION); *dn = ldb_dn_copy(mem_ctx, odn); if (odn && !*dn) { return SWIG_ERROR; } return ret;}ldb_message_element *ldb_msg_element_from_pyobject(TALLOC_CTX *mem_ctx, PyObject *set_obj, int flags, const char *attr_name){ struct ldb_message_element *me = talloc(mem_ctx, struct ldb_message_element); me->name = attr_name; me->flags = flags; if (PyString_Check(set_obj)) { me->num_values = 1; me->values = talloc_array(me, struct ldb_val, me->num_values); me->values[0].length = PyString_Size(set_obj); me->values[0].data = (uint8_t *)talloc_strdup(me->values, PyString_AsString(set_obj)); } else if (PySequence_Check(set_obj)) { int i; me->num_values = PySequence_Size(set_obj); me->values = talloc_array(me, struct ldb_val, me->num_values); for (i = 0; i < me->num_values; i++) { PyObject *obj = PySequence_GetItem(set_obj, i); me->values[i].length = PyString_Size(obj); me->values[i].data = (uint8_t *)PyString_AsString(obj); } } else { talloc_free(me); me = NULL; } return me;}PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, ldb_message_element *me){ int i; PyObject *result; /* Python << 2.5 doesn't have PySet_New and PySet_Add. */ result = PyList_New(me->num_values); for (i = 0; i < me->num_values; i++) { PyList_SetItem(result, i, ldb_val_to_py_object(ldb_ctx, me, &me->values[i])); } return result;}%}#endif/* ldb_message_element */%rename(MessageElement) ldb_message_element;%feature("docstring") ldb_message_element "Message element.";typedef struct ldb_message_element { %extend {#ifdef SWIGPYTHON int __cmp__(ldb_message_element *other) { return ldb_msg_element_compare($self, other); } PyObject *__iter__(void) { return PyObject_GetIter(ldb_msg_element_to_set(NULL, $self)); } PyObject *__set__(void) { return ldb_msg_element_to_set(NULL, $self); } ldb_message_element(PyObject *set_obj, int flags=0, const char *name = NULL) { return ldb_msg_element_from_pyobject(NULL, set_obj, flags, name); } int __len__() { return $self->num_values; }#endif PyObject *get(int i) { if (i < 0 || i >= $self->num_values) return Py_None; return ldb_val_to_py_object(NULL, $self, &$self->values[i]); } ~ldb_message_element() { talloc_free($self); } } %pythoncode { def __getitem__(self, i): ret = self.get(i) if ret is None: raise KeyError("no such value") return ret def __repr__(self): return "MessageElement([%s])" % (",".join(repr(x) for x in self.__set__())) def __eq__(self, other): if (len(self) == 1 and self.get(0) == other): return True if isinstance(other, self.__class__): return self.__cmp__(other) == 0 o = iter(other) for i in range(len(self)): if self.get(i) != o.next(): return False return True }} ldb_message_element;/* ldb_message */%feature("docstring") ldb_message "Message.";%rename(Message) ldb_message;#ifdef SWIGPYTHON%rename(__delitem__) ldb_message::remove_attr;%typemap(out) ldb_message_element * { if ($1 == NULL) PyErr_SetString(PyExc_KeyError, "no such element"); else $result = SWIG_NewPointerObj($1, SWIGTYPE_p_ldb_message_element, 0);}%inline { PyObject *ldb_msg_list_elements(ldb_msg *msg) { int i, j = 0; PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0)); if (msg->dn != NULL) { PyList_SetItem(obj, j, PyString_FromString("dn")); j++; } for (i = 0; i < msg->num_elements; i++) { PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name)); j++; } return obj; }}#endiftypedef struct ldb_message { ldb_dn *dn; %extend {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -