src/pyconfig.c: Make document strings non-const.
[collectd.git] / src / pyconfig.c
1 #include <Python.h>
2 #include <structmember.h>
3
4 #include "collectd.h"
5 #include "common.h"
6
7 #include "cpython.h"
8
9 static char config_doc[] = "This represents a piece of collectd's config file.\n"
10                 "It is passed to scripts with config callbacks (see \"register_config\")\n"
11                 "and is of little use if created somewhere else.\n"
12                 "\n"
13                 "It has no methods beyond the bare minimum and only exists for its\n"
14                 "data members";
15
16 static char parent_doc[] = "This represents the parent of this node. On the root node\n"
17                 "of the config tree it will be None.\n";
18
19 static char key_doc[] = "This is the keyword of this item, ie the first word of any\n"
20                 "given line in the config file. It will always be a string.\n";
21
22 static char values_doc[] = "This is a tuple (which might be empty) of all value, ie words\n"
23                 "following the keyword in any given line in the config file.\n"
24                 "\n"
25                 "Every item in this tuple will be either a string or a float or a bool,\n"
26                 "depending on the contents of the configuration file.\n";
27
28 static char children_doc[] = "This is a tuple of child nodes. For most nodes this will be\n"
29                 "empty. If this node represents a block instead of a single line of the config\n"
30                 "file it will contain all nodes in this block.\n";
31
32 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
33         Config *self;
34         
35         self = (Config *) type->tp_alloc(type, 0);
36         if (self == NULL)
37                 return NULL;
38         
39         self->parent = NULL;
40         self->key = NULL;
41         self->values = NULL;
42         self->children = NULL;
43         return (PyObject *) self;
44 }
45
46 static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
47         PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
48         Config *self = (Config *) s;
49         static char *kwlist[] = {"key", "parent", "values", "children", NULL};
50         
51         if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist,
52                         &key, &parent, &values, &children))
53                 return -1;
54         
55         if (values == NULL) {
56                 values = PyTuple_New(0);
57                 PyErr_Clear();
58         }
59         if (children == NULL) {
60                 children = PyTuple_New(0);
61                 PyErr_Clear();
62         }
63         tmp = self->key;
64         Py_INCREF(key);
65         self->key = key;
66         Py_XDECREF(tmp);
67         if (parent != NULL) {
68                 tmp = self->parent;
69                 Py_INCREF(parent);
70                 self->parent = parent;
71                 Py_XDECREF(tmp);
72         }
73         if (values != NULL) {
74                 tmp = self->values;
75                 Py_INCREF(values);
76                 self->values = values;
77                 Py_XDECREF(tmp);
78         }
79         if (children != NULL) {
80                 tmp = self->children;
81                 Py_INCREF(children);
82                 self->children = children;
83                 Py_XDECREF(tmp);
84         }
85         return 0;
86 }
87
88 static PyObject *Config_repr(PyObject *s) {
89         Config *self = (Config *) s;
90         
91         return PyString_FromFormat("<collectd.Config %snode %s>", self->parent == Py_None ? "root " : "", PyString_AsString(PyObject_Str(self->key)));
92 }
93
94 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
95         Config *c = (Config *) self;
96         Py_VISIT(c->parent);
97         Py_VISIT(c->key);
98         Py_VISIT(c->values);
99         Py_VISIT(c->children);
100         return 0;
101 }
102
103 static int Config_clear(PyObject *self) {
104         Config *c = (Config *) self;
105         Py_CLEAR(c->parent);
106         Py_CLEAR(c->key);
107         Py_CLEAR(c->values);
108         Py_CLEAR(c->children);
109         return 0;
110 }
111
112 static void Config_dealloc(PyObject *self) {
113         Config_clear(self);
114         self->ob_type->tp_free(self);
115 }
116
117 static PyMemberDef Config_members[] = {
118         {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
119         {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
120         {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
121         {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
122         {NULL}
123 };
124
125 PyTypeObject ConfigType = {
126         PyObject_HEAD_INIT(NULL)
127         0,                         /* Always 0 */
128         "collectd.Config",         /* tp_name */
129         sizeof(Config),            /* tp_basicsize */
130         0,                         /* Will be filled in later */
131         Config_dealloc,            /* tp_dealloc */
132         0,                         /* tp_print */
133         0,                         /* tp_getattr */
134         0,                         /* tp_setattr */
135         0,                         /* tp_compare */
136         Config_repr,               /* tp_repr */
137         0,                         /* tp_as_number */
138         0,                         /* tp_as_sequence */
139         0,                         /* tp_as_mapping */
140         0,                         /* tp_hash */
141         0,                         /* tp_call */
142         0,                         /* tp_str */
143         0,                         /* tp_getattro */
144         0,                         /* tp_setattro */
145         0,                         /* tp_as_buffer */
146         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
147         config_doc,                /* tp_doc */
148         Config_traverse,           /* tp_traverse */
149         Config_clear,              /* tp_clear */
150         0,                         /* tp_richcompare */
151         0,                         /* tp_weaklistoffset */
152         0,                         /* tp_iter */
153         0,                         /* tp_iternext */
154         0,                         /* tp_methods */
155         Config_members,            /* tp_members */
156         0,                         /* tp_getset */
157         0,                         /* tp_base */
158         0,                         /* tp_dict */
159         0,                         /* tp_descr_get */
160         0,                         /* tp_descr_set */
161         0,                         /* tp_dictoffset */
162         Config_init,               /* tp_init */
163         0,                         /* tp_alloc */
164         Config_new                 /* tp_new */
165 };
166