Varable parsing error with python
OmPs
torque.india at gmail.com
Tue Feb 10 02:30:04 EST 2015
In an XML file which contain a tag and then these tags contain properties,
which later are being called in anoter tag where these properties are
expanded as variables.
For eg.
<instance name='instancename' user='test' group='test'
fullname='test%20-%20dev%20app' >
<property>
<APPVERSION>1.0</APPVERSION>
</property>
</instance>
<component name='testappComp' user='test' group='test'>
<version>1.2</version>
<os>Red Hat Enterprise Linux Server 5 X86_64</os>
<package>testsapppath</package>
</component>
<package name='testapppath'>
<description>my testapp area</description>
<version>${APPVERSION}</version>
</package>
So now i have written a python program whcih is parsing this program, so
what i am doing in here is reading this xml file
with open(filename, "r") as f:
orig_xml = f.read()
#unescape html characters like %20
xmlstring = urllib.unquote(orig_xml)
#convert xml to dict
doc = xmltodict.parse(xmlstring)
#replace property values in xml string via instancename
s = Template(xmlstring)
#iterate over instances/ match the one we need
for i in doc.get("application").get("instance", None):
#when found
# s/r properties in xml and then convert it back to dict
try:
if i.get("@name", None) == instancename:
try:
instance_property = dict(i.get("property"))
final_string = s.safe_substitute(instance_property)
final_dict = xmltodict.parse(final_string)
except TypeError:
final_dict = doc
# Handle strings in case of dict, strings do not have get method.
except AttributeError:
final_dict = doc
and when i am trying to get the version no. using a function for getting
the package version
def _getPackgeVersion(xmlfile, p):
package = str(p)
if isinstance(fpmdict["application"]["package"], list):
for i in fpmdict["application"]["package"]:
if i["@name"] == p:
_pkgVersion = i["version"]
else:
_pkgversion = fpmdict["application"]["package"]["version"]
return _pkgVersion
pkgVersion = _getPackgeVersion(final_doc, testpackage)
print type(pkgVersion)
I am getting the below error
<type 'NoneType'>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150210/1810aac5/attachment.html>
More information about the Python-list
mailing list