Annotation of gpl/axl/py-axl/py_axl_node.c, revision 1.1.1.2

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_node.h>
                     39: 
                     40: #define LOG_DOMAIN "py-axl-node"
                     41: 
                     42: struct _PyAxlNode {
                     43:        /* header required to initialize python required bits for
                     44:           every python object */
                     45:        PyObject_HEAD
                     46: 
                     47:        /* pointer to the axl node */
                     48:        axlNode   * node;
                     49:        axl_bool    finish_on_gc;
1.1.1.2 ! misho      50: 
        !            51:        /* pointer to the parent document */
        !            52:        PyObject  * py_doc;
1.1       misho      53: };
                     54: 
                     55: static int py_axl_node_init_type (PyAxlNode *self, PyObject *args, PyObject *kwds)
                     56: {
                     57:     return 0;
                     58: }
                     59: 
                     60: /** 
                     61:  * @brief Function used to allocate memory required by the object
                     62:  * axl.Node
                     63:  */
                     64: static PyObject * py_axl_node_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
                     65: {
                     66:        PyAxlNode  * self;
                     67:        const char * name = NULL;
                     68: 
                     69:        /* create the object */
                     70:        self = (PyAxlNode *)type->tp_alloc(type, 0);
                     71: 
                     72:        /* parse and get node name */
                     73:        if (! PyArg_ParseTuple (args, "|s", &name))
                     74:                return NULL;
                     75:                
                     76:        /* create the node if received a defined name */
                     77:        if (name != NULL) {
                     78:                self->node         = axl_node_create (name);
                     79:                self->finish_on_gc = axl_true;
                     80:        }
                     81: 
                     82:        return (PyObject *)self;
                     83: }
                     84: 
                     85: /** 
                     86:  * @brief Function used to finish and dealloc memory used by the
                     87:  * object axl.Node
                     88:  */
                     89: static void py_axl_node_dealloc (PyAxlNode* self)
1.1.1.2 ! misho      90: 
        !            91:        
1.1       misho      92: {
                     93: 
1.1.1.2 ! misho      94:        __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "Finishing axl.Node reference %p", self);
        !            95: 
1.1       misho      96:        /* release node on if it is signaled to be released and it was
                     97:         * not configured to be inside a document, which means the
                     98:         * document will take care of this */
                     99:        if (self->finish_on_gc && axl_node_get_doc (self->node) == NULL)
                    100:                axl_node_free (self->node);
                    101:        self->node = NULL;
                    102: 
1.1.1.2 ! misho     103:        /* decrease reference to the document if defined */
        !           104:        if (self->py_doc) {
        !           105:                __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "  Finishing axl.Doc reference %p (%d)",
        !           106:                           self->py_doc, self->py_doc->ob_refcnt);
        !           107:                Py_DECREF (self->py_doc);
        !           108:                self->py_doc = NULL;
        !           109:        }
        !           110: 
