2 * clippy (CLI preparator in python) wrapper for FRR command_graph
3 * Copyright (C) 2016-2017 David Lamparter for NetDEF, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* note: this wrapper is intended to be used as build-time helper. while
21 * it should be generally correct and proper, there may be the occasional
22 * memory leak or SEGV for things that haven't been well-tested.
26 #include "structmember.h"
30 #include "command_graph.h"
34 static PyObject
*graph_to_pyobj(struct wrap_graph
*graph
,
35 struct graph_node
*gn
);
38 * nodes are wrapped as follows:
39 * - instances can only be acquired from a graph
40 * - the same node will return the same wrapper object (they're buffered
42 * - a reference is held onto the graph
43 * - fields are copied for easy access with PyMemberDef
45 struct wrap_graph_node
{
58 struct graph_node
*node
;
59 struct wrap_graph
*wgraph
;
64 * graphs are wrapped as follows:
65 * - they can only be created by parsing a definition string
66 * - there's a table here for the wrapped nodes (nodewrappers), indexed
67 * by "idx" (corresponds to node's position in graph's table of nodes)
68 * - graphs do NOT hold references to nodes (would be circular)
75 struct wrap_graph_node
**nodewrappers
;
78 static PyObject
*refuse_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
80 PyErr_SetString(PyExc_ValueError
,
81 "cannot create instances of this type");
85 #define member(name, type) \
87 (char *)#name, type, offsetof(struct wrap_graph_node, name), \
88 READONLY, (char *)#name " (" #type ")" \
90 static PyMemberDef members_graph_node
[] = {
91 member(allowrepeat
, T_BOOL
), member(type
, T_STRING
),
92 member(deprecated
, T_BOOL
), member(hidden
, T_BOOL
),
93 member(text
, T_STRING
), member(desc
, T_STRING
),
94 member(min
, T_LONGLONG
), member(max
, T_LONGLONG
),
95 member(varname
, T_STRING
), {},
100 * node.next() -- returns list of all "next" nodes.
101 * this will include circles if the graph has them.
103 static PyObject
*graph_node_next(PyObject
*self
, PyObject
*args
)
105 struct wrap_graph_node
*wrap
= (struct wrap_graph_node
*)self
;
109 && ((struct cmd_token
*)wrap
->node
->data
)->type
== END_TKN
)
110 return PyList_New(0);
111 pylist
= PyList_New(vector_active(wrap
->node
->to
));
112 for (size_t i
= 0; i
< vector_active(wrap
->node
->to
); i
++) {
113 struct graph_node
*gn
= vector_slot(wrap
->node
->to
, i
);
114 PyList_SetItem(pylist
, i
, graph_to_pyobj(wrap
->wgraph
, gn
));
120 * node.join() -- return FORK's JOIN node or None
122 static PyObject
*graph_node_join(PyObject
*self
, PyObject
*args
)
124 struct wrap_graph_node
*wrap
= (struct wrap_graph_node
*)self
;
126 if (!wrap
->node
->data
127 || ((struct cmd_token
*)wrap
->node
->data
)->type
== END_TKN
)
130 struct cmd_token
*tok
= wrap
->node
->data
;
131 if (tok
->type
!= FORK_TKN
)
134 return graph_to_pyobj(wrap
->wgraph
, tok
->forkjoin
);
137 static PyMethodDef methods_graph_node
[] = {
138 {"next", graph_node_next
, METH_NOARGS
, "outbound graph edge list"},
139 {"join", graph_node_join
, METH_NOARGS
, "outbound join node"},
142 static void graph_node_wrap_free(void *arg
)
144 struct wrap_graph_node
*wrap
= arg
;
145 wrap
->wgraph
->nodewrappers
[wrap
->idx
] = NULL
;
146 Py_DECREF(wrap
->wgraph
);
149 static PyTypeObject typeobj_graph_node
= {
150 PyVarObject_HEAD_INIT(NULL
, 0).tp_name
= "_clippy.GraphNode",
151 .tp_basicsize
= sizeof(struct wrap_graph_node
),
152 .tp_flags
= Py_TPFLAGS_DEFAULT
,
153 .tp_doc
= "struct graph_node *",
154 .tp_new
= refuse_new
,
155 .tp_free
= graph_node_wrap_free
,
156 .tp_members
= members_graph_node
,
157 .tp_methods
= methods_graph_node
,
160 static PyObject
*graph_to_pyobj(struct wrap_graph
*wgraph
,
161 struct graph_node
*gn
)
163 struct wrap_graph_node
*wrap
;
166 for (i
= 0; i
< vector_active(wgraph
->graph
->nodes
); i
++)
167 if (vector_slot(wgraph
->graph
->nodes
, i
) == gn
)
169 if (i
== vector_active(wgraph
->graph
->nodes
)) {
170 PyErr_SetString(PyExc_ValueError
, "cannot find node in graph");
173 if (wgraph
->nodewrappers
[i
]) {
174 PyObject
*obj
= (PyObject
*)wgraph
->nodewrappers
[i
];
179 wrap
= (struct wrap_graph_node
*)typeobj_graph_node
.tp_alloc(
180 &typeobj_graph_node
, 0);
183 wgraph
->nodewrappers
[i
] = wrap
;
187 wrap
->wgraph
= wgraph
;
190 wrap
->allowrepeat
= false;
192 struct cmd_token
*tok
= gn
->data
;
194 #define item(x) case x: wrap->type = #x; break;
195 item(WORD_TKN
) // words
196 item(VARIABLE_TKN
) // almost anything
197 item(RANGE_TKN
) // integer range
198 item(IPV4_TKN
) // IPV4 addresses
199 item(IPV4_PREFIX_TKN
) // IPV4 network prefixes
200 item(IPV6_TKN
) // IPV6 prefixes
201 item(IPV6_PREFIX_TKN
) // IPV6 network prefixes
202 item(MAC_TKN
) // MAC address
203 item(MAC_PREFIX_TKN
) // MAC address with mask
206 item(FORK_TKN
) item(JOIN_TKN
) item(START_TKN
)
207 item(END_TKN
) default
208 : wrap
->type
= "???";
211 wrap
->deprecated
= (tok
->attr
== CMD_ATTR_DEPRECATED
);
212 wrap
->hidden
= (tok
->attr
== CMD_ATTR_HIDDEN
);
213 wrap
->text
= tok
->text
;
214 wrap
->desc
= tok
->desc
;
215 wrap
->varname
= tok
->varname
;
216 wrap
->min
= tok
->min
;
217 wrap
->max
= tok
->max
;
218 wrap
->allowrepeat
= tok
->allowrepeat
;
221 return (PyObject
*)wrap
;
224 #define member(name, type) \
226 (char *)#name, type, offsetof(struct wrap_graph, name), \
227 READONLY, (char *)#name " (" #type ")" \
229 static PyMemberDef members_graph
[] = {
230 member(definition
, T_STRING
),
235 /* graph.first() - root node */
236 static PyObject
*graph_first(PyObject
*self
, PyObject
*args
)
238 struct wrap_graph
*gwrap
= (struct wrap_graph
*)self
;
239 struct graph_node
*gn
= vector_slot(gwrap
->graph
->nodes
, 0);
240 return graph_to_pyobj(gwrap
, gn
);
243 static PyMethodDef methods_graph
[] = {
244 {"first", graph_first
, METH_NOARGS
, "first graph node"},
247 static PyObject
*graph_parse(PyTypeObject
*type
, PyObject
*args
,
250 static void graph_wrap_free(void *arg
)
252 struct wrap_graph
*wgraph
= arg
;
254 graph_delete_graph(wgraph
->graph
);
255 free(wgraph
->nodewrappers
);
256 free(wgraph
->definition
);
259 static PyTypeObject typeobj_graph
= {
260 PyVarObject_HEAD_INIT(NULL
, 0).tp_name
= "_clippy.Graph",
261 .tp_basicsize
= sizeof(struct wrap_graph
),
262 .tp_flags
= Py_TPFLAGS_DEFAULT
,
263 .tp_doc
= "struct graph *",
264 .tp_new
= graph_parse
,
265 .tp_free
= graph_wrap_free
,
266 .tp_members
= members_graph
,
267 .tp_methods
= methods_graph
,
270 /* top call / entrypoint for python code */
271 static PyObject
*graph_parse(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
273 const char *def
, *doc
= NULL
;
274 struct wrap_graph
*gwrap
;
275 static const char *kwnames
[] = {"cmddef", "doc", NULL
};
277 gwrap
= (struct wrap_graph
*)typeobj_graph
.tp_alloc(&typeobj_graph
, 0);
281 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "s|s", (char **)kwnames
,
285 struct graph
*graph
= graph_new();
286 struct cmd_token
*token
= cmd_token_new(START_TKN
, 0, NULL
, NULL
);
287 graph_new_node(graph
, token
, (void (*)(void *)) & cmd_token_del
);
289 struct cmd_element cmd
= {.string
= def
, .doc
= doc
};
290 cmd_graph_parse(graph
, &cmd
);
291 cmd_graph_names(graph
);
293 gwrap
->graph
= graph
;
294 gwrap
->definition
= strdup(def
);
295 gwrap
->nodewrappers
= calloc(vector_active(graph
->nodes
),
296 sizeof(gwrap
->nodewrappers
[0]));
297 return (PyObject
*)gwrap
;
300 static PyMethodDef clippy_methods
[] = {
301 {"parse", clippy_parse
, METH_VARARGS
, "Parse a C file"},
302 {NULL
, NULL
, 0, NULL
}};
304 #if PY_MAJOR_VERSION >= 3
305 static struct PyModuleDef pymoddef_clippy
= {
306 PyModuleDef_HEAD_INIT
,
308 NULL
, /* docstring */
312 #define modcreate() PyModule_Create(&pymoddef_clippy)
313 #define initret(val) return val;
315 #define modcreate() Py_InitModule("_clippy", clippy_methods)
316 #define initret(val) \
319 Py_FatalError("initialization failure"); \
324 PyMODINIT_FUNC
command_py_init(void)
328 if (PyType_Ready(&typeobj_graph_node
) < 0)
330 if (PyType_Ready(&typeobj_graph
) < 0)
337 Py_INCREF(&typeobj_graph_node
);
338 PyModule_AddObject(pymod
, "GraphNode", (PyObject
*)&typeobj_graph_node
);
339 Py_INCREF(&typeobj_graph
);
340 PyModule_AddObject(pymod
, "Graph", (PyObject
*)&typeobj_graph
);