Merge branch 'collectd-4.8' into collectd-4.9
[collectd.git] / src / pyconfig.c
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 #include "common.h"
32
33 #include "cpython.h"
34
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"
38                 "\n"
39                 "It has no methods beyond the bare minimum and only exists for its\n"
40                 "data members";
41
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";
44
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";
47
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"
50                 "\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";
53
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";
57
58 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
59         Config *self;
60         
61         self = (Config *) type->tp_alloc(type, 0);
62         if (self == NULL)
63                 return NULL;
64         
65         self->parent = NULL;
66         self->key = NULL;
67         self->values = NULL;
68         self->children = NULL;
69         return (PyObject *) self;
70 }
71
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};
76         
77         if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist,
78                         &key, &parent, &values, &children))
79                 return -1;
80         
81         if (values == NULL) {
82                 values = PyTuple_New(0);
83                 PyErr_Clear();
84         }
85         if (children == NULL) {
86                 children = PyTuple_New(0);
87                 PyErr_Clear();
88         }
89         tmp = self->key;
90         Py_INCREF(key);
91         self->key = key;
92         Py_XDECREF(tmp);
93         if (parent != NULL) {
94                 tmp = self->parent;
95                 Py_INCREF(parent);
96                 self->parent = parent;
97                 Py_XDECREF(tmp);
98         }
99         if (values != NULL) {
100                 tmp = self->values;
101                 Py_INCREF(values);
102                 self->values = values;
103                 Py_XDECREF(tmp);
104         }
105         if (children != NULL) {
106                 tmp = self->children;
107                 Py_INCREF(children);
108                 self->children = children;
109                 Py_XDECREF(tmp);
110         }
111         return 0;
112 }
113
114 static PyObject *Config_repr(PyObject *s) {
115         Config *self = (Config *) s;
116         
117         return PyString_FromFormat("<collectd.Config %snode %s>", self->parent == Py_None ? "root " : "", PyString_AsString(PyObject_Str(self->key)));
118 }
119
120 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
121         Config *c = (Config *) self;
122         Py_VISIT(c->parent);
123         Py_VISIT(c->key);
124         Py_VISIT(c->values);
125         Py_VISIT(c->children);
126         return 0;
127 }
128
129 static int Config_clear(PyObject *self) {
130         Config *c = (Config *) self;
131         Py_CLEAR(c->parent);
132         Py_CLEAR(c->key);
133         Py_CLEAR(c->values);
134         Py_CLEAR(c->children);
135         return 0;
136 }
137
138 static void Config_dealloc(PyObject *self) {
139         Config_clear(self);
140         self->ob_type->tp_free(self);
141 }
142
143 static PyMemberDef Config_members[] = {
144         {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
145         {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
146         {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
147         {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
148         {NULL}
149 };
150
151 PyTypeObject ConfigType = {
152         PyObject_HEAD_INIT(NULL)
153         0,                         /* Always 0 */
154         "collectd.Config",         /* tp_name */
155         sizeof(Config),            /* tp_basicsize */
156         0,                         /* Will be filled in later */
157         Config_dealloc,            /* tp_dealloc */
158         0,                         /* tp_print */
159         0,                         /* tp_getattr */
160         0,                         /* tp_setattr */
161         0,                         /* tp_compare */
162         Config_repr,               /* tp_repr */
163         0,                         /* tp_as_number */
164         0,                         /* tp_as_sequence */
165         0,                         /* tp_as_mapping */
166         0,                         /* tp_hash */
167         0,                         /* tp_call */
168         0,                         /* tp_str */
169         0,                         /* tp_getattro */
170         0,                         /* tp_setattro */
171         0,                         /* tp_as_buffer */
172         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
173         config_doc,                /* tp_doc */
174         Config_traverse,           /* tp_traverse */
175         Config_clear,              /* tp_clear */
176         0,                         /* tp_richcompare */
177         0,                         /* tp_weaklistoffset */
178         0,                         /* tp_iter */
179         0,                         /* tp_iternext */
180         0,                         /* tp_methods */
181         Config_members,            /* tp_members */
182         0,                         /* tp_getset */
183         0,                         /* tp_base */
184         0,                         /* tp_dict */
185         0,                         /* tp_descr_get */
186         0,                         /* tp_descr_set */
187         0,                         /* tp_dictoffset */
188         Config_init,               /* tp_init */
189         0,                         /* tp_alloc */
190         Config_new                 /* tp_new */
191 };
192