Update of /cvsroot/csp/APPLICATIONS/CSPSim/Bin
In directory sc8-pr-cvs1:/tmp/cvs-serv23034/Bin
Added Files:
CSPSim.py CompileData.py Console.py RebuildData.py Shell.py
ShellEnvironment.py
Log Message:
--- NEW FILE: CSPSim.py ---
#!/usr/bin/python -O
import sys, os.path
#import Shell
#from SimData.Compile import Compiler, CompilerUsageError
# enable lazy loading of shared library modules if available
if hasattr(sys, "setdlopenflags"):
sys.setdlopenflags(0x101)
def printUsage():
print "Combat Simulator Project - CSPSim"
print
print " Primary options:"
print " --compile-data run the data compiler"
print " --config=path path to config (.ini) file"
print " --help help message"
def runCSPSim(args):
if len(args):
print "Unrecognized option '%s'" % args[0]
print
printUsage()
print
sys.exit(1)
datapath = CSP.getDataPath()
dar = os.path.join(datapath, "Sim.dar")
if not os.path.exists(dar):
print
print "Static data archive '%s' not found." % dar
compileData([])
import Shell
app = CSP.CSPSim()
app.init()
app.setShell(Shell.Shell())
app.run()
app.cleanup()
print "CSPSim normal exit."
def compileData(args):
datapath = CSP.getDataPath()
dar = os.path.join(datapath, "Sim.dar")
XML = os.path.join(datapath, "XML")
#print "compile %s %s" % (XML, dar)
try:
from SimData.Compile import Compiler, CompilerUsageError
except:
print
print "ERROR: unable to load the SimData data compiler module."
print
print "Please verify the SimData has been installed correctly in your Python"
print "site-local directory. See the SimData README file for instructions."
print
sys.exit(1)
compiler = Compiler()
try:
compiler.parse([""] + args + [XML, dar])
except CompilerUsageError, e:
if e.getMessage():
print e
printUsage()
print
compiler.printUsage()
print
sys.exit(1)
compiler.compileAll()
print
action = None
config = "../Data/CSPSim.ini"
if os.path.exists("CSPSim.ini") or not os.path.exists(config):
config = "CSPSim.ini"
# process command line options
program = sys.argv[0]
all_args = sys.argv[1:]
other_args = []
for arg in all_args:
if arg == '--compile-data':
action = compileData
elif arg in ("--help", "-h", "-help"):
if action == None:
print
printUsage()
print
sys.exit(1)
else:
other_args.append(arg)
elif arg.startswith("--config="):
config = arg[9:]
else:
other_args.append(arg)
if action is None:
action = runCSPSim
# load simdata
try:
import SimData
except Exception, e:
msg = str(e)
if len(msg) > 60:
msg = msg.replace(": ", ":\n ")
print """
ERROR: Unable to import SimData.
%s
""" % msg,
if str(e).find("symbol") >= 0:
print """
Unresolved symbols often indicate missing libraries or improper link options.
""",
print """
Please verify that SimData has been properly installed on your system. See
the README file in the SimData distribution for details.
"""
sys.exit(1)
SimData.log().set_output("SimData.log")
SimData.log().setLogLevels(SimData.LOG_ALL, SimData.LOG_DEBUG)
try:
import cCSP as CSP
except Exception, e:
msg = str(e)
if len(msg) > 60:
msg = msg.replace(": ", ":\n ")
print """
ERROR: Unable to import cCSP.py
%s
""" % msg,
if str(e).find("No module named") >= 0:
print """
Some required files appear to be missing. Please verify that you have
successfully built CSPSim. See the README for details. If you are
still having trouble, ask for help on the forums at
http://csp.sourceforge.net/forum
"""
else:
print """
See the README files for additional information. If you are still having
trouble, ask for help on the forums at
http://csp.sourceforge.net/forum
"""
sys.exit(1)
SimData.log().setLogLevels(SimData.LOG_ALL, SimData.LOG_ALERT)
if not CSP.openConfig(config):
print "Unable to open primary configuration file (%s)" % config
sys.exit(0)
action(other_args)
--- NEW FILE: CompileData.py ---
import sys
sys.path = ['../Source'] + sys.path
sys.argv.append("--compile-data")
import CSPSim
--- NEW FILE: Console.py ---
import Tkinter, CSP
import traceback, sys
import threading
class Scrolled:
def __init__(self, barx, bary):
self.barx = barx
self.bary = bary
if barx is not None:
barx['command'] = self.__scrollXHandler
self["xscrollcommand"] = barx.set
if bary is not None:
bary['command'] = self.__scrollYHandler
self["yscrollcommand"] = bary.set
def __scrollXHandler(self, *L):
op, count = L[0], L[1]
if op == "scroll":
units = L[2]
self.xview_scroll(count, units)
else:
self.xview_moveto(count)
def __scrollYHandler(self, *L):
op, count = L[0], L[1]
if op == "scroll":
units = L[2]
self.yview_scroll(count, units)
else:
self.yview_moveto(count)
class SmartEntry(Tkinter.Entry, Scrolled):
def __init__(self, barx, bary, hwindow, *args, **opts):
Tkinter.Entry.__init__(self, *args, **opts)
Scrolled.__init__(self, barx, bary)
self.hwindow = hwindow
self.contents = Tkinter.StringVar()
self["textvariable"] = self.contents
self.bind('<Up>', self.up)
self.bind('<Down>', self.down)
self.bind('<Key-Return>', self.enter)
self.history=['']
self.idx = 0
self.command = ''
self.cb = None
def setCallback(self, cb):
self.cb = cb
def getCommand(self):
return self.command
def save(self):
if self.idx == 0:
self.history[-1] = self.contents.get()
def enter(self, event):
self.command = self.contents.get()
self.idx = 0
if len(self.command) > 0:
self.idx = 0
self.save()
self.hwindow.add(">>> " + self.command)
self.history.append('')
self.contents.set('')
if self.cb is not None:
self.cb(self)
def jump(self, idx):
if idx >= 0 and idx < len(self.history):
self.idx = idx
command = self.history[-1-idx]
self.contents.set(command)
self.icursor(len(command))
def up(self, event):
self.save()
self.jump(self.idx + 1)
def down(self, event):
self.jump(self.idx - 1)
class History(Tkinter.Text, Scrolled):
def __init__(self, barx, bary, *args, **opts):
Tkinter.Text.__init__(self, *args, **opts)
Scrolled.__init__(self, barx, bary)
def add(self, line):
self['state'] = Tkinter.NORMAL
self.insert(Tkinter.END, line+"\n")
self['state'] = Tkinter.DISABLED
self.see(Tkinter.END)
font = ("Courier", "14")
class TkThread(threading.Thread):
def __init__(self, sim):
threading.Thread.__init__(self)
self.sim = sim
def __del__(self):
print dir(self)
print self.__dict__
print "~THREAD"
def run(self):
self.build()
self.toplevel.deiconify()
self.toplevel.mainloop()
self.toplevel.withdraw()
self.clean()
self.sim.endConsole()
print "THREAD COMPLETE."
def clean(self):
self.toplevel.destroy()
self.root.destroy()
del self.root
del self.toplevel
del self.frame
del self.entry
del self.history
def build(self):
self.root = Tkinter.Tk()
self.root.withdraw()
self.toplevel = Tkinter.Toplevel()
self.toplevel.protocol("WM_DELETE_WINDOW", self.exit)
self.frame = Tkinter.Frame(self.toplevel)
self.frame.master.title("CSP Console")
self.frame.pack(expand=1, fill="both")
frame = Tkinter.Frame(self.frame)
frame.pack(expand=1, fill="both")
scroll = Tkinter.Scrollbar(frame, orient=Tkinter.VERTICAL)
self.history = History(None, scroll, frame, height=20, width=80, state=Tkinter.DISABLED, takefocus=0, selectbackground="blue", selectforeground="white")
self.history.pack(expand=1, side='left', fill="both")
scroll.pack(side='right', expand=0, fill="y")
frame = Tkinter.Frame(self.frame)
frame.pack(expand=0, fill="both")
scroll = Tkinter.Scrollbar(frame, orient=Tkinter.HORIZONTAL)
self.entry = SmartEntry(scroll, None, self.history, frame, width=80, bg="white")
self.entry.bind('<Escape>', self.exit)
self.entry.pack(side='top', fill="x")
self.entry.setCallback(self.do)
scroll.pack(side='bottom', fill="x")
self.locals = {}
def do(self, entry):
command = entry.getCommand()
if command.strip() == "quit":
self.exit()
return
gl = CSP.__dict__
try:
self.history.add(str(eval(command, gl, self.locals)))
except:
try:
exec(command, gl, self.locals)
except:
msg = ''.join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
self.history.add(msg)
def exit(self, *args):
#print self.entry.contents.get()
self.frame.quit()
class Console:
def __init__(self, sim):
self.sim = sim
def run(self):
thread = TkThread(self.sim)
thread.start()
print "THREAD STARTED."
if __name__ == "__main__":
Console().run()
--- NEW FILE: RebuildData.py ---
import sys
sys.path = ['../Source'] + sys.path
sys.argv.append("--compile-data")
sys.argv.append("--rebuild")
import CSPSim
--- NEW FILE: Shell.py ---
import traceback, sys
class Shell:
def __init__(self):
import ShellEnvironment
self.locals = ShellEnvironment.__dict__
self.globals = ShellEnvironment.__dict__
def run(self, command):
result = ""
try:
result = eval(command, self.globals, self.locals)
if result is None:
result = ""
else:
result = str(result)
except:
try:
exec(command, self.globals, self.locals)
except:
msg = ''.join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
result = msg
return result.strip()
--- NEW FILE: ShellEnvironment.py ---
import cCSP as CSP
theSim = CSP.cvar.CSPSim_theSim
copyright = """
Combat Simulator Project - FlightSim Demo
Copyright (C) 2002 The Combat Simulator Project
http://csp.sourceforge.net
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
"""
quit = "Press <ESC> to exit the shell."
exit = quit
help = """The help system is not yet implemented.
To get you started, you can use up and down arrows to move
through the command history. The builtin dir() command
lists items defined in the local namespace. You can also
type dir(object) to see methods and variable that object
defines. When you are done, press <ESC> to exit the shell."""
|