2016-05-25, 13:29
When I run the following python code in the terminal from the home folder, it runs perfectly. I know how to get the right input window to popup [change "raw_input(" for "xbmcgui.Dialog().numeric(3, "] from within Kodi. How can I have this code work within Kodi? I've tried to modify the "Hello World" addon, and was only able to get the input dialog up, but the file setting[/code]s didn't change. What am I doing wrong?
Code:
import xbmcaddon
import xbmcgui
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from lxml import etree
import re
import os
import shutil
addon = xbmcaddon.Addon()
addonname = addon.getAddonInfo('name')
settings_files = [
'special://home/userdata/profiles/DVR/addon_data/pvr.mythtv/settings.xml',
'special://home/userdata/profiles/HDHR/addon_data/pvr.mythtv/settings.xml',
'special://home/userdata/profiles/stream/addon_data/pvr.mythtv/settings.xml'
]
ip_pattern = '((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))'
def change_xml(xml_file, ip_address):
old_xml = xml_file.replace('.xml', '_old.xml')
if os.path.exists(old_xml):
os.remove(old_xml)
shutil.copy(xml_file, old_xml)
tree = etree.parse(xml_file)
element = tree.xpath('//setting[@id="host"]')
if element:
element = element[0]
element.set('value', ip_address)
element = tree.xpath('//setting[@id="host_ether"]')
if element:
element = element[0]
element.set('value', ip_address)
fileHandler = open(xml_file, "wb")
tree.write(fileHandler, encoding="utf-8", xml_declaration=True, pretty_print=True)
fileHandler.close()
def sample():
change_xml('sample.xml', '192.168.1.0')
def run():
global ip_pattern
ip_address = None
while True:
ip_address = xbmcgui.Dialog().numeric(3, 'Please input your ZION-P1 IP Address:')
data=int(ip_address)
if not re.match(ip_pattern, ip_address):
break
continue
for xml_file in settings_files:
change_xml(xml_file, ip_address)
if __name__ == '__main__':
run()
xbmcgui.Dialog().ok(addonname, ip_address)