| 1 |
/** |
| 2 |
* collectd - src/pyconfig.c |
| 3 |
* Copyright (C) 2009 Sven Trenkel |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Sven Trenkel <collectd at semidefinite.de> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include <Python.h> |
| 28 |
#include <structmember.h> |
| 29 |
|
| 30 |
#include "collectd.h" |
| 31 |
|
| 32 |
#include "utils/common/common.h" |
| 33 |
|
| 34 |
#include "cpython.h" |
| 35 |
|
| 36 |
static char config_doc[] = |
| 37 |
"This represents a piece of collectd's config file.\n" |
| 38 |
"It is passed to scripts with config callbacks (see \"register_config\")\n" |
| 39 |
"and is of little use if created somewhere else.\n" |
| 40 |
"\n" |
| 41 |
"It has no methods beyond the bare minimum and only exists for its\n" |
| 42 |
"data members"; |
| 43 |
|
| 44 |
static char parent_doc[] = |
| 45 |
"This represents the parent of this node. On the root node\n" |
| 46 |
"of the config tree it will be None.\n"; |
| 47 |
|
| 48 |
static char key_doc[] = |
| 49 |
"This is the keyword of this item, ie the first word of any\n" |
| 50 |
"given line in the config file. It will always be a string.\n"; |
| 51 |
|
| 52 |
static char values_doc[] = |
| 53 |
"This is a tuple (which might be empty) of all value, ie words\n" |
| 54 |
"following the keyword in any given line in the config file.\n" |
| 55 |
"\n" |
| 56 |
"Every item in this tuple will be either a string or a float or a bool,\n" |
| 57 |
"depending on the contents of the configuration file.\n"; |
| 58 |
|
| 59 |
static char children_doc[] = |
| 60 |
"This is a tuple of child nodes. For most nodes this will be\n" |
| 61 |
"empty. If this node represents a block instead of a single line of the " |
| 62 |
"config\n" |
| 63 |
"file it will contain all nodes in this block.\n"; |
| 64 |
|
| 65 |
static PyObject *Config_new(PyTypeObject *type, PyObject *args, |
| 66 |
PyObject *kwds) { |
| 67 |
Config *self; |
| 68 |
|
| 69 |
self = (Config *)type->tp_alloc(type, 0); |
| 70 |
if (self == NULL) |
| 71 |
return NULL; |
| 72 |
|
| 73 |
self->parent = NULL; |
| 74 |
self->key = NULL; |
| 75 |
self->values = NULL; |
| 76 |
self->children = NULL; |
| 77 |
return (PyObject *)self; |
| 78 |
} |
| 79 |
|
| 80 |
static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) { |
| 81 |
PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp; |
| 82 |
Config *self = (Config *)s; |
| 83 |
static char *kwlist[] = {"key", "parent", "values", "children", NULL}; |
| 84 |
|
| 85 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &key, &parent, |
| 86 |
&values, &children)) |
| 87 |
return -1; |
| 88 |
|
| 89 |
if (!IS_BYTES_OR_UNICODE(key)) { |
| 90 |
PyErr_SetString(PyExc_TypeError, "argument 1 must be str"); |
| 91 |
Py_XDECREF(parent); |
| 92 |
Py_XDECREF(values); |
| 93 |
Py_XDECREF(children); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
if (values == NULL) { |
| 97 |
values = PyTuple_New(0); |
| 98 |
PyErr_Clear(); |
| 99 |
} |
| 100 |
if (children == NULL) { |
| 101 |
children = PyTuple_New(0); |
| 102 |
PyErr_Clear(); |
| 103 |
} |
| 104 |
tmp = self->key; |
| 105 |
Py_INCREF(key); |
| 106 |
self->key = key; |
| 107 |
Py_XDECREF(tmp); |
| 108 |
if (parent != NULL) { |
| 109 |
tmp = self->parent; |
| 110 |
Py_INCREF(parent); |
| 111 |
self->parent = parent; |
| 112 |
Py_XDECREF(tmp); |
| 113 |
} |
| 114 |
if (values != NULL) { |
| 115 |
tmp = self->values; |
| 116 |
Py_INCREF(values); |
| 117 |
self->values = values; |
| 118 |
Py_XDECREF(tmp); |
| 119 |
} |
| 120 |
if (children != NULL) { |
| 121 |
tmp = self->children; |
| 122 |
Py_INCREF(children); |
| 123 |
self->children = children; |
| 124 |
Py_XDECREF(tmp); |
| 125 |
} |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static PyObject *Config_repr(PyObject *s) { |
| 130 |
Config *self = (Config *)s; |
| 131 |
PyObject *ret = NULL; |
| 132 |
static PyObject *node_prefix, *root_prefix, *ending; |
| 133 |
|
| 134 |
/* This is ok because we have the GIL, so this is thread-save by default. */ |
| 135 |
if (node_prefix == NULL) |
| 136 |
node_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config node "); |
| 137 |
if (root_prefix == NULL) |
| 138 |
root_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config root node "); |
| 139 |
if (ending == NULL) |
| 140 |
ending = cpy_string_to_unicode_or_bytes(">"); |
| 141 |
if (node_prefix == NULL || root_prefix == NULL || ending == NULL) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
ret = PyObject_Str(self->key); |
| 145 |
CPY_SUBSTITUTE(PyObject_Repr, ret, ret); |
| 146 |
if (self->parent == NULL || self->parent == Py_None) |
| 147 |
CPY_STRCAT(&ret, root_prefix); |
| 148 |
else |
| 149 |
CPY_STRCAT(&ret, node_prefix); |
| 150 |
CPY_STRCAT(&ret, ending); |
| 151 |
|
| 152 |
return ret; |
| 153 |
} |
| 154 |
|
| 155 |
static int Config_traverse(PyObject *self, visitproc visit, void *arg) { |
| 156 |
Config *c = (Config *)self; |
| 157 |
Py_VISIT(c->parent); |
| 158 |
Py_VISIT(c->key); |
| 159 |
Py_VISIT(c->values); |
| 160 |
Py_VISIT(c->children); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
static int Config_clear(PyObject *self) { |
| 165 |
Config *c = (Config *)self; |
| 166 |
Py_CLEAR(c->parent); |
| 167 |
Py_CLEAR(c->key); |
| 168 |
Py_CLEAR(c->values); |
| 169 |
Py_CLEAR(c->children); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
static void Config_dealloc(PyObject *self) { |
| 174 |
Config_clear(self); |
| 175 |
self->ob_type->tp_free(self); |
| 176 |
} |
| 177 |
|
| 178 |
static PyMemberDef Config_members[] = { |
| 179 |
{"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc}, |
| 180 |
{"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc}, |
| 181 |
{"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc}, |
| 182 |
{"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc}, |
| 183 |
{NULL}}; |
| 184 |
|
| 185 |
PyTypeObject ConfigType = { |
| 186 |
CPY_INIT_TYPE "collectd.Config", /* tp_name */ |
| 187 |
sizeof(Config), /* tp_basicsize */ |
| 188 |
0, /* Will be filled in later */ |
| 189 |
Config_dealloc, /* tp_dealloc */ |
| 190 |
0, /* tp_print */ |
| 191 |
0, /* tp_getattr */ |
| 192 |
0, /* tp_setattr */ |
| 193 |
0, /* tp_compare */ |
| 194 |
Config_repr, /* tp_repr */ |
| 195 |
0, /* tp_as_number */ |
| 196 |
0, /* tp_as_sequence */ |
| 197 |
0, /* tp_as_mapping */ |
| 198 |
0, /* tp_hash */ |
| 199 |
0, /* tp_call */ |
| 200 |
0, /* tp_str */ |
| 201 |
0, /* tp_getattro */ |
| 202 |
0, /* tp_setattro */ |
| 203 |
0, /* tp_as_buffer */ |
| 204 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 205 |
config_doc, /* tp_doc */ |
| 206 |
Config_traverse, /* tp_traverse */ |
| 207 |
Config_clear, /* tp_clear */ |
| 208 |
0, /* tp_richcompare */ |
| 209 |
0, /* tp_weaklistoffset */ |
| 210 |
0, /* tp_iter */ |
| 211 |
0, /* tp_iternext */ |
| 212 |
0, /* tp_methods */ |
| 213 |
Config_members, /* tp_members */ |
| 214 |
0, /* tp_getset */ |
| 215 |
0, /* tp_base */ |
| 216 |
0, /* tp_dict */ |
| 217 |
0, /* tp_descr_get */ |
| 218 |
0, /* tp_descr_set */ |
| 219 |
0, /* tp_dictoffset */ |
| 220 |
Config_init, /* tp_init */ |
| 221 |
0, /* tp_alloc */ |
| 222 |
Config_new /* tp_new */ |
| 223 |
}; |