2 * collectd - src/pyconfig.c
3 * Copyright (C) 2009 Sven Trenkel
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sven Trenkel <collectd at semidefinite.de>
28 #include <structmember.h>
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"
41 "It has no methods beyond the bare minimum and only exists for its\n"
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";
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";
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"
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";
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 "
63 "file it will contain all nodes in this block.\n";
65 static PyObject *Config_new(PyTypeObject *type, PyObject *args,
69 self = (Config *)type->tp_alloc(type, 0);
76 self->children = NULL;
77 return (PyObject *)self;
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};
85 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &key, &parent,
89 if (!IS_BYTES_OR_UNICODE(key)) {
90 PyErr_SetString(PyExc_TypeError, "argument 1 must be str");
97 values = PyTuple_New(0);
100 if (children == NULL) {
101 children = PyTuple_New(0);
108 if (parent != NULL) {
111 self->parent = parent;
114 if (values != NULL) {
117 self->values = values;
120 if (children != NULL) {
121 tmp = self->children;
123 self->children = children;
129 static PyObject *Config_repr(PyObject *s) {
130 Config *self = (Config *)s;
131 PyObject *ret = NULL;
132 static PyObject *node_prefix = NULL, *root_prefix = NULL, *ending = NULL;
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 ");
140 ending = cpy_string_to_unicode_or_bytes(">");
141 if (node_prefix == NULL || root_prefix == NULL || ending == NULL)
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);
149 CPY_STRCAT(&ret, node_prefix);
150 CPY_STRCAT(&ret, ending);
155 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
156 Config *c = (Config *)self;
160 Py_VISIT(c->children);
164 static int Config_clear(PyObject *self) {
165 Config *c = (Config *)self;
169 Py_CLEAR(c->children);
173 static void Config_dealloc(PyObject *self) {
175 self->ob_type->tp_free(self);
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},
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 */
194 Config_repr, /* tp_repr */
195 0, /* tp_as_number */
196 0, /* tp_as_sequence */
197 0, /* tp_as_mapping */
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 */
213 Config_members, /* tp_members */
217 0, /* tp_descr_get */
218 0, /* tp_descr_set */
219 0, /* tp_dictoffset */
220 Config_init, /* tp_init */
222 Config_new /* tp_new */