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>
35 static char config_doc[] = "This represents a piece of collectd's config file.\n"
36 "It is passed to scripts with config callbacks (see \"register_config\")\n"
37 "and is of little use if created somewhere else.\n"
39 "It has no methods beyond the bare minimum and only exists for its\n"
42 static char parent_doc[] = "This represents the parent of this node. On the root node\n"
43 "of the config tree it will be None.\n";
45 static char key_doc[] = "This is the keyword of this item, ie the first word of any\n"
46 "given line in the config file. It will always be a string.\n";
48 static char values_doc[] = "This is a tuple (which might be empty) of all value, ie words\n"
49 "following the keyword in any given line in the config file.\n"
51 "Every item in this tuple will be either a string or a float or a bool,\n"
52 "depending on the contents of the configuration file.\n";
54 static char children_doc[] = "This is a tuple of child nodes. For most nodes this will be\n"
55 "empty. If this node represents a block instead of a single line of the config\n"
56 "file it will contain all nodes in this block.\n";
58 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
61 self = (Config *) type->tp_alloc(type, 0);
68 self->children = NULL;
69 return (PyObject *) self;
72 static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
73 PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
74 Config *self = (Config *) s;
75 static char *kwlist[] = {"key", "parent", "values", "children", NULL};
77 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist,
78 &key, &parent, &values, &children))
81 if (!IS_BYTES_OR_UNICODE(key)) {
82 PyErr_SetString(PyExc_TypeError, "argument 1 must be str");
89 values = PyTuple_New(0);
92 if (children == NULL) {
93 children = PyTuple_New(0);
100 if (parent != NULL) {
103 self->parent = parent;
106 if (values != NULL) {
109 self->values = values;
112 if (children != NULL) {
113 tmp = self->children;
115 self->children = children;
121 /*static PyObject *Config_repr(PyObject *s) {
122 Config *self = (Config *) s;
124 return PyString_FromFormat("<collectd.Config %snode %s>", self->parent == Py_None ? "root " : "", PyString_AsString(PyObject_Str(self->key)));
127 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
128 Config *c = (Config *) self;
132 Py_VISIT(c->children);
136 static int Config_clear(PyObject *self) {
137 Config *c = (Config *) self;
141 Py_CLEAR(c->children);
145 static void Config_dealloc(PyObject *self) {
147 self->ob_type->tp_free(self);
150 static PyMemberDef Config_members[] = {
151 {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
152 {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
153 {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
154 {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
158 PyTypeObject ConfigType = {
160 "collectd.Config", /* tp_name */
161 sizeof(Config), /* tp_basicsize */
162 0, /* Will be filled in later */
163 Config_dealloc, /* tp_dealloc */
168 0/*Config_repr*/, /* tp_repr */
169 0, /* tp_as_number */
170 0, /* tp_as_sequence */
171 0, /* tp_as_mapping */
177 0, /* tp_as_buffer */
178 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
179 config_doc, /* tp_doc */
180 Config_traverse, /* tp_traverse */
181 Config_clear, /* tp_clear */
182 0, /* tp_richcompare */
183 0, /* tp_weaklistoffset */
187 Config_members, /* tp_members */
191 0, /* tp_descr_get */
192 0, /* tp_descr_set */
193 0, /* tp_dictoffset */
194 Config_init, /* tp_init */
196 Config_new /* tp_new */