1.1       misho     111:        /* free the node it self */
                    112:        self->ob_type->tp_free ((PyObject*)self);
                    113: 
                    114:        return;
                    115: }
                    116: 
                    117: /** 
                    118:  * @brief This function implements the generic attribute getting that
                    119:  * allows to perform complex member resolution (not merely direct
                    120:  * member access).
                    121:  */
                    122: PyObject * py_axl_node_get_attr (PyObject *o, PyObject *attr_name) {
                    123:        const char      * attr = NULL;
                    124:        PyObject        * result;
                    125:        PyAxlNode       * self = (PyAxlNode *) o; 
                    126:        PyObject        * tuple;
                    127:        int               size = 0;
                    128: 
                    129:        /* now implement other attributes */
                    130:        if (! PyArg_Parse (attr_name, "s", &attr))
                    131:                return NULL;
                    132: 
                    133:        __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "received request to report node attr name %s (self: %p)",
                    134:                   attr, o);
                    135: 
                    136:        if (axl_cmp (attr, "name")) {
                    137:                /* name */
                    138:                return Py_BuildValue ("s", axl_node_get_name (self->node));
                    139:        } else if (axl_cmp (attr, "first_child")) {
                    140:                /* first_child */
                    141:                if (axl_node_get_first_child (self->node) == NULL) {
                    142:                        Py_INCREF (Py_None);
                    143:                        return Py_None;
                    144:                } 
                    145:                        
1.1.1.2 ! misho     146:                return py_axl_node_create (axl_node_get_first_child (self->node), axl_false, self->py_doc);
1.1       misho     147:        } else if (axl_cmp (attr, "next")) {
                    148:                /* next */
                    149:                if (axl_node_get_next (self->node) == NULL) {
                    150:                        Py_INCREF (Py_None);
                    151:                        return Py_None;
                    152:                } 
                    153:                        
1.1.1.2 ! misho     154:                return py_axl_node_create (axl_node_get_next (self->node), axl_false, self->py_doc);
1.1       misho     155:        } else if (axl_cmp (attr, "previous")) {
                    156:                /* previous */
                    157:                if (axl_node_get_previous (self->node) == NULL) {
                    158:                        Py_INCREF (Py_None);
                    159:                        return Py_None;
                    160:                } 
                    161:                        
1.1.1.2 ! misho     162:                return py_axl_node_create (axl_node_get_previous (self->node), axl_false, self->py_doc);
1.1       misho     163:        } else if (axl_cmp (attr, "parent")) {
                    164:                /* parent */
                    165:                if (axl_node_get_parent (self->node) == NULL) {
                    166:                        Py_INCREF (Py_None);
                    167:                        return Py_None;
                    168:                } 
                    169:                        
1.1.1.2 ! misho     170:                return py_axl_node_create (axl_node_get_parent (self->node), axl_false, self->py_doc);
1.1       misho     171: 
                    172:        } else if (axl_cmp (attr, "content")) {
                    173:                /* return a tuple with content and size */
                    174:                tuple = PyTuple_New (2);
                    175:                
                    176:                PyTuple_SetItem (tuple, 0, Py_BuildValue ("z", axl_node_get_content (self->node, &size)));
                    177:                PyTuple_SetItem (tuple, 1, Py_BuildValue ("i", size));
                    178:                        
                    179:                return tuple;
1.1.1.2 ! misho     180:        } else if (axl_cmp (attr, "trans")) {
        !           181:                /* return a tuple with content and size */
        !           182:                tuple = PyTuple_New (2);
        !           183:                
        !           184:                PyTuple_SetItem (tuple, 0, Py_BuildValue ("z", axl_node_get_content_trans (self->node, &size)));
        !           185:                PyTuple_SetItem (tuple, 1, Py_BuildValue ("i", size));
        !           186:                        
        !           187:                return tuple;
1.1       misho     188:        } else if (axl_cmp (attr, "doc")) {
                    189:                
                    190:                /* check if the node has a document configured */
                    191:                if (axl_node_get_doc (self->node) == NULL) {
                    192:                        /* return none */
                    193:                        Py_INCREF (Py_None);
                    194:                        return Py_None;
                    195:                } /* end if */
                    196: 
                    197:                /* ok, return document holding the node */
                    198:                return py_axl_doc_create (axl_node_get_doc (self->node), axl_false);
                    199:        }
                    200: 
                    201:        /* first implement generic attr already defined */
                    202:        result = PyObject_GenericGetAttr (o, attr_name);
                    203:        if (result)
                    204:                return result;
                    205:        
                    206:        return NULL;
                    207: }
                    208: 
                    209: /** 
                    210:  * @brief Implements attribute set operation.
                    211:  */
                    212: int py_axl_node_set_attr (PyObject *o, PyObject *attr_name, PyObject *v)
                    213: {
1.1.1.2 ! misho     214:        const char      * attr    = NULL;
        !           215:        const char      * content = NULL;
        !           216:        PyAxlNode        * self = (PyAxlNode *) o; 
1.1       misho     217: /*     axl_bool          boolean_value = axl_false; */
                    218: 
                    219:        /* now implement other attributes */
                    220:        if (! PyArg_Parse (attr_name, "s", &attr))
                    221:                return -1;
                    222: 
1.1.1.2 ! misho     223:         if (axl_cmp (attr, "content")) {
        !           224:                if (! PyArg_Parse (v, "s", &content))
        !           225:                        return -1;              
        !           226:                /* check if the content has invalid chars */
        !           227:                axl_node_set_content (self->node, content, -1);
        !           228:                return 0;
        !           229:        }
        !           230: 
1.1       misho     231:        /* now implement generic setter */
                    232:        return PyObject_GenericSetAttr (o, attr_name, v);
                    233: }
                    234: 
                    235: static PyObject * py_axl_node_next_called (PyObject * _self, PyObject * args)
                    236: {
                    237:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    238:        const char * node_name = NULL;
                    239:        axlNode    * node = NULL;
                    240: 
                    241:        /* parse and check result */
                    242:        if (! PyArg_ParseTuple (args, "s", &node_name))
                    243:                return NULL;
                    244:        
                    245:        /* get next called */
                    246:        node = axl_node_get_next_called (self->node, node_name);
                    247:        if (node == NULL) {
                    248:                Py_INCREF (Py_None);
                    249:                return Py_None;
                    250:        } /* end if */
                    251:        
                    252:        /* create node result */
1.1.1.2 ! misho     253:        return py_axl_node_create (node, axl_false, self->py_doc);
1.1       misho     254: }
                    255: 
                    256: static PyObject * py_axl_node_previous_called (PyObject * _self, PyObject * args)
                    257: {
                    258:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    259:        const char * node_name = NULL;
                    260:        axlNode    * node = NULL;
                    261: 
                    262:        /* parse and check result */
                    263:        if (! PyArg_ParseTuple (args, "s", &node_name))
                    264:                return NULL;
                    265:        
                    266:        /* get previous called */
                    267:        node = axl_node_get_previous_called (self->node, node_name);
                    268:        if (node == NULL) {
                    269:                Py_INCREF (Py_None);
                    270:                return Py_None;
                    271:        } /* end if */
                    272:        
                    273:        /* create node result */
1.1.1.2 ! misho     274:        return py_axl_node_create (node, axl_false, self->py_doc);
1.1       misho     275: }
                    276: 
                    277: static PyObject * py_axl_node_child_called (PyObject * _self, PyObject * args)
                    278: {
                    279:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    280:        const char * node_name = NULL;
                    281:        axlNode    * node = NULL;
                    282: 
                    283:        /* parse and check result */
                    284:        if (! PyArg_ParseTuple (args, "s", &node_name))
                    285:                return NULL;
                    286:        
                    287:        /* get previous called */
                    288:        node = axl_node_get_child_called (self->node, node_name);
                    289:        if (node == NULL) {
                    290:                Py_INCREF (Py_None);
                    291:                return Py_None;
                    292:        } /* end if */
                    293:        
                    294:        /* create node result */
1.1.1.2 ! misho     295:        return py_axl_node_create (node, axl_false, self->py_doc);
1.1       misho     296: }
                    297: 
                    298: static PyObject * py_axl_node_find_called (PyObject * _self, PyObject * args)
                    299: {
                    300:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    301:        const char * node_name = NULL;
                    302:        axlNode    * node = NULL;
                    303: 
                    304:        /* parse and check result */
                    305:        if (! PyArg_ParseTuple (args, "s", &node_name))
                    306:                return NULL;
                    307:        
                    308:        /* get previous called */
                    309:        node = axl_node_find_called (self->node, node_name);
                    310:        if (node == NULL) {
                    311:                Py_INCREF (Py_None);
                    312:                return Py_None;
                    313:        } /* end if */
                    314:        
                    315:        /* create node result */
1.1.1.2 ! misho     316:        return py_axl_node_create (node, axl_false, self->py_doc);
1.1       misho     317: }
                    318: 
                    319: static PyObject * py_axl_node_nth_child (PyObject * _self, PyObject * args)
                    320: {
                    321:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    322:        int          position  = -1;
                    323:        axlNode    * node = NULL;
                    324: 
                    325:        /* parse and check result */
                    326:        if (! PyArg_ParseTuple (args, "i", &position))
                    327:                return NULL;
                    328:        
                    329:        /* get previous called */
                    330:        node = axl_node_get_child_nth (self->node, position);
                    331:        if (node == NULL) {
                    332:                Py_INCREF (Py_None);
                    333:                return Py_None;
                    334:        } /* end if */
                    335:        
                    336:        /* create node result */
1.1.1.2 ! misho     337:        return py_axl_node_create (node, axl_false, self->py_doc);
1.1       misho     338: }
                    339: 
                    340: static PyObject * py_axl_node_has_attr (PyObject * _self, PyObject * args)
                    341: {
                    342:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    343:        char       * attr_name  = NULL;
                    344:        char       * attr_value = NULL;
                    345: 
                    346:        /* parse and check result */
                    347:        if (! PyArg_ParseTuple (args, "s|s", &attr_name, &attr_value))
                    348:                return NULL;
                    349: 
                    350:        if (attr_value == NULL)
                    351:                return Py_BuildValue ("i", HAS_ATTR (self->node, attr_name));
                    352:        return Py_BuildValue ("i", HAS_ATTR_VALUE (self->node, attr_name, attr_value));
                    353: }
                    354: 
                    355: static PyObject * py_axl_node_attr_value (PyObject * _self, PyObject * args)
                    356: {
                    357:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    358:        char       * attr_name  = NULL;
                    359:        char       * attr_value = NULL;
                    360: 
1.1.1.2 ! misho     361:        if (self->node == NULL) {
        !           362:                Py_INCREF (Py_None);
        !           363:                return Py_None;
        !           364:        } /* end if */
        !           365: 
1.1       misho     366:        /* parse and check result */
                    367:        if (! PyArg_ParseTuple (args, "s|s", &attr_name, &attr_value))
                    368:                return NULL;
                    369: 
                    370:        /* check set operation */
                    371:        if (PyTuple_Size (args) == 2) {
                    372:                /* remove previous attribute if exists */
                    373:                axl_node_remove_attribute (self->node, attr_name);
                    374: 
                    375:                /* found set operation */
                    376:                axl_node_set_attribute (self->node, attr_name, attr_value);
                    377:                Py_INCREF (Py_None);
                    378:                return Py_None;
                    379:        } /* end if */
                    380:        
                    381:        return Py_BuildValue ("z", ATTR_VALUE (self->node, attr_name));
                    382: }
                    383: 
