PDA

View Full Version : Autoexec.py editor def!! HANDY?


stanley87
2007-02-16, 11:08
Hi all,

I just wrote this code for my ResumeX script and thought this might come in handy for people that add functions into their scripts that can enable/disable scripts run on startup.

def editauto(self):
test = xbmc.getInfoLabel("system.profilename")
test = str(test)
print str(test)
if test != "Master user":
if os.access("P:\\scripts\\", os.F_OK)==0:
os.mkdir("P:\\scripts\\")
autoexecfile = "P:\\scripts\\autoexec.py"
else:
if os.access("Q:\\UserData\\scripts\\", os.F_OK)==0:
os.mkdir("Q:\\UserData\\scripts\\")
autoexecfile = "Q:\\UserData\\scripts\\autoexec.py"
if os.path.exists(autoexecfile):
fh = open(autoexecfile)
count = 0
lines = []
for line in fh.readlines():
theLine = line.strip()
if theLine.startswith("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')"):
return
lines.append(str(line))
count = count + 1
fh.close()
lines.append("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')\n")
count = count + 1
f = open(autoexecfile, "wb")
for i in range (0 , count):
f.write(lines[i])
f.close()
return
else:
f = open(autoexecfile, "wb")
f.write("import xbmc\n")
f.write("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')")
f.close()
return

Of course, it needs to be edited to the your particular script, but apart from that, is should work good. It has a feature so it doesnt add the line twice etc, so this can be run everytime the script is run via autoexec.py and NO double ups will occur. Also, its profile friendly! :-D

Let me know your thoughts.

Cheers
Stanley87

stanley87
2007-02-16, 12:38
def addauto(self):
test = xbmc.getInfoLabel("system.profilename")
test = str(test)
if test != "Master user":
if os.access("P:\\scripts\\", os.F_OK)==0:
os.mkdir("P:\\scripts\\")
autoexecfile = "P:\\scripts\\autoexec.py"
else:
if os.access("Q:\\UserData\\scripts\\", os.F_OK)==0:
os.mkdir("Q:\\UserData\\scripts\\")
autoexecfile = "Q:\\UserData\\scripts\\autoexec.py"
if os.path.exists(autoexecfile):
fh = open(autoexecfile)
count = 0
lines = []
for line in fh.readlines():
theLine = line.strip()
if theLine.startswith("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')"):
return
lines.append(str(line))
count = count + 1
fh.close()
lines.append("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')\n")
count = count + 1
f = open(autoexecfile, "w")
for i in range (0 , count):
f.write(lines[i])
f.close()
return
else:
f = open(autoexecfile, "w")
f.write("import xbmc\n")
f.write("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')")
f.close()
return


def removeauto(self):
test = xbmc.getInfoLabel("system.profilename")
test = str(test)
if test != "Master user":
if os.access("P:\\scripts\\", os.F_OK)==0:
return
autoexecfile = "P:\\scripts\\autoexec.py"
else:
if os.access("Q:\\UserData\\scripts\\", os.F_OK)==0:
return
autoexecfile = "Q:\\UserData\\scripts\\autoexec.py"
if os.path.exists(autoexecfile):
fh = open(autoexecfile)
count = 0
lines = []
for line in fh.readlines():
theLine = line.strip()
if theLine.startswith("xbmc.executescript('Q:\scripts\ResumeX\lib\engine. py')"):
pass
else:
lines.append(str(line))
count = count + 1
fh.close()
f = open(autoexecfile, "w")
for i in range (0 , count-1):
f.write(lines[i])
f.close()
return
return

So there is a def for adding to/creating an autoexec.py and also a def for removing the scripts execute line from autoexec.py

So, this code won't CHANGE any previous autoexec.py allready in place, but instead, add to it. Same when removing the script. Any other script that is run via the autoexec.py will stay in tact :-D

stanley87
2007-02-16, 12:45
See this in action at the link below:
http://xbmc-scripting.googlecode.com/svn/trunk/XinBox/default.py

stanley87
2007-02-16, 12:46
oops, wrong link, this one:
http://xbmc-scripting.googlecode.com/svn/trunk/ResumeX/default.py

Asteron
2007-02-16, 17:58
Pretty cool. If you want to make it more generic you could pass in the script pass as an argument instead.

Also there are some places that can be shorter... like instead of count use len(lines)... also there is a f.writelines(lines)

If you don't mind going a bit more advanced your central read loop can be replaced with

lines = [ line for line in fh if not line.strip().startswith(script) ]
(the in operator here is an iterator like readline() )

stanley87
2007-02-17, 07:17
heres abit better version:

def addauto(self):
if os.access("P:\\scripts\\", os.F_OK)==0:
os.mkdir("P:\\scripts\\")
autoexecfile = "P:\\scripts\\autoexec.py"
if os.path.exists(autoexecfile):
fh = open(autoexecfile)
lines = []
for line in fh.readlines():
theLine = line.strip()
if theLine.startswith(Script):
return
lines.append(str(line))
fh.close()
lines.append(Script+"\n")
f = open(autoexecfile, "w")
f.writelines(lines)
f.close()
return
else:
f = open(autoexecfile, "w")
f.write("import xbmc\n")
f.write(Script)
f.close()
return


def removeauto(self):
if os.access("P:\\scripts\\", os.F_OK)==0:
return
autoexecfile = "P:\\scripts\\autoexec.py"
if os.path.exists(autoexecfile):
fh = open(autoexecfile)
lines = [ line for line in fh if not line.strip().startswith(Script) ]
fh.close()
f = open(autoexecfile, "w")
f.writelines(lines)
f.close()
return
return

Where Script is a global variable in the form:
Script = "xbmc.executescript('Q:\\scripts\\ResumeX\\engine.p y')"

stanley87
2007-02-17, 07:19
Actually, probably safer to do:
Script = "xbmc.executescript('Q:\\\\scripts\\\\ResumeX\\\\en gine.py')"