d794c9a8a1556c223149289b04943472e789bede
[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, "O|OOO", kwlist,
78                         &key, &parent, &values, &children))
79                 return -1;
80         
81         if (!IS_BYTES_OR_UNICODE(key)) {
82                 PyErr_SetString(PyExc_TypeError, "argument 1 must be str");
83                 Py_XDECREF(parent);
84                 Py_XDECREF(values);
85                 Py_XDECREF(children);
86                 return -1;
87         }
88         if (values == NULL) {
89                 values = PyTuple_New(0);
90                 PyErr_Clear();
91         }
92         if (children == NULL) {
93                 children = PyTuple_New(0);
94                 PyErr_Clear();
95         }
96         tmp = self->key;
97         Py_INCREF(key);
98         self->key = key;
99         Py_XDECREF(tmp);
100         if (parent != NULL) {
101                 tmp = self->parent;
102                 Py_INCREF(parent);
103                 self->parent = parent;
104                 Py_XDECREF(tmp);
105         }
106         if (values != NULL) {
107                 tmp = self->values;
108                 Py_INCREF(values);
109                 self->values = values;
110                 Py_XDECREF(tmp);
111         }
112         if (children != NULL) {
113                 tmp = self->children;
114                 Py_INCREF(children);
115                 self->children = children;
116                 Py_XDECREF(tmp);
117         }
118         return 0;
119 }
120
121 static PyObject *Config_repr(PyObject *s) {
122         Config *self = (Config *) s;
123         PyObject *name, *tmp, *ret = NULL;
124         static PyObject *node_prefix = NULL, *root_prefix = NULL, *ending = NULL;
125         
126         /* This is ok because we have the GIL, so this is thread-save by default. */
127         if (node_prefix == NULL)
128                 node_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config node '");
129         if (root_prefix == NULL)
130                 root_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config root node '");
131         if (ending == NULL)
132                 ending = cpy_string_to_unicode_or_bytes("'>");
133         if (node_prefix == NULL || root_prefix == NULL || ending == NULL)
134                 return NULL;
135         
136         name = PyObject_Str(self->key);
137         if (name == NULL)
138                 return NULL;
139
140         if (self->parent == NULL || self->parent == Py_None)
141                 tmp = CPY_STRCAT(root_prefix, name);
142         else
143                 tmp = CPY_STRCAT(node_prefix, name);
144         
145         Py_DECREF(name);
146         if (tmp != NULL)
147                 ret = CPY_STRCAT(tmp, ending);
148         Py_DECREF(tmp);
149         
150         return ret;
151 }
152
153 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
154         Config *c = (Config *) self;
155         Py_VISIT(c->parent);
156         Py_VISIT(c->key);
157         Py_VISIT(c->values);
158         Py_VISIT(c->children);
159         return 0;}
160
161 static int Config_clear(PyObject *self) {
162         Config *c = (Config *) self;
163         Py_CLEAR(c->parent);
164         Py_CLEAR(c->key);
165         Py_CLEAR(c->values);
166         Py_CLEAR(c->children);
167         return 0;
168 }
169
170 static void Config_dealloc(PyObject *self) {
171         Config_clear(self);
172         self->ob_type->tp_free(self);
173 }
174
175 static PyMemberDef Config_members[] = {
176         {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
177         {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
178         {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
179         {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
180         {NULL}
181 };
182
183 PyTypeObject ConfigType = {
184         CPY_INIT_TYPE
185         "collectd.Config",         /* tp_name */
186         sizeof(Config),            /* tp_basicsize */
187         0,                         /* Will be filled in later */
188         Config_dealloc,            /* tp_dealloc */
189         0,                         /* tp_print */
190         0,                         /* tp_getattr */
191         0,                         /* tp_setattr */
192         0,                         /* tp_compare */
193         Config_repr,               /* tp_repr */
194         0,                         /* tp_as_number */
195         0,                         /* tp_as_sequence */
196         0,                         /* tp_as_mapping */
197         0,                         /* tp_hash */
198         0,                         /* tp_call */
199         0,                         /* tp_str */
200         0,                         /* tp_getattro */
201         0,                         /* tp_setattro */
202         0,                         /* tp_as_buffer */
203         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
204         config_doc,                /* tp_doc */
205         Config_traverse,           /* tp_traverse */
206         Config_clear,              /* tp_clear */
207         0,                         /* tp_richcompare */
208         0,                         /* tp_weaklistoffset */
209         0,                         /* tp_iter */
210         0,                         /* tp_iternext */
211         0,                         /* tp_methods */
212         Config_members,            /* tp_members */
213         0,                         /* tp_getset */
214         0,                         /* tp_base */
215         0,                         /* tp_dict */
216         0,                         /* tp_descr_get */
217         0,                         /* tp_descr_set */
218         0,                         /* tp_dictoffset */
219         Config_init,               /* tp_init */
220         0,                         /* tp_alloc */
221         Config_new                 /* tp_new */
222 };
223