1.1.1.2 ! misho     384: static PyObject * py_axl_node_attr_value_trans (PyObject * _self, PyObject * args)
        !           385: {
        !           386:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           387:        char       * attr_name  = NULL;
        !           388:        char       * value;
        !           389:        PyObject   * result;
        !           390: 
        !           391:        if (self->node == NULL) {
        !           392:                Py_INCREF (Py_None);
        !           393:                return Py_None;
        !           394:        } /* end if */
        !           395: 
        !           396:        /* parse and check result */
        !           397:        if (! PyArg_ParseTuple (args, "s", &attr_name))
        !           398:                return NULL;
        !           399: 
        !           400:        /* get attribute */
        !           401:        value  = axl_node_get_attribute_value_trans (self->node, attr_name);
        !           402:        result = Py_BuildValue ("z", value);
        !           403: 
        !           404:        /* free value */
        !           405:        axl_free (value);
        !           406:        return result;
        !           407: }
        !           408: 
        !           409: static PyObject * py_axl_node_str (PyObject * _self)
        !           410: {
        !           411:        char      * dump   = NULL;
        !           412:        PyAxlNode * self   = (PyAxlNode *) _self;
        !           413:        PyObject  * result = NULL;
        !           414: 
        !           415:        if (self == NULL || self->node == NULL) {
        !           416:                Py_INCREF (Py_None);
        !           417:                return Py_None;
        !           418:        }
        !           419:        
        !           420:        if (! axl_node_dump_pretty (self->node, &dump, NULL, 4)) {
        !           421:                Py_INCREF (Py_None);
        !           422:                return Py_None;
        !           423:        }
        !           424: 
        !           425:        result = Py_BuildValue ("z", dump);
        !           426:        axl_free (dump);
        !           427:        return result;
        !           428: }
        !           429: 
        !           430: 
        !           431: 
