summaryrefslogtreecommitdiffstats
path: root/src/xmlpythonizer.c
blob: 0065633d77e9622b624f804dd12d183f82dab138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*   Converts XML docs and nodes to Python dicts and lists by
 *   using an XML file which describes the Python dict layout
 *
 *   Copyright 2009      David Sommerseth <davids@redhat.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *   For the avoidance of doubt the "preferred form" of this code is one which
 *   is in an open unpatent encumbered format. Where cryptographic key signing
 *   forms part of the process of creating an executable the information
 *   including keys needed to generate an equivalently functional executable
 *   are deemed to be part of the source code.
 */

#include <Python.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <libxml/tree.h>
#include <libxml/xpath.h>

#include "dmixml.h"
#include "xmlpythonizer.h"

ptzMAP *ptzmap_Add(const ptzMAP *chain, char *rootp,
                   ptzTYPES ktyp, const char *key,
                   ptzTYPES vtyp, const char *value,
                   ptzMAP *child)
{
        ptzMAP *ret = NULL;

        assert( (ktyp == ptzCONST) || (ktyp == ptzSTR) || (ktyp == ptzINT) || (ktyp == ptzFLOAT) );
        assert( key != NULL );
        // Make sure that value and child are not used together
        assert( ((value == NULL) && child != NULL) || ((value != NULL) && (child == NULL)) );

        ret = (ptzMAP *) malloc(sizeof(ptzMAP)+2);
        assert( ret != NULL );
        memset(ret, 0, sizeof(ptzMAP)+2);

        if( rootp != NULL ) {
                ret->rootpath = strdup(rootp);
        }

        ret->type_key = ktyp;
        ret->key = strdup(key);

        ret->type_value = vtyp;
        if( value != NULL ) {
                ret->value = strdup(value);
                ret->child = NULL;
        } else if( child != NULL ) {
                ret->value = NULL;
                ret->child = child;
        }

        if( chain != NULL ) {
                ret->next = (ptzMAP *) chain;
        }
        return ret;
};

void ptzmap_Free_func(ptzMAP *ptr)
{
        if( ptr == NULL ) {
                return;
        }

        if( ptr->rootpath != NULL ) {
                free(ptr->rootpath);
                ptr->rootpath = NULL;
        }

        free(ptr->key);
        ptr->key = NULL;

        if( ptr->value != NULL ) {
                free(ptr->value);
                ptr->value = NULL;
        }

        if( ptr->child != NULL ) {
                ptzmap_Free(ptr->child);
        }
        if( ptr->next != NULL ) {
                ptzmap_Free(ptr->next);
        }
        free(ptr);
}


#if 0
// DEBUG FUNCTIONS
static const char *ptzTYPESstr[] = { "ptzCONST", "ptzSTR", "ptzINT", "ptzFLOAT", "ptzBOOL",
                                     "ptzLIST_STR", "ptzLIST_INT", "ptzLIST_FLOAT", "ptzLIST_BOOL",
                                     "ptzDICT", NULL };

void indent(int lvl)
{
        int i = 0;
        if( lvl == 0 ) {
                return;
        }

        for( i = 0; i < (lvl * 3); i++ ) {
                printf(" ");
        }
}

#define ptzmap_Dump(ptr) { ptzmap_Dump_func(ptr, 0); }
void ptzmap_Dump_func(const ptzMAP *ptr, int level)
{
        if( ptr == NULL ) {
                return;
        }

        indent(level); printf("key type:   (%i) %-13.13s - key:   %s\n",
                              ptr->type_key, ptzTYPESstr[ptr->type_key], ptr->key);
        indent(level); printf("value type: (%i) %-13.13s - value: %s\n",
                              ptr->type_value, ptzTYPESstr[ptr->type_value], ptr->value);
        if( ptr->child != NULL ) {
                indent(level); printf(" ** CHILD\n");
                ptzmap_Dump_func(ptr->child, level + 1);
                indent(level); printf(" ** ---------\n");
        }
        if( ptr->next != NULL ) {
                printf("\n");
                ptzmap_Dump_func(ptr->next, level);
        }
}
#endif // END OF DEBUG FUNCTIONS

//
//  Parser for the XML -> Python mapping XML file
//
//  This mappipng XML file describes how the Python result
//  should look like and where it should pick the data from
//  when later on parsing the dmidecode XML data.
//

