ctdb-vacuum: systematize counters into various structs
[Samba/wip.git] / source4 / librpc / ndr / py_misc.c
blobd8edff899aca0ca376fb5fda346f5fc13833639a
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <Python.h>
21 #include "librpc/gen_ndr/misc.h"
23 static int py_GUID_cmp(PyObject *py_self, PyObject *py_other)
25 int ret;
26 struct GUID *self = pytalloc_get_ptr(py_self), *other;
27 other = pytalloc_get_ptr(py_other);
28 if (other == NULL)
29 return -1;
31 ret = GUID_compare(self, other);
32 if (ret < 0) {
33 return -1;
34 } else if (ret > 0) {
35 return 1;
36 } else {
37 return 0;
41 static PyObject *py_GUID_str(PyObject *py_self)
43 struct GUID *self = pytalloc_get_ptr(py_self);
44 char *str = GUID_string(NULL, self);
45 PyObject *ret = PyString_FromString(str);
46 talloc_free(str);
47 return ret;
50 static PyObject *py_GUID_repr(PyObject *py_self)
52 struct GUID *self = pytalloc_get_ptr(py_self);
53 char *str = GUID_string(NULL, self);
54 PyObject *ret = PyString_FromFormat("GUID('%s')", str);
55 talloc_free(str);
56 return ret;
59 static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
61 PyObject *str = NULL;
62 NTSTATUS status;
63 struct GUID *guid = pytalloc_get_ptr(self);
64 const char *kwnames[] = { "str", NULL };
66 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
67 return -1;
69 if (str != NULL) {
70 DATA_BLOB guid_val;
72 if (!PyString_Check(str)) {
73 PyErr_SetString(PyExc_TypeError, "Expected a string argument to GUID()");
74 return -1;
76 guid_val.data = (uint8_t *)PyString_AsString(str);
77 guid_val.length = PyString_Size(str);
78 status = GUID_from_data_blob(&guid_val, guid);
79 if (!NT_STATUS_IS_OK(status)) {
80 PyErr_SetNTSTATUS(status);
81 return -1;
85 return 0;
88 static void py_GUID_patch(PyTypeObject *type)
90 type->tp_init = py_GUID_init;
91 type->tp_str = py_GUID_str;
92 type->tp_repr = py_GUID_repr;
93 type->tp_compare = py_GUID_cmp;
96 #define PY_GUID_PATCH py_GUID_patch
98 static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
100 char *str = NULL;
101 NTSTATUS status;
102 struct policy_handle *handle = pytalloc_get_ptr(self);
103 const char *kwnames[] = { "uuid", "type", NULL };
105 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", discard_const_p(char *, kwnames), &str, &handle->handle_type))
106 return -1;
108 if (str != NULL) {
109 status = GUID_from_string(str, &handle->uuid);
110 if (!NT_STATUS_IS_OK(status)) {
111 PyErr_SetNTSTATUS(status);
112 return -1;
116 return 0;
119 static PyObject *py_policy_handle_repr(PyObject *py_self)
121 struct policy_handle *self = pytalloc_get_ptr(py_self);
122 char *uuid_str = GUID_string(NULL, &self->uuid);
123 PyObject *ret = PyString_FromFormat("policy_handle(%d, '%s')", self->handle_type, uuid_str);
124 talloc_free(uuid_str);
125 return ret;
128 static PyObject *py_policy_handle_str(PyObject *py_self)
130 struct policy_handle *self = pytalloc_get_ptr(py_self);
131 char *uuid_str = GUID_string(NULL, &self->uuid);
132 PyObject *ret = PyString_FromFormat("%d, %s", self->handle_type, uuid_str);
133 talloc_free(uuid_str);
134 return ret;
137 static void py_policy_handle_patch(PyTypeObject *type)
139 type->tp_init = py_policy_handle_init;
140 type->tp_repr = py_policy_handle_repr;
141 type->tp_str = py_policy_handle_str;
144 #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch