Autoexec.py Editor Code Within
#1
Star 
Hi all,

Heres some handy code for adding/removing a new autoexec.py entry for scripts :-D

If everyone used this within scripts that edit the autoexec.py then we won't run into problems with existing autoexec.py entries being removed/edited.

Code:
from os.path import exists
from os import remove

def addauto(newentry, scriptcode):
        autoexecfile = "Q:\\scripts\\autoexec.py"                      
        if exists(autoexecfile):
                fh = open(autoexecfile)
                lines = []
                for line in fh.readlines():
                     lines.append(line)
                lines.append("import time" + "#" + scriptcode + "\n")
                lines.append("time.sleep(2)" + "#" + scriptcode + "\n")
                lines.append(newentry + "#" + scriptcode + "\n")
                fh.close()
                f = open(autoexecfile, "w")
                if not "import xbmc\n" in lines:
                    f.write("import xbmc" + "#" + scriptcode + "\n")
                f.writelines(lines)
                f.close()
        else:
                f = open(autoexecfile, "w")
                f.write("import xbmc" + "#" + scriptcode + "\n")
                f.write(newentry + "#" + scriptcode + "\n")
                f.close()


def removeauto(scriptcode):
        autoexecfile = "Q:\\scripts\\autoexec.py"      
        if exists(autoexecfile):
                fh = open(autoexecfile)
                lines = [ line for line in fh if not line.strip().endswith("#" + scriptcode) ]
                fh.close()
                if len(lines) == 0:
                    remove(autoexecfile)
                else:
                    f = open(autoexecfile, "w")
                    f.writelines(lines)
                    f.close()

newentry = string of new entry eg: "xbmc.executescript('Q:\scripts\XinBox\lib\minimode.py')"
scriptcode = string of unique code for your script eg: "xib" - for XinBox

What it does, is add your new entry with a #code for each new line added by your script, and then when removed, only removes the lines that have been added by your script.

Eg:

existing autoexec.py file may look like this:

Code:
import xbmc
xbmc.executescript('Q:\scripts\ResumeX\lib\engine.py')

and then another script wants to add its entry and you get:

Code:
import xbmc
xbmc.executescript('Q:\scripts\ResumeX\lib\engine.py')
import time#xib
time.sleep(2)#xib
xbmc.executescript('Q:\scripts\XinBox\lib\minimode.py')#xib


the time.sleep(2) is needed to allow the first script to start before the next script starts.
I have "dumb'd down" the code above to make it easier to read and understand.

Hope this can become standard :-D

Thanks,

Stanley87
Reply

Logout Mark Read Team Forum Stats Members Help
Autoexec.py Editor Code Within0