// Valid key and value types for the mapping file
inline ptzTYPES _convert_maptype(const char *str) {
        if( strcmp(str, "string") == 0 ) {
                return ptzSTR;
        } else if( strcmp(str, "constant") == 0 ) {
                return ptzCONST;
        } else if( strcmp(str, "integer") == 0 ) {
                return ptzINT;
        } else if( strcmp(str, "float") == 0 ) {
                return ptzFLOAT;
        } else if( strcmp(str, "boolean") == 0 ) {
                return ptzBOOL;
        } else if( strcmp(str, "list:string") == 0 ) {
                return ptzLIST_STR;
        } else if( strcmp(str, "list:integer") == 0 ) {
                return ptzLIST_INT;
        } else if( strcmp(str, "list:float") == 0 ) {
                return ptzLIST_FLOAT;
        } else if( strcmp(str, "list:boolean") == 0 ) {
                return ptzLIST_BOOL;
        } else if( strcmp(str, "dict") == 0 ) {
                return ptzDICT;
        } else {
                fprintf(stderr, "Unknown field type: %s - defaulting to 'string'\n", str);
                return ptzSTR;
        }
}

// Internal parser
ptzMAP *_do_dmimap_parsing(xmlNode *node) {
        ptzMAP *retmap = NULL;
        xmlNode *ptr_n = NULL, *map_n = NULL;;

        // Go to the next XML_ELEMENT_NODE
        for( map_n = node; map_n != NULL; map_n = map_n->next ) {
                if( map_n->type == XML_ELEMENT_NODE ) {
                        break;
                }
        }
        if( map_n == NULL ) {
                return NULL;
        }

        // Go to the first <Map> node
        if( xmlStrcmp(node->name, (xmlChar *) "Map") != 0 ) {
                map_n = dmixml_FindNode(node, "Map");
                if( map_n == NULL ) {
                        return NULL;
                }
        }

        // Loop through it's children
        for( ptr_n = map_n ; ptr_n != NULL; ptr_n = ptr_n->next ) {
                ptzTYPES type_key, type_value;
                char *key = NULL, *value = NULL;
                char *rootpath = NULL;

                if( ptr_n->type != XML_ELEMENT_NODE ) {
                        continue;
                }

                // Get the attributes defining key, keytype, value and valuetype
                key = dmixml_GetAttrValue(ptr_n, "key");
                type_key = _convert_maptype(dmixml_GetAttrValue(ptr_n, "keytype"));

                value = dmixml_GetAttrValue(ptr_n, "value");
                type_value = _convert_maptype(dmixml_GetAttrValue(ptr_n, "valuetype"));

                rootpath = dmixml_GetAttrValue(ptr_n, "rootpath");

                if( type_value == ptzDICT ) {
                        // When value type is ptzDICT, traverse the children nodes
                        // - should contain another Map set instead of a value attribute
                        if( ptr_n->children == NULL ) {
                                continue;
                        }
                        // Recursion
                        retmap = ptzmap_Add(retmap, rootpath, type_key, key, type_value, NULL,
                                            _do_dmimap_parsing(ptr_n->children->next));
                } else {
                        // Append the value as a normal value when the
                        // value type is not a Python Dict
                        retmap = ptzmap_Add(retmap, rootpath, type_key, key, type_value, value, NULL);
                }
                value = NULL;
                key = NULL;
        }
        return retmap;
}

// Main parser function for the mapping XML
ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname) {
        ptzMAP *map = NULL;
        xmlNode *node = NULL;

        // Find the root tag and locate our mapping
        node = xmlDocGetRootElement(xmlmap);
        assert( node != NULL );

        // Verify that the root node got the right name
        if( (node == NULL)
            || (xmlStrcmp(node->name, (xmlChar *) "dmidecode_fieldmap") != 0 )) {
                // PyErr_SetString(PyExc_IOError, "Invalid XML-Python mapping file");
                return NULL;
        }

        // Verify that it's of a version we support
        if( strcmp(dmixml_GetAttrValue(node, "version"), "1") != 0 ) {
                // PyErr_SetString(PyExc_IOError, "Unsupported XML-Python mapping file format");
                return NULL;
        }

        // Find the <Mapping> section matching our request (mapname)
        for( node = node->children->next; node != NULL; node = node->next ) {
                if( xmlStrcmp(node->name, (xmlChar *) "Mapping") == 0) {
                        char *name = dmixml_GetAttrValue(node, "name");
                        if( (name != NULL) && (strcmp(name, mapname) == 0) ) {
                                break;
                        }
                }
        }

        if( node == NULL ) {
                char msg[8194];
                snprintf(msg, 8193, "No mapping for '%s' was found "
                         "in the XML-Python mapping file%c", mapname, 0);
                // PyErr_SetString(PyExc_IOError, msg);
                return NULL;
        }

        // Start creating an internal map structure based on the mapping XML.
        map = _do_dmimap_parsing(node);

        return map;
}


