From 13ff9d7e48ab1574b36473f75701cc7f7c1e461a Mon Sep 17 00:00:00 2001 From: Nima Talebi Date: Wed, 27 May 2009 00:11:54 +1000 Subject: WIP commit Merged the two XML files into one, and amended relevant code. I still want to modify the XML tag names, but not yet. The calls to dmidecode.type() not function as expected, but the others are broken - this is next. --- src/config.h | 6 +- src/dmidecodemodule.c | 69 +++---- src/dmihelper.h | 2 - src/py-map.xml | 54 ------ src/py-typemap.xml | 441 -------------------------------------------- src/pymap.xml | 495 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/setup-dbg.py | 2 +- src/setup.py | 2 +- src/xmlpythonizer.c | 70 ++++--- src/xmlpythonizer.h | 2 +- 10 files changed, 563 insertions(+), 580 deletions(-) delete mode 100644 src/py-map.xml delete mode 100644 src/py-typemap.xml create mode 100644 src/pymap.xml (limited to 'src') diff --git a/src/config.h b/src/config.h index 4fd5f41..add77cc 100644 --- a/src/config.h +++ b/src/config.h @@ -24,11 +24,7 @@ #endif #ifndef PYTHON_XML_MAP -#define PYTHON_XML_MAP "/usr/share/python-dmidecode/py-map.xml" -#endif - -#ifndef PYTHON_XML_TYPEMAP -#define PYTHON_XML_TYPEMAP "/usr/share/python-dmidecode/py-typemap.xml" +#define PYTHON_XML_MAP "/usr/share/python-dmidecode/pymap.xml" #endif #endif diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c index db780f3..1b18f47 100644 --- a/src/dmidecodemodule.c +++ b/src/dmidecodemodule.c @@ -1,6 +1,6 @@ /*. ******* coding:utf-8 AUTOHEADER START v1.1 ******* - *. vim: fileencoding=utf-8 syntax=c sw=2 ts=2 et + *. vim: fileencoding=utf-8 syntax=c sw=8 ts=8 et *. *. © 2007-2009 Nima Talebi *. © 2009 David Sommerseth @@ -60,11 +60,9 @@ static void init(void) opt.dumpfile = NULL; opt.flags = 0; opt.type = NULL; - opt.mappingxml = NULL; - opt.typemappingxml = NULL; opt.dmiversion_n = NULL; + opt.mappingxml = NULL; opt.python_xml_map = strdup(PYTHON_XML_MAP); - opt.python_xml_typemap = strdup(PYTHON_XML_TYPEMAP); } u8 *parse_opt_type(u8 * p, const char *arg) @@ -191,8 +189,12 @@ xmlNode *dmidecode_set_version() return ver_n; } -xmlNode *dmidecode_get_xml(PyObject *self, const char *section) +int dmidecode_get_xml(xmlNode* dmixml_n) { + assert(dmixml_n != NULL); + if(dmixml_n == NULL) { + return 0; + } //mtrace(); int ret = 0; @@ -201,16 +203,7 @@ xmlNode *dmidecode_get_xml(PyObject *self, const char *section) int efi; u8 *buf; - xmlNode *dmixml_n = xmlNewNode(NULL, (xmlChar *) "dmidecode"); - assert( dmixml_n != NULL ); - - // Append DMI version info - if( opt.dmiversion_n != NULL ) { - xmlAddChild(dmixml_n, xmlCopyNode(opt.dmiversion_n, 1)); - } - const char *f = opt.dumpfile ? PyString_AsString(opt.dumpfile) : opt.devmem; - if(access(f, R_OK) < 0) PyErr_SetString(PyExc_IOError, "Permission denied to memory file/device"); @@ -263,16 +256,16 @@ xmlNode *dmidecode_get_xml(PyObject *self, const char *section) free(opt.type); if(ret == 0) { free(buf); - } else { + } /* else { TODO: Review this and if correctly commented out, then just delete it... xmlFreeNode(dmixml_n); dmixml_n = NULL; - } + }*/ //muntrace(); - return dmixml_n; + return ret; } -static PyObject *dmidecode_get(PyObject *self, const char *section) +static PyObject *dmidecode_get(const char *section) { PyObject *pydata = NULL; xmlNode *dmixml_n = NULL; @@ -287,8 +280,14 @@ static PyObject *dmidecode_get(PyObject *self, const char *section) return NULL; } - dmixml_n = dmidecode_get_xml(self, section); - if( dmixml_n != NULL ) { + dmixml_n = xmlNewNode(NULL, (xmlChar *) "dmidecode"); + assert( dmixml_n != NULL ); + // Append DMI version info + if( opt.dmiversion_n != NULL ) { + xmlAddChild(dmixml_n, xmlCopyNode(opt.dmiversion_n, 1)); + } + + if(dmidecode_get_xml(dmixml_n) == 0) { ptzMAP *mapping = NULL; // Convert the retrieved XML nodes to Python dicts @@ -298,14 +297,7 @@ static PyObject *dmidecode_get(PyObject *self, const char *section) assert( opt.mappingxml != NULL ); } - if( opt.typemappingxml == NULL ) { - // Load mapping into memory - opt.typemappingxml = xmlReadFile(opt.python_xml_typemap, NULL, 0); - assert( opt.typemappingxml != NULL ); - } - - - mapping = dmiMAP_ParseMappingXML(opt.mappingxml, opt.typemappingxml, section); + mapping = dmiMAP_ParseMappingXML(opt.mappingxml, section); if( mapping == NULL ) { return NULL; } @@ -332,39 +324,39 @@ static PyObject *dmidecode_get(PyObject *self, const char *section) static PyObject *dmidecode_get_bios(PyObject * self, PyObject * args) { - return dmidecode_get(self, "bios"); + return dmidecode_get("bios"); } static PyObject *dmidecode_get_system(PyObject * self, PyObject * args) { - return dmidecode_get(self, "system"); + return dmidecode_get("system"); } static PyObject *dmidecode_get_baseboard(PyObject * self, PyObject * args) { - return dmidecode_get(self, "baseboard"); + return dmidecode_get("baseboard"); } static PyObject *dmidecode_get_chassis(PyObject * self, PyObject * args) { - return dmidecode_get(self, "chassis"); + return dmidecode_get("chassis"); } static PyObject *dmidecode_get_processor(PyObject * self, PyObject * args) { - return dmidecode_get(self, "processor"); + return dmidecode_get("processor"); } static PyObject *dmidecode_get_memory(PyObject * self, PyObject * args) { - return dmidecode_get(self, "memory"); + return dmidecode_get("memory"); } static PyObject *dmidecode_get_cache(PyObject * self, PyObject * args) { - return dmidecode_get(self, "cache"); + return dmidecode_get("cache"); } static PyObject *dmidecode_get_connector(PyObject * self, PyObject * args) { - return dmidecode_get(self, "connector"); + return dmidecode_get("connector"); } static PyObject *dmidecode_get_slot(PyObject * self, PyObject * args) { - return dmidecode_get(self, "slot"); + return dmidecode_get("slot"); } static PyObject *dmidecode_get_type(PyObject * self, PyObject * args) { @@ -375,9 +367,8 @@ static PyObject *dmidecode_get_type(PyObject * self, PyObject * args) if(PyArg_ParseTuple(args, (char *)"i", &lu)) { if(lu < 256) { char s[8]; - sprintf(s, "%lu", lu); - return dmidecode_get(self, s); + return dmidecode_get(s); } e = 1; //return Py_False; diff --git a/src/dmihelper.h b/src/dmihelper.h index 57ad7ab..82a936d 100644 --- a/src/dmihelper.h +++ b/src/dmihelper.h @@ -113,9 +113,7 @@ typedef struct _options { u8 *type; const struct string_keyword *string; xmlDoc *mappingxml; - xmlDoc *typemappingxml; char *python_xml_map; - char *python_xml_typemap; xmlNode *dmiversion_n; PyObject *dumpfile; } options; diff --git a/src/py-map.xml b/src/py-map.xml deleted file mode 100644 index fce89fc..0000000 --- a/src/py-map.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/py-typemap.xml b/src/py-typemap.xml deleted file mode 100644 index b695948..0000000 --- a/src/py-typemap.xml +++ /dev/null @@ -1,441 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/pymap.xml b/src/pymap.xml new file mode 100644 index 0000000..8619211 --- /dev/null +++ b/src/pymap.xml @@ -0,0 +1,495 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/setup-dbg.py b/src/setup-dbg.py index 988c3f3..e0a2358 100644 --- a/src/setup-dbg.py +++ b/src/setup-dbg.py @@ -8,7 +8,7 @@ setup( author = "Nima Talebi & David Sommerseth", author_email = "nima@it.net.au, davids@redhat.com", url = "http://projects.autonomy.net.au/python-dmidecode/", - data_files = [ ('share/python-dmidecode-dbg', ['src/py-map.xml', 'src/py-typemap.xml']) ], + data_files = [ ('share/python-dmidecode-dbg', ['src/pymap.xml']) ], ext_modules = [ Extension( "dmidecode", diff --git a/src/setup.py b/src/setup.py index dde35eb..bb79a14 100644 --- a/src/setup.py +++ b/src/setup.py @@ -8,7 +8,7 @@ setup( author = "Nima Talebi & David Sommerseth", author_email = "nima@it.net.au, davids@redhat.com", url = "http://projects.autonomy.net.au/python-dmidecode/", - data_files = [ ('share/python-dmidecode', ['src/py-map.xml', 'src/py-typemap.xml']) ], + data_files = [ ('share/python-dmidecode', ['src/pymap.xml']) ], ext_modules = [ Extension( "dmidecode", diff --git a/src/xmlpythonizer.c b/src/xmlpythonizer.c index 3099e5d..86bbb69 100644 --- a/src/xmlpythonizer.c +++ b/src/xmlpythonizer.c @@ -353,8 +353,9 @@ ptzMAP *_do_dmitypemap_parsing(xmlNode *node) { } // Internal parser - Mapper (Groups of Types) -ptzMAP *_do_dmimap_parsing(xmlNode *node, xmlDoc *xmlmap, xmlDoc *xmltypemap) { +ptzMAP *_do_dmimap_parsing(xmlNode *node, xmlDoc *xmlmap) { ptzMAP *retmap = NULL; + ptzMAP *tmp = NULL; xmlNode *ptr_n = NULL, *map_n = NULL;; // Go to the next XML_ELEMENT_NODE @@ -376,14 +377,21 @@ ptzMAP *_do_dmimap_parsing(xmlNode *node, xmlDoc *xmlmap, xmlDoc *xmltypemap) { } // Loop through it's children - xmlNode *typemap = xmlDocGetRootElement(xmltypemap); + xmlNode *typemap = xmlDocGetRootElement(xmlmap); assert( typemap != NULL ); + for( ptr_n = map_n ; ptr_n != NULL; ptr_n = ptr_n->next ) { char *type_id = NULL; + type_id = dmixml_GetAttrValue(ptr_n, "id"); map_n = dmixml_FindNodeByAttr(typemap, "id", type_id); - retmap = _do_dmitypemap_parsing(map_n); - break; + if( tmp != NULL) { + tmp->next = _do_dmitypemap_parsing(map_n); + tmp = tmp->next; + } else { + tmp = _do_dmitypemap_parsing(map_n); + retmap = tmp; + } } return retmap; } @@ -391,50 +399,40 @@ ptzMAP *_do_dmimap_parsing(xmlNode *node, xmlDoc *xmlmap, xmlDoc *xmltypemap) { // Main parser function for the mapping XML -ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, xmlDoc *xmltypemap, const char *mapname) { +ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname) { ptzMAP *map = NULL; xmlNode *node = NULL; int type_id = is_int(mapname); - if(type_id > -1) { - // Find the root tag and locate our mapping - node = xmlDocGetRootElement(xmltypemap); - assert( node != NULL ); - // Verify that the root node got the right name - if( (node == NULL) - || (xmlStrcmp(node->name, (xmlChar *) "dmidecode_typemap") != 0 )) { - PyErr_SetString(PyExc_IOError, "Invalid XML-Python mapping file"); - return NULL; - } + // Find the root tag and locate our mapping + node = xmlDocGetRootElement(xmlmap); + assert( node != 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; - } + // Verify that the root node got the right name + if( (node == NULL) + || (xmlStrcmp(node->name, (xmlChar *) "dmidecode_mapping") != 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; + } + + if(type_id > -1) { + node = dmixml_FindNode(node, "TypeMapping"); + assert( node != NULL ); char type_id_hex[5]; snprintf(type_id_hex, 5, "0x%02x", type_id); node = dmixml_FindNodeByAttr(node, "id", type_id_hex); } else { - // Find the root tag and locate our mapping - node = xmlDocGetRootElement(xmlmap); + node = dmixml_FindNode(node, "GroupMapping"); 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 section matching our request (mapname) for( node = node->children->next; node != NULL; node = node->next ) { if( xmlStrcmp(node->name, (xmlChar *) "Mapping") == 0) { @@ -455,7 +453,7 @@ ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, xmlDoc *xmltypemap, const char *m } // Start creating an internal map structure based on the mapping XML. - map = (type_id == -1) ? _do_dmimap_parsing(node, xmlmap, xmltypemap) : _do_dmitypemap_parsing(node); + map = (type_id == -1) ? _do_dmimap_parsing(node, xmlmap) : _do_dmitypemap_parsing(node); return map; } diff --git a/src/xmlpythonizer.h b/src/xmlpythonizer.h index 2f71dc8..e659340 100644 --- a/src/xmlpythonizer.h +++ b/src/xmlpythonizer.h @@ -48,7 +48,7 @@ typedef struct ptzMAP_s { } ptzMAP; -ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, xmlDoc *xmltypemap, const char *mapname); +ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname); #define ptzmap_Free(ptr) { ptzmap_Free_func(ptr); ptr = NULL; } void ptzmap_Free_func(ptzMAP *ptr); -- cgit