Autoexec.py editor def!! HANDY?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Big Grin  Autoexec.py editor def!! HANDY? Post: #1
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.

Code:
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
find quote
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Post: #2
Code:
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
find quote
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Post: #3
See this in action at the link below:
http://xbmc-scripting.googlecode.com/svn...default.py
find quote
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Post: #4
oops, wrong link, this one:
http://xbmc-scripting.googlecode.com/svn...default.py
find quote
Asteron Offline
"Skilled" Python Coder
Posts: 933
Joined: Feb 2004
Reputation: 0
Post: #5
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
Code:
lines = [ line for line in fh if not line.strip().startswith(script) ]
(the in operator here is an iterator like readline() )
(This post was last modified: 2007-02-16 18:03 by Asteron.)
find quote
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Post: #6
heres abit better version:

Code:
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.py')"
find quote
stanley87 Offline
Skilled Python Coder
Posts: 555
Joined: Sep 2006
Reputation: 2
Location: Chch, New Zealand
Post: #7
Actually, probably safer to do:
Script = "xbmc.executescript('Q:\\\\scripts\\\\ResumeX\\\\engine.py')"
find quote