1.1       misho     432: static PyObject * py_axl_node_set_child (PyObject * _self, PyObject * args)
                    433: {
                    434:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    435:        PyObject   * child     = Py_None;
                    436: 
1.1.1.2 ! misho     437:        if (self->node == NULL) {
        !           438:                Py_INCREF (Py_None);
        !           439:                return Py_None;
        !           440:        } /* end if */
        !           441: 
1.1       misho     442:        /* parse and check result */
                    443:        if (! PyArg_ParseTuple (args, "O", &child))
                    444:                return NULL;
                    445: 
                    446:        /* check object received */
                    447:        if (! py_axl_node_check (child)) {
                    448:                /* set exception */
                    449:                PyErr_SetString (PyExc_TypeError, "Expected to receive an axl.Node object but received something different");
                    450:                return NULL;
                    451:        }
                    452: 
                    453:        /* set the node as child */
                    454:        axl_node_set_child (self->node, py_axl_node_get (child));
1.1.1.2 ! misho     455: 
        !           456:        /* and make the node to not release its internal node when it
        !           457:         * is deallocated to avoid the node finishing the reference
        !           458:         * that was finished by the document holding */
        !           459:        if (axl_node_get_doc (py_axl_node_get (_self)) || self->finish_on_gc)
        !           460:                ((PyAxlNode *)(child))->finish_on_gc = axl_false;
        !           461:        
        !           462:        /* return ok */
        !           463:        Py_INCREF (Py_None);
        !           464:        return Py_None;
        !           465: }
        !           466: 
        !           467: static PyObject * py_axl_node_set_child_after (PyObject * _self, PyObject * args)
        !           468: {
        !           469:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           470:        PyObject   * child     = Py_None;
        !           471: 
        !           472:        /* parse and check result */
        !           473:        if (! PyArg_ParseTuple (args, "O", &child))
        !           474:                return NULL;
        !           475: 
        !           476:        /* check object received */
        !           477:        if (! py_axl_node_check (child)) {
        !           478:                /* set exception */
        !           479:                PyErr_SetString (PyExc_TypeError, "Expected to receive an axl.Node object but received something different");
        !           480:                return NULL;
        !           481:        }
        !           482: 
        !           483:        /* set the node as child */
        !           484:        axl_node_set_child_after (self->node, py_axl_node_get (child));
        !           485: 
        !           486:        /* and make the node to not release its internal node when it
        !           487:         * is deallocated to avoid the node finishing the reference
        !           488:         * that was finished by the document holding */
        !           489:        if (axl_node_get_doc (py_axl_node_get (_self)) || self->finish_on_gc)
        !           490:                ((PyAxlNode *)(child))->finish_on_gc = axl_false;
1.1       misho     491:        
                    492:        /* return ok */
                    493:        Py_INCREF (Py_None);
                    494:        return Py_None;
                    495: }
                    496: 
                    497: static PyObject * py_axl_node_attr_cursor_new (PyObject * _self, PyObject * args)
                    498: {
                    499:        PyAxlNode  * self      = (PyAxlNode *) _self;
                    500:        
                    501:        /* create node cursor attr */
                    502:        return py_axl_attr_cursor_create (axl_node_attr_cursor_new (self->node), axl_true);
                    503: }
                    504: 
