2 #include <structmember.h>
9 static const 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"
13 "It has no methods beyond the bare minimum and only exists for its\n"
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";
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";
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"
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";
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";
32 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
35 self = (Config *) type->tp_alloc(type, 0);
42 self->children = NULL;
43 return (PyObject *) self;
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};
51 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist,
52 &key, &parent, &values, &children))
56 values = PyTuple_New(0);
59 if (children == NULL) {
60 children = PyTuple_New(0);
70 self->parent = parent;
76 self->values = values;
79 if (children != NULL) {
82 self->children = children;
88 static PyObject *Config_repr(PyObject *s) {
89 Config *self = (Config *) s;
91 return PyString_FromFormat("<collectd.Config %snode %s>", self->parent == Py_None ? "root " : "", PyString_AsString(PyObject_Str(self->key)));
94 static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
95 Config *c = (Config *) self;
99 Py_VISIT(c->children);
103 static int Config_clear(PyObject *self) {
104 Config *c = (Config *) self;
108 Py_CLEAR(c->children);
112 static void Config_dealloc(PyObject *self) {
114 self->ob_type->tp_free(self);
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},
125 PyTypeObject ConfigType = {
126 PyObject_HEAD_INIT(NULL)
128 "collectd.Config", /* tp_name */
129 sizeof(Config), /* tp_basicsize */
130 0, /* Will be filled in later */
131 Config_dealloc, /* tp_dealloc */
136 Config_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
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 */
155 Config_members, /* tp_members */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 Config_init, /* tp_init */
164 Config_new /* tp_new */