//
//  Parser routines for converting XML data into Python structures
//
inline PyObject *StringToPyObj(ptzTYPES type, const char *str) {
        PyObject *value;

        if( str == NULL ) {
                return Py_None;
        }

        switch( type ) {
        case ptzINT:
        case ptzLIST_INT:
                value = PyInt_FromLong(atoi(str));
                break;

        case ptzFLOAT:
        case ptzLIST_FLOAT:
                value = PyFloat_FromDouble(atof(str));
                break;

        case ptzBOOL:
        case ptzLIST_BOOL:
                value = PyBool_FromLong((atoi(str) == 1 ? 1:0));
                break;

        case ptzSTR:
        case ptzLIST_STR:
                value = PyString_FromString(str);
                break;

        default:
                fprintf(stderr, "Invalid type '%i' for value '%s'\n", type, str);
                value = Py_None;
        }
        return value;
}

// Retrieve a value from the XML doc (XPath Context) based on a XPath query
xmlXPathObject *_get_xpath_values(xmlXPathContext *xpctx, const char *xpath) {
        xmlChar *xp_xpr = NULL;
        xmlXPathObject *xp_obj = NULL;

        if( xpath == NULL ) {
                return NULL;
        }

        xp_xpr = xmlCharStrdup(xpath);
        xp_obj = xmlXPathEvalExpression(xp_xpr, xpctx);
        assert( xp_obj != NULL );
        free(xp_xpr);

        return xp_obj;
}

char *_get_key_value(char *key, size_t buflen, ptzMAP *map_p, xmlXPathContext *xpctx, int idx) {
        xmlXPathObject *xpobj = NULL;

        memset(key, 0, buflen);

        switch( map_p->type_key ) {
        case ptzCONST:
                strncpy(key, map_p->key, buflen-1);
                break;

        case ptzSTR:
        case ptzINT:
        case ptzFLOAT:
                xpobj = _get_xpath_values(xpctx, map_p->key);
                if( xpobj == NULL ) {
                        return NULL;
                }
                if( dmixml_GetXPathContent(key, buflen, xpobj, idx) == NULL ) {
                        xmlXPathFreeObject(xpobj);
                        return NULL;
                }
                xmlXPathFreeObject(xpobj);
                break;

        default:
                fprintf(stderr, "Unknown key type: %i\n", map_p->type_key);
                return NULL;
        }
        // We consider to have a key, if the first byte is a readable
        // character (usually starting at 0x20/32d)
        return ((key != NULL) && (strlen(key) > 0) ? key : NULL) ;
}



#define PyADD_DICT_VALUE(p, k, v) {                                \
                PyDict_SetItemString(p, k, v);                     \
                Py_DECREF(v);                                      \
        }

inline void _add_xpath_result(PyObject *pydat, xmlXPathContext *xpctx, ptzMAP *map_p, xmlXPathObject *value) {
        int i = 0;
        char *key = NULL;
        char *val = NULL;

        assert( pydat != NULL && value != NULL );

        key = (char *) malloc(258);
        assert( key != NULL );

        val = (char *) malloc(4098);
        assert( val != NULL );

        switch( value->type ) {
        case XPATH_NODESET:
                if( value->nodesetval == NULL ) {
                        break;
                }
                if( value->nodesetval->nodeNr == 0 ) {
                        if( _get_key_value(key, 256, map_p, xpctx, 0) != NULL ) {
                                PyADD_DICT_VALUE(pydat, key, Py_None);
                        }
                } else {
                        for( i = 0; i < value->nodesetval->nodeNr; i++ ) {
                                if( _get_key_value(key, 256, map_p, xpctx, i) != NULL ) {
                                        dmixml_GetXPathContent(val, 4097, value, i);
                                        PyADD_DICT_VALUE(pydat, key, StringToPyObj(map_p->type_value, val));
                                }
                        }
                }
                break;
        default:
                if( _get_key_value(key, 256, map_p, xpctx, 0) != NULL ) {
                        dmixml_GetXPathContent(val, 4097, value, 0);
                        PyADD_DICT_VALUE(pydat, key, StringToPyObj(map_p->type_value, val));
                }
                break;
        }
        free(key);
        free(val);
}