1.1.1.2 ! misho     505: static PyObject * py_axl_node_remove (PyObject * _self, PyObject * args)
        !           506: {
        !           507:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           508:        axl_bool     dealloc   = axl_true;
        !           509: 
        !           510:        /* parse and check result */
        !           511:        if (! PyArg_ParseTuple (args, "|i", &dealloc))
        !           512:                return NULL;
        !           513:        
        !           514:        /* remove the node from the document */
        !           515:        axl_node_remove (self->node, dealloc);
        !           516: 
        !           517:        /* clean internal reference when dealloc is true */
        !           518:        self->node = NULL;
        !           519: 
        !           520:        Py_INCREF (Py_None);
        !           521:        return Py_None;
        !           522: }
        !           523: 
        !           524: static PyObject * py_axl_node_replace (PyObject * _self, PyObject * args)
        !           525: {
        !           526:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           527:        PyAxlNode  * new_ref   = NULL;
        !           528:        axl_bool     dealloc   = axl_true;
        !           529: 
        !           530:        /* parse and check result */
        !           531:        if (! PyArg_ParseTuple (args, "O|i", &new_ref, &dealloc))
        !           532:                return NULL;
        !           533: 
        !           534:        if (! py_axl_node_check ((PyObject *)new_ref)) {
        !           535:                /* set exception */
        !           536:                PyErr_SetString (PyExc_TypeError, "Expected to receive an axl.Node object but received something different");
        !           537:                return NULL;
        !           538:        } /* end if */
        !           539:        
        !           540:        /* replace node reference */
        !           541:        axl_node_replace (self->node, new_ref->node, dealloc);
        !           542: 
        !           543:        if (dealloc)
        !           544:                self->node = NULL;
        !           545: 
        !           546:        /* and make the node to not release its internal node when it
        !           547:         * is deallocated to avoid the node finishing the reference
        !           548:         * that was finished by the document holding */
        !           549:        if (axl_node_get_doc (new_ref->node)) 
        !           550:                new_ref->finish_on_gc = axl_false;
        !           551: 
        !           552:        Py_INCREF (Py_None);
        !           553:        return Py_None;
        !           554: }
        !           555: 
        !           556: static PyObject * py_axl_node_deattach (PyObject * _self, PyObject * args)
        !           557: {
        !           558:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           559: 
        !           560:        /* deattach node */
        !           561:        axl_node_deattach (self->node);
        !           562:        Py_INCREF (Py_None);
        !           563:        return Py_None;
        !           564: }
        !           565: 
        !           566: static PyObject * py_axl_node_set_empty (PyObject * _self, PyObject * args)
        !           567: {
        !           568:        PyAxlNode  * self      = (PyAxlNode *) _self;
        !           569: 
        !           570:        /* remove the node from the document */
        !           571:        axl_node_set_is_empty (self->node, axl_true);
        !           572:        Py_INCREF (Py_None);
        !           573:        return Py_None;
        !           574: }
        !           575: 
