diff options
author | David Sommerseth <davids@redhat.com> | 2009-04-28 11:12:58 +0200 |
---|---|---|
committer | David Sommerseth <davids@redhat.com> | 2009-04-29 11:22:12 +0200 |
commit | c7be629d44d4e2be6c8116796714e0042a977885 (patch) | |
tree | a126396444123f0b9aa505f8a8d675838af73458 /src/xmlpythonizer.h | |
parent | 70e35765fbb9be6ddd6aab6555916777a593e0aa (diff) | |
download | python-dmidecode-c7be629d44d4e2be6c8116796714e0042a977885.tar.gz python-dmidecode-c7be629d44d4e2be6c8116796714e0042a977885.tar.xz python-dmidecode-c7be629d44d4e2be6c8116796714e0042a977885.zip |
Added generic XML -> Python parser
The xmlpythonizer module will convert any XML data (xmlNode
or xmlDoc) and format it as a Python object. The formatting is
defined in it's own XML file.
Basic format:
<dmidecode_fieldmap version="1">
<Mapping name="{name of mapping table}">
<Map keytype="{key type}" key="{key value}"
valuetype="{value type} value="{value}"/>
</Mapping>
</dmidecode_fieldmap>
The keytype and key attributes defines the key in the Python Dict. The
root element will always be a Python Dict structure. The valid key types
are:
* constant
Uses the value in {key} as the key value
* string, integer, float
Uses a string value from the data XML to be converted to Python. The
value set in the key attribute defines an XPath value which points to the
data to be used as a Python dict key.
Since Python only supports C strings in the C interface for Python dict
keys, integer and float will be treated as strings.
The valuetype and value attributes are similar to the keys, but with some more
features. Valid valuetypes are:
* constant
The value given in the value attribute will be used in the value in
the Python result.
* string, integer, float
The value given in the value attribute defines the XPath to the data XML,
of where to retrieve the value for the given key.
The valuetype defines if the data should be understood as a string, integer
or float in the Python result.
* list:string, list:integer, list:float
This does the same as the string, integer or float type, with a tweak. The
data will be put into a list. If the XPath given returns multiple nodes,
all of them will be added to this list.
* dict
The dict valuetype is more special. It should not contain any value
attribute. On the other hand, it should contain a sub-level of <Map> tags.
In this way, you can build up a multi dimensional Python dict.
Example:
** pythonmap.xml **
<?xml version="1.0" encoding="UTF-8"?>
<dmidecode_fieldmap version="1">
<Mapping name="example_map">
<Map keytype="constant" key="DemoCase" valuetype="constant" value="XML Pythonizing"/>
<Map keytype="constant" key="String1" valuetype="string" value="/example/string1"/>
<Map keytype="constant" key="AttribString1" valuetype="integer" value="/example/string1/@int_value"/>
<Map keytype="string" key="/example/keyset/@value" valuetype="dict">
<Map keytype="constant" key="Value1" valuetype="string" value="/example/keyset/value1"/>
<Map keytype="constant" key="ValueList" valuetype="list:string" value="/example/keyset/valuelist"/>
</Map>
</Mapping>
</dmidecode_fieldmap>
** exampledata.xml **
<?xml version="1.0" encoding="UTF-8"?>
<example>
<string1 int_value="1234">String value #1</string1>
<keyset value="TestData">
<value1>More test data</value1>
<valuelist>Value1 in list</valuelist>
<valuelist>Value2 in list</valuelist>
<valuelist>Value3 in list</valuelist>
</keyset>
</example>
** C code snippet **
void xmlpythonizer() {
xmlDoc *xmlmap = NULL;
xmlDoc *xmldata = NULL;
ptzMAP *mapping = NULL;
PyObject *pythondata = NULL;
// Read XML files
xmlmap = xmlReadFile("pythonmap.xml", NULL, 0);
xmldata = xmlReadFile("exampledata.xml", NULL, 0);
// Parse the mapping XML
mapping = dmiMAP_ParseMappingXML(xmlmap, "example_map");
// Parse the xmldata into a Python object
pythondata = pythonizeXMLdoc(mapping, xmldata);
// ..... the program continues to do something useful
}
The result stored inside the pythondata object should now be
something similar to:
{'DemoCase': 'XML Pythonizing',
'String1': 'String value #1',
'AttribString1: 1234,
'TestData': {'Value1': 'More test data',
'ValueList': ['Value1 in list','Value2 in list','Value3 in list']}
}
Diffstat (limited to 'src/xmlpythonizer.h')
-rw-r--r-- | src/xmlpythonizer.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/xmlpythonizer.h b/src/xmlpythonizer.h new file mode 100644 index 0000000..4882d81 --- /dev/null +++ b/src/xmlpythonizer.h @@ -0,0 +1,50 @@ +/* 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. + */ + +#ifndef _XMLPYTHONIZER_H +#define _XMLPYTHONIZER_H + +typedef enum ptzTYPES_e { ptzCONST, ptzSTR, ptzINT, ptzFLOAT, + ptzLIST_STR, ptzLIST_INT, ptzLIST_FLOAT, + ptzDICT } ptzTYPES; + +typedef struct ptzMAP_s { + ptzTYPES type_key; // Valid types: ptzCONST, ptzSTR, ptzINT, ptzFLOAT + char *key; // for ptzCONST key contains a static string, other types an XPath to XML data + ptzTYPES type_value; + char *value; // for ptzCONST key contains a static string, + // the rest of types, an XPath to XML data + struct ptzMAP_s *child; // Only used for type_value == pyDICT + struct ptzMAP_s *next; // Pointer chain + +} ptzMAP; + + +ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname); +PyObject *pythonizeXMLdoc(ptzMAP *map, xmlDoc *xmldoc); +PyObject *pythonizeXMLnode(ptzMAP *map, xmlNode *nodes); + +#endif // _XMLPYTHONIZER_H |