// Internal XML parser routine, which traverses the given mapping table,
// returning a Python structure accordingly to the map.
PyObject *_deep_pythonize(PyObject *retdata, ptzMAP *in_map, xmlNode *data_n, int elmtid) {
        ptzMAP *map_p = NULL;
        char *key = NULL;
        xmlXPathContext *xpctx = NULL;
        xmlDoc *xpdoc = NULL;

        xpdoc = xmlNewDoc((xmlChar *) "1.0");
        assert( xpdoc != NULL );
        xmlDocSetRootElement(xpdoc, xmlCopyNode(data_n, 1));

        xpctx = xmlXPathNewContext(xpdoc);
        assert( xpctx != NULL );
        xpctx->node = data_n;

        key = (char *) malloc(258);
        assert( key != NULL );

        for( map_p = in_map; map_p != NULL; map_p = map_p->next) {
                xmlXPathObject *xpo = NULL;
                PyObject *value = NULL;
                int i;

                // Extract value
                switch( map_p->type_value ) {
                case ptzCONST:
                        if( _get_key_value(key, 256, map_p, xpctx, 0) != NULL ) {
                                value = PyString_FromString(map_p->value);
                                PyADD_DICT_VALUE(retdata, key, value);
                        } else {
                                char msg[8094];
                                snprintf(msg, 8092, "Could not get key value: %s [%i] (Defining key: %s)%c",
                                         map_p->rootpath, elmtid, map_p->key, 0);
                                PyErr_SetString(PyExc_LookupError, msg);
                                continue;
                        }
                        break;

                case ptzSTR:
                case ptzINT:
                case ptzFLOAT:
                case ptzBOOL:
                        xpo = _get_xpath_values(xpctx, map_p->value);
                        if( xpo != NULL ) {
                                _add_xpath_result(retdata, xpctx, map_p, xpo);
                                xmlXPathFreeObject(xpo);
                        }
                        break;

                case ptzLIST_STR:
                case ptzLIST_INT:
                case ptzLIST_FLOAT:
                case ptzLIST_BOOL:
                        xpo = _get_xpath_values(xpctx, map_p->value);
                        if( xpo != NULL ) {
                                if( _get_key_value(key, 256, map_p, xpctx, 0) != NULL ) {
                                        if( xpo->nodesetval->nodeNr > 0 ) {
                                                value = PyList_New(0);
                                                for( i = 0; i < xpo->nodesetval->nodeNr; i++ ) {
                                                        char *valstr = NULL;
                                                        valstr = (char *) malloc(4098);
                                                        dmixml_GetXPathContent(valstr, 4097, xpo, i);
                                                        PyList_Append(value, StringToPyObj(map_p->type_value,
                                                                                           valstr));
                                                        free(valstr);
                                                }
                                        } else {
                                                value = Py_None;
                                        }
                                        PyADD_DICT_VALUE(retdata, key, value);
                                        xmlXPathFreeObject(xpo);
                                } else {
                                        char msg[8094];
                                        snprintf(msg, 8092, "Could not get key value: "
                                                 "%s [%i] (Defining key: %s)%c",
                                                 map_p->rootpath, elmtid, map_p->key, 0);
                                        PyErr_SetString(PyExc_LookupError, msg);
                                        continue;
                                }
                        }
                        break;

                case ptzDICT:
                        // Traverse children nodes
                        if( map_p->child == NULL ) {
                                break;
                        }
                        if( _get_key_value(key, 256, map_p, xpctx, 0) == NULL ) {
                                char msg[8094];
                                snprintf(msg, 8092, "Could not get key value: %s [%i] (Defining key: %s)%c",
                                         in_map->rootpath, elmtid, map_p->key, 0);
                                PyErr_SetString(PyExc_LookupError, msg);
                                continue;
                        }
                        value = pythonizeXMLnode(map_p->child, data_n);
                        PyADD_DICT_VALUE(retdata, key, (value != NULL ? value : Py_None));
                        break;

                default:
                        fprintf(stderr, "Unknown value type: %i\n", map_p->type_value);
                        break;
                }
        }

        free(key);
        xmlXPathFreeContext(xpctx);
        xmlFreeDoc(xpdoc);
        return retdata;
}