1.1       misho     576: 
                    577: static PyMethodDef py_axl_node_methods[] = { 
                    578:        /* next_called */
                    579:        {"next_called", (PyCFunction) py_axl_node_next_called, METH_VARARGS,
                    580:         "Gest the xml node found at the same level, but with the name provided."},
                    581:        /* previous_called */
                    582:        {"previous_called", (PyCFunction) py_axl_node_previous_called, METH_VARARGS,
                    583:         "Gest the xml node found at the same level, but with the name provided."},
                    584:        /* child_called */
                    585:        {"child_called", (PyCFunction) py_axl_node_child_called, METH_VARARGS,
                    586:         "Gets the xml child node called as provided. The child is looked up only on direct childs."},
                    587:        /* find_called */
                    588:        {"find_called", (PyCFunction) py_axl_node_find_called, METH_VARARGS,
                    589:         "Gets the xml child node called as provided. The child is looked up in all childs found starting the parent node."},
                    590:        /* nth_child */
                    591:        {"nth_child", (PyCFunction) py_axl_node_nth_child, METH_VARARGS,
                    592:         "Allows to get a reference to the child node located at the same level at the nth position"},
                    593:        /* has_attr */
                    594:        {"has_attr", (PyCFunction) py_axl_node_has_attr, METH_VARARGS,
                    595:         "Allows to check if the provided node has the given attribute."},
                    596:        /* attr */
                    597:        {"attr", (PyCFunction) py_axl_node_attr_value, METH_VARARGS,
                    598:         "Allows to get/set the given attribute on the provided node."},
1.1.1.2 ! misho     599:        /* attr_trans */
        !           600:        {"attr_trans", (PyCFunction) py_axl_node_attr_value_trans, METH_VARARGS,
        !           601:         "Allows to get the given attribute on the provided node translate entity references ' < & >."},
1.1       misho     602:        /* set_child */
                    603:        {"set_child", (PyCFunction) py_axl_node_set_child, METH_VARARGS,
                    604:         "Allows to configure the provided node as the instance's child."},
1.1.1.2 ! misho     605:        /* set_child_after */
        !           606:        {"set_child_after", (PyCFunction) py_axl_node_set_child_after, METH_VARARGS,
        !           607:         "Allows to configure a new child node placing it after the calling node inside the same level. Both nodes will share parent node."},
1.1       misho     608:        /* attr_cursor_new */
                    609:        {"attr_cursor_new", (PyCFunction) py_axl_node_attr_cursor_new, METH_NOARGS,
                    610:         "Allows to create a new attribute cursor object used to iterate over all attributes of a node."},
1.1.1.2 ! misho     611:        /* remove */
        !           612:        {"remove", (PyCFunction) py_axl_node_remove, METH_VARARGS,
        !           613:         "Allows to remove the provided node from its current document. The method also receives an optional Boolean parameter to signal to also finish the internal reference."},
        !           614:        /* replace */
        !           615:        {"replace", (PyCFunction) py_axl_node_replace, METH_VARARGS,
        !           616:         "Allows to replace current node with the node provided. The method also receives an optional Boolean parameter to signal to also finish the internal reference."},
        !           617:        /* dettach */
        !           618:        {"deattach", (PyCFunction) py_axl_node_deattach, METH_NOARGS,
        !           619:         "Allows to dettach the current node from its holding document. This operation is useful to prepare node to be moved inside the same document or different documents."},
        !           620:        /* set_empty */
        !           621:        {"set_empty", (PyCFunction) py_axl_node_set_empty, METH_NOARGS,
        !           622:         "Allows to clear all node content."},
1.1       misho     623:        {NULL}  
                    624: }; 
                    625: 
                    626: static PyTypeObject PyAxlNodeType = {
                    627:     PyObject_HEAD_INIT(NULL)
                    628:     0,                         /* ob_size*/
                    629:     "axl.Node",                 /* tp_name*/
                    630:     sizeof(PyAxlNode),       /* tp_basicsize*/
                    631:     0,                         /* tp_itemsize*/
                    632:     (destructor)py_axl_node_dealloc, /* tp_dealloc*/
                    633:     0,                         /* tp_print*/
                    634:     0,                         /* tp_getattr*/
                    635:     0,                         /* tp_setattr*/
                    636:     0,                         /* tp_compare*/
                    637:     0,                         /* tp_repr*/
                    638:     0,                         /* tp_as_number*/
                    639:     0,                         /* tp_as_sequence*/
                    640:     0,                         /* tp_as_mapping*/
                    641:     0,                         /* tp_hash */
                    642:     0,                         /* tp_call*/
1.1.1.2 ! misho     643:     py_axl_node_str,           /* tp_str*/
1.1       misho     644:     py_axl_node_get_attr,    /* tp_getattro*/
                    645:     py_axl_node_set_attr,    /* tp_setattro*/
                    646:     0,                         /* tp_as_buffer*/
                    647:     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /* tp_flags*/
                    648:     "Node object; wrapper of axlNode API type",           /* tp_doc */
                    649:     0,                        /* tp_traverse */
                    650:     0,                        /* tp_clear */
                    651:     0,                        /* tp_richcompare */
                    652:     0,                        /* tp_weaklistoffset */
                    653:     0,                        /* tp_iter */
                    654:     0,                        /* tp_iternext */
                    655:     py_axl_node_methods,     /* tp_methods */
                    656:     0, /* py_axl_node_members, */     /* tp_members */
                    657:     0,                         /* tp_getset */
                    658:     0,                         /* tp_base */
                    659:     0,                         /* tp_dict */
                    660:     0,                         /* tp_descr_get */
                    661:     0,                         /* tp_descr_set */
                    662:     0,                         /* tp_dictoffset */
                    663:     (initproc)py_axl_node_init_type,      /* tp_init */
                    664:     0,                         /* tp_alloc */
                    665:     py_axl_node_new,         /* tp_new */
                    666: 
                    667: };
                    668: 
                    669: 
                    670: /** 
                    671:  * @brief Allows to check if the PyObject received represents a
                    672:  * PyAxlNode reference.
                    673:  */
                    674: axl_bool             py_axl_node_check    (PyObject          * obj)
                    675: {
                    676:        /* check null references */
                    677:        if (obj == NULL)
                    678:                return axl_false;
                    679: 
                    680:        /* return check result */
                    681:        return PyObject_TypeCheck (obj, &PyAxlNodeType);
                    682: }
                    683: 
                    684: /** 
                    685:  * @brief Allows to create a new PyAxlNode instance using the copy
                    686:  * provided. 
                    687:  *
                    688:  * @param node The xml node that will represents the python instance.
                    689:  *
                    690:  * @param finish_on_gc Signal to finish or not the internal copy once
                    691:  * the variable gets garbage collected.
                    692:  */
                    693: PyObject   * py_axl_node_create   (axlNode  * node, 
1.1.1.2 ! misho     694:                                   axl_bool   finish_on_gc,
        !           695:                                   PyObject * py_doc)
