Skip to content

Commit 1bf1da9

Browse files
author
David Sommerseth
committed
Improved setting of pythonmap.xml
The default file is now set to /usr/share/python-dmidecode/pythonmap.xml (defined in config.h) and can be overridden with the dmidecode.pythonmap() function in Python.
1 parent 89d2c50 commit 1bf1da9

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@
2323
#define ALIGNMENT_WORKAROUND
2424
#endif
2525

26+
#ifndef PYTHON_XML_MAP
27+
#define PYTHON_XML_MAP "/usr/share/python-dmidecode/pythonmap.xmp"
28+
#endif
29+
2630
#endif

src/dmidecodemodule.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static void init(void)
2121
opt.type = NULL;
2222
opt.mappingxml = NULL;
2323
opt.dmiversion_n = NULL;
24+
opt.python_xml_map = strdup(PYTHON_XML_MAP);
2425
}
2526

2627
u8 *parse_opt_type(u8 * p, const char *arg)
@@ -250,7 +251,7 @@ static PyObject *dmidecode_get(PyObject *self, const char *section)
250251
// Convert the retrieved XML nodes to Python dicts
251252
if( opt.mappingxml == NULL ) {
252253
// Load mapping into memory
253-
opt.mappingxml = xmlReadFile("pythonmap.xml", NULL, 0);
254+
opt.mappingxml = xmlReadFile(opt.python_xml_map, NULL, 0);
254255
assert( opt.mappingxml != NULL );
255256
}
256257

@@ -387,6 +388,27 @@ static PyObject *dmidecode_set_dev(PyObject * self, PyObject * arg)
387388
//PyErr_Occurred();
388389
}
389390

391+
static PyObject *dmidecode_set_pythonxmlmap(PyObject * self, PyObject * arg)
392+
{
393+
if(PyString_Check(arg)) {
394+
struct stat fileinfo;
395+
char *fname = PyString_AsString(arg);
396+
397+
memset(&fileinfo, 0, sizeof(struct stat));
398+
399+
if( stat(fname, &fileinfo) != 0 ) {
400+
PyErr_SetString(PyExc_IOError, "Could not access the given python map XML file");
401+
return NULL;
402+
}
403+
404+
free(opt.python_xml_map);
405+
opt.python_xml_map = strdup(fname);
406+
Py_RETURN_TRUE;
407+
} else {
408+
Py_RETURN_FALSE;
409+
}
410+
}
411+
390412

391413
static PyMethodDef DMIDataMethods[] = {
392414
{(char *)"dump", dmidecode_dump, METH_NOARGS, (char *)"Dump dmidata to set file"},
@@ -407,6 +429,9 @@ static PyMethodDef DMIDataMethods[] = {
407429

408430
{(char *)"type", dmidecode_get_type, METH_VARARGS, (char *)"By Type"},
409431

432+
{(char *)"pythonmap", dmidecode_set_pythonxmlmap, METH_O,
433+
(char *) "Use another python dict map definition. The default file is " PYTHON_XML_MAP},
434+
410435
{NULL, NULL, 0, NULL}
411436
};
412437

src/dmihelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ typedef struct _options {
115115
xmlDoc *mappingxml;
116116
xmlNode *dmiversion_n;
117117
PyObject *dumpfile;
118+
char *python_xml_map;
118119
} options;
119120
extern options opt;
120121

0 commit comments

Comments
 (0)