// Convert a xmlNode to a Python object, based on the given map
PyObject *pythonizeXMLnode(ptzMAP *in_map, xmlNode *data_n) {
        xmlXPathContext *xpctx = NULL;
        xmlDoc *xpdoc = NULL;
        PyObject *retdata = NULL;

        if( (in_map == NULL) || (data_n == NULL) ) {
                PyErr_SetString(PyExc_LookupError, "XMLnode or map is NULL");
                return NULL;
        }

        // Set the root node in the XPath context
        retdata = PyDict_New();
        if( in_map->rootpath != NULL ) {
                xmlXPathObject *xpo = NULL;
                int i;

                xpdoc = xmlNewDoc((xmlChar *) "1.0");
                assert( xpdoc != NULL );
                xmlDocSetRootElement(xpdoc, xmlCopyNode(data_n, 1));

                xpctx = xmlXPathNewContext(xpdoc);
                assert( xpctx != NULL );
                xpctx->node = data_n;

                xpo = _get_xpath_values(xpctx, in_map->rootpath);
                if( (xpo == NULL) || (xpo->nodesetval == NULL) || (xpo->nodesetval->nodeNr == 0) ) {
                        char msg[8094];
                        snprintf(msg, 8092, "Could not locate XML path node: %s (Defining key: %s)%c",
                                 in_map->rootpath, in_map->key, 0);
                        PyErr_SetString(PyExc_LookupError, msg);

                        if( xpo != NULL ) {
                                xmlXPathFreeObject(xpo);
                        }
                        xmlFreeDoc(xpdoc);
                        xmlXPathFreeContext(xpctx);
                        return NULL;
                }

                retdata = PyDict_New();
                for( i = 0; i < xpo->nodesetval->nodeNr; i++ ) {
                        _deep_pythonize(retdata, in_map, xpo->nodesetval->nodeTab[i], i);
                }
                xmlXPathFreeObject(xpo);
                xmlXPathFreeContext(xpctx);
                xmlFreeDoc(xpdoc);
       } else {
                _deep_pythonize(retdata, in_map, data_n, 0);
        }

        return retdata;
}


// Convert a xmlDoc to a Python object, based on the given map
PyObject *pythonizeXMLdoc(ptzMAP *map, xmlDoc *doc)
{
        xmlNode *node = NULL;

        node = xmlDocGetRootElement(doc);
        return pythonizeXMLnode(map, node);
}


#if 0
// Simple independent main function - only for debugging
int main() {
        xmlDoc *doc = NULL, *data = NULL;
        ptzMAP *map = NULL;
        PyObject *pydat = NULL;

        Py_Initialize();

        doc = xmlReadFile("pythonmap.xml", NULL, 0);
        assert( doc != NULL );

        map = dmiMAP_ParseMappingXML(doc, "processor");
        // ptzmap_Dump(map);
        printf("----------------------\n");


        data = xmlReadFile("cpu.xml", NULL, 0);
        assert( data != NULL );

        pydat = pythonizeXMLdoc(map, data);
        Py_INCREF(pydat);
        PyObject_Print(pydat, stdout, 0);
        Py_DECREF(pydat);

        ptzmap_Free(map);
        xmlFreeDoc(data);
        xmlFreeDoc(doc);

        return 0;
}
#endif

#if 0
// Simple test module for Python - only for debugging
PyObject* demo_xmlpy()
{
        xmlDoc *doc = NULL, *mapping_xml = NULL;
        ptzMAP *mapping = NULL;
        PyObject *ret = NULL;

        // Read the XML-Python mapping setup
        mapping_xml = xmlReadFile("pythonmap.xml", NULL, 0);
        assert( mapping_xml != NULL );

        mapping = dmiMAP_ParseMappingXML(mapping_xml, "bios");
        assert( mapping != NULL );

        // Read XML data from file
        doc = xmlReadFile("cpu.xml", NULL, 0);
        assert( doc != NULL );

        // Create a PyObject out of the XML indata
        ret = pythonizeXMLdoc(mapping, doc);

        // Clean up and return the data
        ptzmap_Free(mapping);
        xmlFreeDoc(doc);
        xmlFreeDoc(mapping_xml);

        return ret;
}

static PyMethodDef DemoMethods[] = {
        {"xmlpy", demo_xmlpy, METH_NOARGS, ""},
        {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initxmlpythonizer(void) {
        PyObject *module =
                Py_InitModule3((char *)"xmlpythonizer", DemoMethods,
                               "XML to Python Proof-of-Concept Python Module");

        PyObject *version = PyString_FromString("2.10");
        Py_INCREF(version);
        PyModule_AddObject(module, "version", version);
}
#endif // Python test module