1.1       misho     696: {
                    697:        /* return a new instance */
                    698:        PyAxlNode * obj = (PyAxlNode *) PyObject_CallObject ((PyObject *) &PyAxlNodeType, NULL); 
                    699: 
                    700:        /* check ref created */
                    701:        if (obj == NULL) {
                    702:                __axl_log (LOG_DOMAIN, AXL_LEVEL_CRITICAL, "Failed to create PyAxlNode object, returning NULL");
                    703:                return NULL;
                    704:        } /* end if */
                    705: 
                    706:        /* set node if defined */
                    707:        if (node)
                    708:                obj->node = node;
                    709:        obj->finish_on_gc = finish_on_gc;
                    710: 
1.1.1.2 ! misho     711:        /* get a reference to the document if defined */
        !           712:        if (py_doc) {
        !           713:                Py_INCREF (py_doc);
        !           714:                obj->py_doc = py_doc;
        !           715:        } /* end if */
        !           716: 
        !           717:        __axl_log (LOG_DOMAIN, AXL_LEVEL_DEBUG, "Created axl.Node reference (%p, ref count: %d): %s (doc: %p, refcount: %d)", 
        !           718:                   obj, obj->ob_refcnt, axl_node_get_name (obj->node), obj->py_doc, obj->py_doc->ob_refcnt);
        !           719: 
1.1       misho     720:        return __PY_OBJECT (obj);
                    721: }
                    722: 
                    723: /** 
                    724:  * @internal Allows to get the internal reference used by the provided
                    725:  * PyAxlNode.
                    726:  */
                    727: axlNode    * py_axl_node_get      (PyObject * obj)
                    728: {
                    729:        /* check object received */
                    730:        if (! py_axl_node_check (obj))
                    731:                return NULL;
                    732: 
                    733:        /* return the node */
                    734:        return ((PyAxlNode *)obj)->node;
                    735: }
                    736: 
1.1.1.2 ! misho     737: /** 
        !           738:  * @internal Function used to set the node to be deallocated or not
        !           739:  * according to the provided dealloc value.
        !           740:  */
        !           741: void         py_axl_node_set_dealloc (PyObject * obj, axl_bool dealloc)
        !           742: {
        !           743:        PyAxlNode * node = (PyAxlNode *) obj;
        !           744:        node->finish_on_gc = dealloc;
        !           745:        return;
        !           746: }
        !           747: 
1.1       misho     748: void        init_axl_node      (PyObject * module)
                    749: {
                    750:        /* register type */
                    751:        if (PyType_Ready(&PyAxlNodeType) < 0)
                    752:                return;
                    753:        
                    754:        Py_INCREF (&PyAxlNodeType);
                    755:        PyModule_AddObject(module, "Node", (PyObject *)&PyAxlNodeType);
                    756: 
                    757: }
                    758: 
                    759: 

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