File:  [ELWIX - Embedded LightWeight unIX -] / gpl / axl / py-axl / py_axl_doc.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Fri Feb 17 12:50:03 2012 UTC (12 years, 4 months ago) by misho
Branches: axl, MAIN
CVS tags: HEAD, AXL0_6_7
version 0.6.7

    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: 		__axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "reporting root node (doc ref: %p, ref count: %d)",
  136: 			   self, self->ob_refcnt);
  137: 		return py_axl_node_create (axl_doc_get_root (self->doc), axl_false, o);
  138: 	}
  139: 
  140: 	/* first implement generic attr already defined */
  141: 	result = PyObject_GenericGetAttr (o, attr_name);
  142: 	if (result)
  143: 		return result;
  144: 	
  145: 	return NULL;
  146: }
  147: 
  148: /** 
  149:  * @brief Implements attribute set operation.
  150:  */
  151: int py_axl_doc_set_attr (PyObject *o, PyObject *attr_name, PyObject *v)
  152: {
  153: 	const char      * attr    = NULL;
  154: 	PyAxlDoc        * self    = (PyAxlDoc *) o; 
  155: 	PyObject        * py_node = NULL;
  156: 
  157: 	/* now implement other attributes */
  158: 	if (! PyArg_Parse (attr_name, "s", &attr))
  159: 		return -1;
  160: 
  161: 	if (axl_cmp (attr, "root")) {
  162: 		if (! PyArg_Parse (v, "O", &py_node))
  163: 			return -1;
  164: 
  165: 		/* configure the node */
  166: 		axl_doc_set_root (self->doc, py_axl_node_get (py_node));
  167: 
  168: 		/* set the node to not dealloc internal node */
  169: 		py_axl_node_set_dealloc (py_node, axl_false);
  170: 
  171: 		return 0;
  172: 	}
  173: 
  174: 	/* now implement generic setter */
  175: 	return PyObject_GenericSetAttr (o, attr_name, v);
  176: }
  177: 
  178: static PyObject * py_axl_doc_get_node (PyObject * _self, PyObject * args)
  179: {
  180: 	PyAxlDoc  * self = (PyAxlDoc *) _self;
  181: 	const char * path = NULL;
  182: 	axlNode    * node = NULL;
  183: 
  184: 	/* parse and check result */
  185: 	if (! PyArg_ParseTuple (args, "s", &path))
  186: 		return NULL;
  187: 	
  188: 	/* get next called */
  189: 	node = axl_doc_get (self->doc, path);
  190: 	if (node == NULL) {
  191: 		Py_INCREF (Py_None);
  192: 		return Py_None;
  193: 	} /* end if */
  194: 	
  195: 	/* create node result */
  196: 	return py_axl_node_create (node, axl_false, _self);
  197: }
  198: 
  199: static PyObject * py_axl_doc_dump (PyObject * _self, PyObject * args, PyObject * kwds)
  200: {
  201: 	PyAxlDoc   * self    = (PyAxlDoc *) _self;
  202: 	int          tabular = 0;
  203: 	char       * content = NULL;
  204: 	int          size    = 0;
  205: 	PyObject   * result;
  206: 	
  207: 
  208: 	/* parse and check result */
  209: 	if (PyTuple_Size (args) == 0) {
  210: 		/* simple dump operation */
  211: 		/* dump content */
  212: 		if (! axl_doc_dump (self->doc, &content, &size)) {
  213: 			/* failed to dump */
  214: 			PyErr_SetString (PyExc_TypeError, "axl_doc_dump operation failed, unable to produce dumped content");
  215: 			return NULL;
  216: 		} /* end if */
  217: 
  218: 		/* create the tuple */
  219: 		result = PyTuple_New (2);
  220: 		PyTuple_SetItem (result, 0, Py_BuildValue ("s", content));
  221: 		axl_free (content);
  222: 		PyTuple_SetItem (result, 1, Py_BuildValue ("i", size));
  223: 	} else {
  224: 		/* dump with tabular configuration */
  225: 		if (! PyArg_ParseTuple (args, "i", &tabular))
  226: 			return NULL;
  227: 		
  228: 		/* check tabular value */
  229: 		if (tabular < 0) {
  230: 			PyErr_SetString (PyExc_ValueError, "failed to produce dump operation, received a non-positive (including 0) tabular value");
  231: 			return NULL;
  232: 		} /* end if */
  233: 		
  234: 		/* dump content with tabular configured */
  235: 		if (! axl_doc_dump_pretty (self->doc, &content, &size, tabular)) {
  236: 			/* failed to dump */
  237: 			PyErr_SetString (PyExc_TypeError, "axl_doc_dump operation failed, unable to produce dumped content");
  238: 			return NULL;
  239: 		} /* end if */
  240: 
  241: 		/* create the tuple */
  242: 		result = PyTuple_New (2);
  243: 		PyTuple_SetItem (result, 0, Py_BuildValue ("s", content));
  244: 		axl_free (content);
  245: 		PyTuple_SetItem (result, 1, Py_BuildValue ("i", size));
  246: 	} /* end if */
  247: 
  248: 	/* return tuple */
  249: 	return result;
  250: }
  251: 
  252: static PyObject * py_axl_doc_file_dump (PyObject * _self, PyObject * args, PyObject * kwds)
  253: {
  254: 	PyAxlDoc   * self    = (PyAxlDoc *) _self;
  255: 	int          tabular = 0;
  256: 	char       * file    = NULL;
  257: 
  258: 	/* parse and check result */
  259: 	if (PyTuple_Size (args) == 1) {
  260: 
  261: 		/* dump with tabular configuration */
  262: 		if (! PyArg_ParseTuple (args, "s", &file))
  263: 			return NULL;
  264: 
  265: 		/* simple dump operation */
  266: 		/* dump content */
  267: 		if (! axl_doc_dump_to_file (self->doc, file)) {
  268: 			/* failed to dump */
  269: 			PyErr_SetString (PyExc_TypeError, "axl_doc_dump_to_file operation failed, unable to produce dumped content");
  270: 			return NULL;
  271: 		} /* end if */
  272: 		
  273: 	} else if (PyTuple_Size (args) == 2) {
  274: 
  275: 		/* dump with tabular configuration */
  276: 		if (! PyArg_ParseTuple (args, "si", &file, &tabular))
  277: 			return NULL;
  278: 
  279: 		/* simple dump operation */
  280: 		/* dump content */
  281: 		if (! axl_doc_dump_pretty_to_file (self->doc, file, tabular)) {
  282: 			/* failed to dump */
  283: 			PyErr_SetString (PyExc_TypeError, "axl_doc_dump_pretty_to_file operation failed, unable to produce dumped content");
  284: 			return NULL;
  285: 		} /* end if */
  286: 
  287: 	} /* end if */
  288: 
  289: 	/* return ok operation */
  290: 	return Py_BuildValue ("i", 1);
  291: }
  292: 
  293: static PyObject * py_axl_doc_has_pi (PyObject * _self, PyObject * args)
  294: {
  295: 	PyAxlDoc  * self      = (PyAxlDoc *) _self;
  296: 	char      * pi_name   = NULL;
  297: 
  298: 	/* parse and check result */
  299: 	if (! PyArg_ParseTuple (args, "s", &pi_name))
  300: 		return NULL;
  301: 	
  302: 	return Py_BuildValue ("i", axl_doc_has_pi_target (self->doc, pi_name));
  303: }
  304: 
  305: static PyObject * py_axl_doc_pi_value (PyObject * _self, PyObject * args)
  306: {
  307: 	PyAxlDoc  * self    = (PyAxlDoc *) _self;
  308: 	char      * pi_name = NULL;
  309: 
  310: 	/* parse and check result */
  311: 	if (! PyArg_ParseTuple (args, "s", &pi_name))
  312: 		return NULL;
  313: 	
  314: 	return Py_BuildValue ("z", axl_doc_get_pi_target_content (self->doc, pi_name));
  315: }
  316: 
  317: static PyMethodDef py_axl_doc_methods[] = { 
  318: 	/* next_called */
  319: 	{"get", (PyCFunction) py_axl_doc_get_node, METH_VARARGS,
  320: 	 "Allows to get a node found at a particular path (method wrapper of axl_doc_get)"},
  321: 	/* next_called */
  322: 	{"dump", (PyCFunction) py_axl_doc_dump, METH_VARARGS | METH_KEYWORDS,
  323: 	 "Allows to perform a dump operation returning the content and the size that result in a tuple."},
  324: 	{"file_dump", (PyCFunction) py_axl_doc_file_dump, METH_VARARGS | METH_KEYWORDS,
  325: 	 "Allows to perform a dump operation sending the content to a file."},
  326: 	/* has_pi */
  327: 	{"has_pi", (PyCFunction) py_axl_doc_has_pi, METH_VARARGS,
  328: 	 "Allows to check if the provided doc has the given process instruction."},
  329: 	/* pi */
  330: 	{"pi", (PyCFunction) py_axl_doc_pi_value, METH_VARARGS,
  331: 	 "Allows to get the value of the given process instruction on the provided doc."},
  332:  	{NULL, NULL, 0, NULL}  
  333: }; 
  334: 
  335: static PyObject * py_axl_doc_str (PyObject * _self)
  336: {
  337: 	char      * dump   = NULL;
  338: 	int         size   = 0;
  339: 	PyAxlDoc  * self   = (PyAxlDoc *) _self;
  340: 	PyObject  * result = NULL;
  341: 
  342: 	if (self == NULL || self->doc == NULL) {
  343: 		Py_INCREF (Py_None);
  344: 		return Py_None;
  345: 	}
  346: 	
  347: 	if (! axl_doc_dump_pretty (self->doc, &dump, &size, 4)) {
  348: 		Py_INCREF (Py_None);
  349: 		return Py_None;
  350: 	}
  351: 
  352: 	result = Py_BuildValue ("z", dump);
  353: 	axl_free (dump);
  354: 	return result;
  355: }
  356: 
  357: static PyTypeObject PyAxlDocType = {
  358:     PyObject_HEAD_INIT(NULL)
  359:     0,                         /* ob_size*/
  360:     "axl.Doc",                 /* tp_name*/
  361:     sizeof(PyAxlDoc),       /* tp_basicsize*/
  362:     0,                         /* tp_itemsize*/
  363:     (destructor)py_axl_doc_dealloc, /* tp_dealloc*/
  364:     0,                         /* tp_print*/
  365:     0,                         /* tp_getattr*/
  366:     0,                         /* tp_setattr*/
  367:     0,                         /* tp_compare*/
  368:     0,                         /* tp_repr*/
  369:     0,                         /* tp_as_number*/
  370:     0,                         /* tp_as_sequence*/
  371:     0,                         /* tp_as_mapping*/
  372:     0,                         /* tp_hash */
  373:     0,                         /* tp_call*/
  374:     py_axl_doc_str,            /* tp_str*/
  375:     py_axl_doc_get_attr,    /* tp_getattro*/
  376:     py_axl_doc_set_attr,    /* tp_setattro*/
  377:     0,                         /* tp_as_buffer*/
  378:     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /* tp_flags*/
  379:     "Document object; wrapper of axlDoc API type",           /* tp_doc */
  380:     0,		               /* tp_traverse */
  381:     0,		               /* tp_clear */
  382:     0,		               /* tp_richcompare */
  383:     0,		               /* tp_weaklistoffset */
  384:     0,		               /* tp_iter */
  385:     0,		               /* tp_iternext */
  386:     py_axl_doc_methods,     /* tp_methods */
  387:     0, /* py_axl_doc_members, */     /* tp_members */
  388:     0,                         /* tp_getset */
  389:     0,                         /* tp_base */
  390:     0,                         /* tp_dict */
  391:     0,                         /* tp_descr_get */
  392:     0,                         /* tp_descr_set */
  393:     0,                         /* tp_dictoffset */
  394:     (initproc)py_axl_doc_init_type,      /* tp_init */
  395:     0,                         /* tp_alloc */
  396:     py_axl_doc_new,         /* tp_new */
  397: 
  398: };
  399: 
  400: 
  401: /** 
  402:  * @brief Allows to check if the PyObject received represents a
  403:  * PyAxlDoc reference.
  404:  */
  405: axl_bool             py_axl_doc_check    (PyObject          * obj)
  406: {
  407: 	/* check null references */
  408: 	if (obj == NULL)
  409: 		return axl_false;
  410: 
  411: 	/* return check result */
  412: 	return PyObject_TypeCheck (obj, &PyAxlDocType);
  413: }
  414: 
  415: PyObject   * py_axl_doc_create    (axlDoc * doc, axl_bool finish_on_gc)
  416: {
  417: 	/* return a new instance */
  418: 	PyAxlDoc * obj = (PyAxlDoc *) PyObject_CallObject ((PyObject *) &PyAxlDocType, NULL); 
  419: 
  420: 	/* check ref created */
  421: 	if (obj == NULL) {
  422: 		__axl_log (LOG_DOMAIN, AXL_LEVEL_CRITICAL, "Failed to create PyAxlDoc object, returning NULL");
  423: 		return NULL;
  424: 	} /* end if */
  425: 
  426: 	/* configure finish handling */
  427: 	obj->finish_on_gc = finish_on_gc;
  428: 
  429: 	/* set doc if defined */
  430: 	if (doc) {
  431: 		__axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "Created axl.Doc reference (%p), refcount: %d",
  432: 			   obj, obj->ob_refcnt);
  433: 		/* clear previous reference */
  434: 		axl_doc_free (obj->doc);
  435: 		obj->doc = doc;
  436: 	}
  437: 
  438: 	return __PY_OBJECT (obj);
  439: }
  440: 
  441: axlDoc     * py_axl_doc_get      (PyObject * doc)
  442: {
  443: 	PyAxlDoc * _doc = (PyAxlDoc *) doc;
  444: 	
  445: 	if (! py_axl_doc_check (doc))
  446: 		return NULL;
  447: 	/* return doc reference */
  448: 	return _doc->doc;
  449: }
  450: 
  451: void        init_axl_doc      (PyObject * module)
  452: {
  453: 	/* register type */
  454: 	if (PyType_Ready(&PyAxlDocType) < 0)
  455: 		return;
  456: 	
  457: 	Py_INCREF (&PyAxlDocType);
  458: 	PyModule_AddObject(module, "Doc", (PyObject *)&PyAxlDocType);
  459: 
  460: }
  461: 
  462: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>