Annotation of gpl/axl/py-axl/py_axl_doc.c, revision 1.1.1.1
1.1 misho 1: /**
2: * PyAxl: Axl Library python bindings
3: * Copyright (C) 2009 Advanced Software Production Line, S.L.
4: *
5: * This program is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public License
7: * as published by the Free Software Foundation; either version 2.1
8: * of the License, or (at your option) any later version.
9: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this program; if not, write to the Free
17: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18: * 02111-1307 USA
19: *
20: * You may find a copy of the license under this software is released
21: * at COPYING file. This is LGPL software: you are welcome to develop
22: * proprietary applications using this library without any royalty or
23: * fee but returning back any change, improvement or addition in the
24: * form of source code, project image, documentation patches, etc.
25: *
26: * For commercial support for XML enabled solutions contact us:
27: *
28: * Postal address:
29: * Advanced Software Production Line, S.L.
30: * C/ Antonio Suarez Nº 10,
31: * Edificio Alius A, Despacho 102
32: * Alcalá de Henares 28802 (Madrid)
33: * Spain
34: *
35: * Email address:
36: * info@aspl.es - http://www.aspl.es/axl
37: */
38: #include <py_axl_doc.h>
39:
40: #define LOG_DOMAIN "py-axl-doc"
41:
42: struct _PyAxlDoc {
43: /* header required to initialize python required bits for
44: every python object */
45: PyObject_HEAD
46:
47: /* pointer to the axl doc */
48: axlDoc * doc;
49: /* release the document on gc */
50: axl_bool finish_on_gc;
51: };
52:
53: static int py_axl_doc_init_type (PyAxlDoc *self, PyObject *args, PyObject *kwds)
54: {
55: return 0;
56: }
57:
58: /**
59: * @brief Function used to allocate memory required by the object
60: * axl.Doc
61: */
62: static PyObject * py_axl_doc_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
63: {
64: PyAxlDoc * self;
65: const char * version = NULL;
66: const char * encoding = NULL;
67: axl_bool stand_alone = axl_true;
68:
69: /* now parse arguments */
70: static char *kwlist[] = {"version", "encoding", "standalone", NULL};
71:
72: /* create the object */
73: self = (PyAxlDoc *)type->tp_alloc(type, 0);
74:
75: /* in the case the constructor was called with arguments,
76: * check them */
77: if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ssi", kwlist, &version, &encoding, &stand_alone))
78: return NULL;
79:
80: /* create the document */
81: self->finish_on_gc = axl_true;
82: self->doc = axl_doc_create (version, encoding, stand_alone);
83:
84: /* return reference created */
85: return (PyObject *)self;
86: }
87:
88: /**
89: * @brief Function used to finish and dealloc memory used by the
90: * object axl.Doc
91: */
92: static void py_axl_doc_dealloc (PyAxlDoc* self)
93: {
94: /* free document and nullfy pointer */
95: if (self->finish_on_gc)
96: axl_doc_free (self->doc);
97: self->doc = NULL;
98:
99: /* free the node it self */
100: self->ob_type->tp_free ((PyObject*)self);
101:
102: return;
103: }
104:
105: /**
106: * @brief This function implements the generic attribute getting that
107: * allows to perform complex member resolution (not merely direct
108: * member access).
109: */
110: PyObject * py_axl_doc_get_attr (PyObject *o, PyObject *attr_name) {
111: const char * attr = NULL;
112: PyObject * result;
113: PyAxlDoc * self = (PyAxlDoc *) o;
114:
115: /* now implement other attributes */
116: if (! PyArg_Parse (attr_name, "s", &attr))
117: return NULL;
118:
119: __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "received request to report doc attr name %s (self: %p)",
120: attr, o);
121: if (axl_cmp (attr, "encoding")) {
122: /* encoding */
123: return Py_BuildValue ("s", axl_doc_get_encoding (self->doc));
124: } else if (axl_cmp (attr, "standalone") || axl_cmp (attr, "stand_alone")) {
125: /* standalone */
126: return Py_BuildValue ("i", axl_doc_get_standalone (self->doc));
127: } else if (axl_cmp (attr, "root")) {
128: if (axl_doc_get_root (self->doc) == NULL) {
129: Py_INCREF (Py_None);
130: return Py_None;
131: }
132:
133: /* return root axl node: signaling to not finish the
134: * internal copy once it is collected the reference */
135: return py_axl_node_create (axl_doc_get_root (self->doc), axl_false);
136: }
137:
138: /* first implement generic attr already defined */
139: result = PyObject_GenericGetAttr (o, attr_name);
140: if (result)
141: return result;
142:
143: return NULL;
144: }
145:
146: /**
147: * @brief Implements attribute set operation.
148: */
149: int py_axl_doc_set_attr (PyObject *o, PyObject *attr_name, PyObject *v)
150: {
151: const char * attr = NULL;
152: PyAxlDoc * self = (PyAxlDoc *) o;
153: PyObject * py_node = NULL;
154:
155: /* now implement other attributes */
156: if (! PyArg_Parse (attr_name, "s", &attr))
157: return -1;
158:
159: if (axl_cmp (attr, "root")) {
160: if (! PyArg_Parse (v, "O", &py_node))
161: return -1;
162:
163: /* configure the node */
164: axl_doc_set_root (self->doc, py_axl_node_get (py_node));
165: return 0;
166: }
167:
168: /* now implement generic setter */
169: return PyObject_GenericSetAttr (o, attr_name, v);
170: }
171:
172: static PyObject * py_axl_doc_get_node (PyObject * _self, PyObject * args)
173: {
174: PyAxlDoc * self = (PyAxlDoc *) _self;
175: const char * path = NULL;
176: axlNode * node = NULL;
177:
178: /* parse and check result */
179: if (! PyArg_ParseTuple (args, "s", &path))
180: return NULL;
181:
182: /* get next called */
183: node = axl_doc_get (self->doc, path);
184: if (node == NULL) {
185: Py_INCREF (Py_None);
186: return Py_None;
187: } /* end if */
188:
189: /* create node result */
190: return py_axl_node_create (node, axl_false);
191: }
192:
193: static PyObject * py_axl_doc_dump (PyObject * _self, PyObject * args, PyObject * kwds)
194: {
195: PyAxlDoc * self = (PyAxlDoc *) _self;
196: int tabular = 0;
197: char * content = NULL;
198: int size = 0;
199: PyObject * result;
200:
201:
202: /* parse and check result */
203: if (PyTuple_Size (args) == 0) {
204: /* simple dump operation */
205: /* dump content */
206: if (! axl_doc_dump (self->doc, &content, &size)) {
207: /* failed to dump */
208: PyErr_SetString (PyExc_TypeError, "axl_doc_dump operation failed, unable to produce dumped content");
209: return NULL;
210: } /* end if */
211:
212: /* create the tuple */
213: result = PyTuple_New (2);
214: PyTuple_SetItem (result, 0, Py_BuildValue ("s", content));
215: axl_free (content);
216: PyTuple_SetItem (result, 1, Py_BuildValue ("i", size));
217: } else {
218: /* dump with tabular configuration */
219: if (! PyArg_ParseTuple (args, "i", &tabular))
220: return NULL;
221:
222: /* check tabular value */
223: if (tabular < 0) {
224: PyErr_SetString (PyExc_ValueError, "failed to produce dump operation, received a non-positive (including 0) tabular value");
225: return NULL;
226: } /* end if */
227:
228: /* dump content with tabular configured */
229: if (! axl_doc_dump_pretty (self->doc, &content, &size, tabular)) {
230: /* failed to dump */
231: PyErr_SetString (PyExc_TypeError, "axl_doc_dump operation failed, unable to produce dumped content");
232: return NULL;
233: } /* end if */
234:
235: /* create the tuple */
236: result = PyTuple_New (2);
237: PyTuple_SetItem (result, 0, Py_BuildValue ("s", content));
238: axl_free (content);
239: PyTuple_SetItem (result, 1, Py_BuildValue ("i", size));
240: } /* end if */
241:
242: /* return tuple */
243: return result;
244: }
245:
246: static PyObject * py_axl_doc_file_dump (PyObject * _self, PyObject * args, PyObject * kwds)
247: {
248: PyAxlDoc * self = (PyAxlDoc *) _self;
249: int tabular = 0;
250: char * file = NULL;
251:
252: /* parse and check result */
253: if (PyTuple_Size (args) == 1) {
254:
255: /* dump with tabular configuration */
256: if (! PyArg_ParseTuple (args, "s", &file))
257: return NULL;
258:
259: /* simple dump operation */
260: /* dump content */
261: if (! axl_doc_dump_to_file (self->doc, file)) {
262: /* failed to dump */
263: PyErr_SetString (PyExc_TypeError, "axl_doc_dump_to_file operation failed, unable to produce dumped content");
264: return NULL;
265: } /* end if */
266:
267: } else if (PyTuple_Size (args) == 2) {
268:
269: /* dump with tabular configuration */
270: if (! PyArg_ParseTuple (args, "si", &file, &tabular))
271: return NULL;
272:
273: /* simple dump operation */
274: /* dump content */
275: if (! axl_doc_dump_pretty_to_file (self->doc, file, tabular)) {
276: /* failed to dump */
277: PyErr_SetString (PyExc_TypeError, "axl_doc_dump_pretty_to_file operation failed, unable to produce dumped content");
278: return NULL;
279: } /* end if */
280:
281: } /* end if */
282:
283: /* return ok operation */
284: return Py_BuildValue ("i", 1);
285: }
286:
287: static PyObject * py_axl_doc_has_pi (PyObject * _self, PyObject * args)
288: {
289: PyAxlDoc * self = (PyAxlDoc *) _self;
290: char * pi_name = NULL;
291:
292: /* parse and check result */
293: if (! PyArg_ParseTuple (args, "s", &pi_name))
294: return NULL;
295:
296: return Py_BuildValue ("i", axl_doc_has_pi_target (self->doc, pi_name));
297: }
298:
299: static PyObject * py_axl_doc_pi_value (PyObject * _self, PyObject * args)
300: {
301: PyAxlDoc * self = (PyAxlDoc *) _self;
302: char * pi_name = NULL;
303:
304: /* parse and check result */
305: if (! PyArg_ParseTuple (args, "s", &pi_name))
306: return NULL;
307:
308: return Py_BuildValue ("z", axl_doc_get_pi_target_content (self->doc, pi_name));
309: }
310:
311: static PyMethodDef py_axl_doc_methods[] = {
312: /* next_called */
313: {"get", (PyCFunction) py_axl_doc_get_node, METH_VARARGS,
314: "Allows to get a node found at a particular path (method wrapper of axl_doc_get)"},
315: /* next_called */
316: {"dump", (PyCFunction) py_axl_doc_dump, METH_VARARGS | METH_KEYWORDS,
317: "Allows to perform a dump operation returning the content and the size that result in a tuple."},
318: {"file_dump", (PyCFunction) py_axl_doc_file_dump, METH_VARARGS | METH_KEYWORDS,
319: "Allows to perform a dump operation sending the content to a file."},
320: /* has_pi */
321: {"has_pi", (PyCFunction) py_axl_doc_has_pi, METH_VARARGS,
322: "Allows to check if the provided doc has the given process instruction."},
323: /* pi */
324: {"pi", (PyCFunction) py_axl_doc_pi_value, METH_VARARGS,
325: "Allows to get the value of the given process instruction on the provided doc."},
326: {NULL, NULL, 0, NULL}
327: };
328:
329: static PyTypeObject PyAxlDocType = {
330: PyObject_HEAD_INIT(NULL)
331: 0, /* ob_size*/
332: "axl.Doc", /* tp_name*/
333: sizeof(PyAxlDoc), /* tp_basicsize*/
334: 0, /* tp_itemsize*/
335: (destructor)py_axl_doc_dealloc, /* tp_dealloc*/
336: 0, /* tp_print*/
337: 0, /* tp_getattr*/
338: 0, /* tp_setattr*/
339: 0, /* tp_compare*/
340: 0, /* tp_repr*/
341: 0, /* tp_as_number*/
342: 0, /* tp_as_sequence*/
343: 0, /* tp_as_mapping*/
344: 0, /* tp_hash */
345: 0, /* tp_call*/
346: 0, /* tp_str*/
347: py_axl_doc_get_attr, /* tp_getattro*/
348: py_axl_doc_set_attr, /* tp_setattro*/
349: 0, /* tp_as_buffer*/
350: Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags*/
351: "Document object; wrapper of axlDoc API type", /* tp_doc */
352: 0, /* tp_traverse */
353: 0, /* tp_clear */
354: 0, /* tp_richcompare */
355: 0, /* tp_weaklistoffset */
356: 0, /* tp_iter */
357: 0, /* tp_iternext */
358: py_axl_doc_methods, /* tp_methods */
359: 0, /* py_axl_doc_members, */ /* tp_members */
360: 0, /* tp_getset */
361: 0, /* tp_base */
362: 0, /* tp_dict */
363: 0, /* tp_descr_get */
364: 0, /* tp_descr_set */
365: 0, /* tp_dictoffset */
366: (initproc)py_axl_doc_init_type, /* tp_init */
367: 0, /* tp_alloc */
368: py_axl_doc_new, /* tp_new */
369:
370: };
371:
372:
373: /**
374: * @brief Allows to check if the PyObject received represents a
375: * PyAxlDoc reference.
376: */
377: axl_bool py_axl_doc_check (PyObject * obj)
378: {
379: /* check null references */
380: if (obj == NULL)
381: return axl_false;
382:
383: /* return check result */
384: return PyObject_TypeCheck (obj, &PyAxlDocType);
385: }
386:
387: PyObject * py_axl_doc_create (axlDoc * doc, axl_bool finish_on_gc)
388: {
389: /* return a new instance */
390: PyAxlDoc * obj = (PyAxlDoc *) PyObject_CallObject ((PyObject *) &PyAxlDocType, NULL);
391:
392: /* check ref created */
393: if (obj == NULL) {
394: __axl_log (LOG_DOMAIN, AXL_LEVEL_CRITICAL, "Failed to create PyAxlDoc object, returning NULL");
395: return NULL;
396: } /* end if */
397:
398: /* configure finish handling */
399: obj->finish_on_gc = finish_on_gc;
400:
401: /* set doc if defined */
402: if (doc) {
403: /* clear previous reference */
404: axl_doc_free (obj->doc);
405: obj->doc = doc;
406: }
407:
408: return __PY_OBJECT (obj);
409: }
410:
411: axlDoc * py_axl_doc_get (PyObject * doc)
412: {
413: PyAxlDoc * _doc = (PyAxlDoc *) doc;
414:
415: if (! py_axl_doc_check (doc))
416: return NULL;
417: /* return doc reference */
418: return _doc->doc;
419: }
420:
421: void init_axl_doc (PyObject * module)
422: {
423: /* register type */
424: if (PyType_Ready(&PyAxlDocType) < 0)
425: return;
426:
427: Py_INCREF (&PyAxlDocType);
428: PyModule_AddObject(module, "Doc", (PyObject *)&PyAxlDocType);
429:
430: }
431:
432:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>