Merge remote-tracking branch 'origin/pr/1346'
[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
32 #include "common.h"
33
34 #include "cpython.h"
35
36 static char config_doc[] = "This represents a piece of collectd's config file.\n"
37                 "It is passed to scripts with config callbacks (see \"register_config\")\n"
38                 "and is of little use if created somewhere else.\n"
39                 "\n"
40                 "It has no methods beyond the bare minimum and only exists for its\n"
41                 "data members";
42
43 static char parent_doc[] = "This represents the parent of this node. On the root node\n"
44                 "of the config tree it will be None.\n";
45
46 static char key_doc[] = "This is the keyword of this item, ie the first word of any\n"
47                 "given line in the config file. It will always be a string.\n";
48
49 static char values_doc[] = "This is a tuple (which might be empty) of all value, ie words\n"
50                 "following the keyword in any given line in the config file.\n"
51                 "\n"
52                 "Every item in this tuple will be either a string or a float or a bool,\n"
53                 "depending on the contents of the configuration file.\n";
54
55 static char children_doc[] = "This is a tuple of child nodes. For most nodes this will be\n"
56                 "empty. If this node represents a block instead of a single line of the config\n"
57                 "file it will contain all nodes in this block.\n";
58
59 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
60         Config *self;
61
62         self = (Config *) type->tp_alloc(type, 0);
63         if (self == NULL)
64                 return NULL;
65
66         self->parent = NULL;
67         self->key = NULL;
68         self->values = NULL;
69         self->children = NULL;
70         return (PyObject *) self;
71 }
72
73 static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
74         PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
75         Config *self = (Config *) s;
76         static char *kwlist[] = {"key", "parent", "values", "children", NULL};
77
78         if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist,
79                         &key, &parent, &values, &children))
80                 return -1;
81
82         if (!IS_BYTES_OR_UNICODE(key)) {
83                 PyErr_SetString(PyExc_TypeError, "argument 1 must be str");
84                 Py_XDECREF(parent);
85                 Py_XDECREF(values);
86                 Py_XDECREF(children);
87                 return -1;
88         }
89         if (values == NULL) {
90                 values = PyTuple_New(0);
91                 PyErr_Clear();
92         }
93         if (children == NULL) {
94                 children = PyTuple_New(0);
95                 PyErr_Clear();
96         }
97         tmp = self->key;
98         Py_INCREF(key);
99         self->key = key;
100         Py_XDECREF(tmp);
101         if (parent != NULL) {
102                 tmp = self->parent;
103                 Py_INCREF(parent);
104                 self->parent = parent;
105                 Py_XDECREF(tmp);
106         }
107         if (values != NULL) {
108                 tmp = self->values;
109                 Py_INCREF(values);
110                 self->values = values;
111                 Py_XDECREF(tmp);
112         }
113         if (children != NULL) {
114                 tmp = self->children;
115                 Py_INCREF(children);
116                 self->children = children;
117                 Py_XDECREF(tmp);
118         }
119         return 0;
120 }
121
122 static PyObject *Config_repr(PyObject *s) {
123         Config *self = (Config *) s;
124         PyObject *ret = NULL;
125         static PyObject *node_prefix = NULL, *root_prefix = NULL, *ending = NULL;
126
127         /* This is ok because we have the GIL, so this is thread-save by default. */
128         if (node_prefix == NULL)
129                 node_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config node ");
130         if (root_prefix == NULL)
131                 root_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config root node ");
132         if (ending == NULL)
133                 ending = cpy_string_to_unicode_or_bytes(">");
134         if (node_prefix == NULL || root_prefix == NULL || ending == NULL)
135                 return NULL;
136
137         ret = PyObject_Str(self->key);
138         CPY_SUBSTITUTE(PyObject_Repr, ret, ret);
139         if (self->parent == NULL || self->parent == Py_None)
140                 CPY_STRCAT(&ret, root_prefix);
141         else
142                 CPY_STRCAT(&ret, node_prefix);
143         CPY_STRCAT(&ret, ending);
144
145         return ret;
146 }
147
148 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
149         Config *c = (Config *) self;
150         Py_VISIT(c->parent);
151         Py_VISIT(c->key);
152         Py_VISIT(c->values);
153         Py_VISIT(c->children);
154         return 0;}
155
156 static int Config_clear(PyObject *self) {
157         Config *c = (Config *) self;
158         Py_CLEAR(c->parent);
159         Py_CLEAR(c->key);
160         Py_CLEAR(c->values);
161         Py_CLEAR(c->children);
162         return 0;
163 }
164
165 static void Config_dealloc(PyObject *self) {
166         Config_clear(self);
167         self->ob_type->tp_free(self);
168 }
169
170 static PyMemberDef Config_members[] = {
171         {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
172         {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
173         {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
174         {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
175         {NULL}
176 };
177
178 PyTypeObject ConfigType = {
179         CPY_INIT_TYPE
180         "collectd.Config",         /* tp_name */
181         sizeof(Config),            /* tp_basicsize */
182         0,                         /* Will be filled in later */
183         Config_dealloc,            /* tp_dealloc */
184         0,                         /* tp_print */
185         0,                         /* tp_getattr */
186         0,                         /* tp_setattr */
187         0,                         /* tp_compare */
188         Config_repr,               /* tp_repr */
189         0,                         /* tp_as_number */
190         0,                         /* tp_as_sequence */
191         0,                         /* tp_as_mapping */
192         0,                         /* tp_hash */
193         0,                         /* tp_call */
194         0,                         /* tp_str */
195         0,                         /* tp_getattro */
196         0,                         /* tp_setattro */
197         0,                         /* tp_as_buffer */
198         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
199         config_doc,                /* tp_doc */
200         Config_traverse,           /* tp_traverse */
201         Config_clear,              /* tp_clear */
202         0,                         /* tp_richcompare */
203         0,                         /* tp_weaklistoffset */
204         0,                         /* tp_iter */
205         0,                         /* tp_iternext */
206         0,                         /* tp_methods */
207         Config_members,            /* tp_members */
208         0,                         /* tp_getset */
209         0,                         /* tp_base */
210         0,                         /* tp_dict */
211         0,                         /* tp_descr_get */
212         0,                         /* tp_descr_set */
213         0,                         /* tp_dictoffset */
214         Config_init,               /* tp_init */
215         0,                         /* tp_alloc */
216         Config_new                 /* tp_new */
217 };
218