Need help translating terminal python code for Kodi
#1
Question 
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)
Reply
#2
Actually, the code above is my attempt at translating this code below, which works flawlessly in the terminal:

Code:
#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

from lxml import etree
import re
import os
import shutil

settings_files = [
    'userdata/profiles/DVR/addon_data/pvr.mythtv/settings.xml',
    'userdata/profiles/HDHR/addon_data/pvr.mythtv/settings.xml',
    '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 = raw_input('Please input your ZION-P1 IP Address:')
        if not re.match(ip_pattern, ip_address):
            continue
        break

    for xml_file in settings_files:
        change_xml(xml_file, ip_address)

if __name__ == '__main__':
    run()
Reply
#3
a Debug Log will likely provide some clues what goes wrong.

looking at the code in the first post, at least the indenting in the while loop is incorrect.
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#4
(2016-05-25, 14:13)ronie Wrote: looking at the code in the first post, at least the indenting in the while loop is incorrect.

So what should the indenting look like? I'm unfortunately a newbie at python.
Reply
#5
change this block:
Code:
######
    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
######

to this:
Code:
######
   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):
            continue
        break
######

(you also had the break and continue statements reversed compared to your terminal code)
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#6
Thank you for your help. I couldn't wrap my head around the indent yet. But I now get this from kodi.log:

19:03:38 T:139781589694208 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.ValueError'>
Error Contents: invalid literal for int() with base 10: '1.1.1.1'
Traceback (most recent call last):
File "/home/wesley/.kodi/addons/script.zion-p1connect/addon.py", line 67, in <module>
run()
File "/home/wesley/.kodi/addons/script.zion-p1connect/addon.py", line 58, in run
data=int(ip_address)
ValueError: invalid literal for int() with base 10: '1.1.1.1'
Reply
#7
I'm not understanding what is wrong with the run() statement and the data=int(ip_address). Can IP addresses be integers or must they be another type of data? If so, what other options are there?
Reply
#8
you can read up on indenting here:
http://www.tutorialspoint.com/python/pyt...syntax.htm

and data types are explained in this tutorial:
http://www.tutorialspoint.com/python/pyt..._types.htm
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply

Logout Mark Read Team Forum Stats Members Help
Need help translating terminal python code for Kodi0