ocarina/src/core/bt/scripting.py

74 lines
1.4 KiB
Python
Raw Normal View History

2010-02-17 00:24:46 -05:00
#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Feb 16, 2010 11:56:00 PM$"
global scripts
import re
2010-02-17 00:24:46 -05:00
scripts = []
from bt import file
2010-02-21 22:44:28 -05:00
from bt.message import write
2010-02-24 23:34:02 -05:00
import session
2010-02-17 00:24:46 -05:00
global extension
extension = ["scion","py"]
2010-02-17 00:24:46 -05:00
def add(script):
global scripts
scripts += [script]
def runScript(script):
# Run commands if the script is not a python script
2010-02-24 19:16:57 -05:00
if re.match("(\/|\w)*\.py",script) == None:
from bt import cmd
f = file.fopen(script)
for line in f:
cmd.run(line)
f.close()
# Execute the script with the python interpreter as a backend
else:
2010-02-24 19:16:57 -05:00
write("Running python script: "+script,2)
execfile(script)
2010-02-17 00:24:46 -05:00
def runScripts():
2010-02-21 22:44:28 -05:00
global scripts
write("===== Begin output =====",1)
write(" ",1)
2010-02-17 00:24:46 -05:00
for script in scripts:
runScript(script)
write(" ",1)
write("====== End output ======",1)
from bt import cmd
cmd.run("exit")
2010-02-17 00:24:46 -05:00
def checkForScripts():
global scripts
if len(scripts) > 0:
2010-02-24 23:34:02 -05:00
session.events.invite("scion-begin",runScripts,0)
2010-02-17 00:24:46 -05:00
# If the given file is a script, we add it to the script list
def isScript(script):
global scripts
write("Checking if file is a script: "+script,2)
for ext in extension:
2010-02-24 19:16:57 -05:00
if not re.match("(\/|\w)*\."+ext,script) == None:
scripts += [script]
2010-02-24 23:34:02 -05:00
session.events.invite("scion-plugins-loaded",checkForScripts)
session.events.invite("scion-process-args",isScript)