Crash proof scripting
#1
Star 
it is a good idea to make your scripts handle errors nicely, so they atleast wont freeze the box... this trick written below goes alot of the way to avoid that the user is forced to reboot, while still providing debug output.

Quote:import sys, traceback
import xbmc, xbmcgui
try: emulating = xbmcgui.emulating
except: emulating = false

#get actioncodes from keymap.xml
action_previous_menu = 10
action_select_item = 7
action_parent_dir = 9
class myclass(xbmcgui.window):
       def (self):
               if emulating: xbmcgui.window.(self)
               self.addcontrol(xbmcgui.controlimage(0,0,720,480, "q:\\scripts\\tutorial\\background.gif"))
               self.stractioninfo = xbmcgui.controllabel(100, 200, 200, 200, "", "font13", "0xffff00ff")
               self.addcontrol(self.stractioninfo)
               self.stractioninfo.setlabel("push back to quit, a to display text and b to erase it")
       def onaction(self, action):
               try:
                       if action == action_previous_menu:
                               self.close()
                       if action == action_select_item:
                               self.straction = xbmcgui.controllabel(300, 300, 200, 200, "", "font14", "0xff00ff00")
                               self.addcontrol(self.straction)
                               self.straction.setlabel("hello world")
                       if action == action_parent_dir:
                               self.removecontrol(self.straction)
               except:
                       self.close()          #<-------------------------- look here!
                       e=sys.exc_info()
                       traceback.print_exception(e[0],e[1],e[2])

mydisplay = myclass()
mydisplay.domodal()
del mydisplay


also, if you are using progress bars. you should encapsulate the creation in a try block and close them in the finally block. that way you ensure that the progressbar is closed if your script chrashes. example:
Quote:                try:
                       self.progressbar=xbmcgui.dialogprogress()
                       self.progressbar.create("downloading...")
                       
                       ... do stuff ...
               finally:
                       self.progressbar.close()


another thing you want to do is to have timeouts on all your downloads... that is automatic if you use cachedhttp from http://www.xbmcscripts.com . but if you are using urllib or urllib2 you should do like this:
Quote:import socket
socket.setdefaulttimeout(7.0) #seconds
Reply
#2
this is good advice! thanks.
Reply

Logout Mark Read Team Forum Stats Members Help
Crash proof scripting0