I have a XML and a code. But I can’t figure out how to do the part.
XML:
<settings>
<plugin id="1" name="Send Mail" execute="func/sendmail.py">
<param name="towhere" value="To Where?" />
<param name="mailsub" value="Mail Subject" />
<param name="mailcont" value="Mail Content" />
<param name="senderid" value="xxxx@gmail.com" />
<param name="senderpw" value="xxxx" />
</plugin>
<plugin id="2" name="Open Tar" execute="func/taropen.py">
<param name="tarname" value="Tar file name" />
<param name="tarloc" value="Tar location" />
<param name="tardest" value="Tar destination" />
</plugin>
<plugin id="3" name="Server Reboot" execute="func/reboot.py">
<param name="rebootafter" value="1" />
</plugin>
</settings>
Code:
from xml.dom import minidom
yXML = minidom.parse('data/config.xml')
for plugin in yXML.getElementsByTagName('plugin'):
print plugin.getAttribute('id')+"- "+plugin.getAttribute('name')
selection = raw_input("Enter your choice: ")
selection = plugin.getAttribute('id')
if selection:
import os
exe = plugin.getAttribute('execute')
os.system('python '+exe)
When I type 1 or 2 as Input, It opens number 3. Could you help?
Output:
ubuntu:~/Desktop$ python test.py
1- Send Mail
2- Open Tar
3- Server Reboot
Enter your choice: 1
python: can't open file 'func/reboot.py': [Errno 2] No such file or directory
ubuntu:~/Desktop$
It’s too easy to solve but I can not think very well.
Thank you in advance!
When the
forloop that prints the values is over,pluginequals the third object (whoseidis3). So every use ofpluginafter that point will always refer to the server reboot option. Instead, you need: