Package teamwork :: Package shell :: Module TerminalShell
[hide private]
[frames] | no frames]

Source Code for Module teamwork.shell.TerminalShell

  1  import string 
  2  import sys 
  3   
  4  from teamwork.shell.PsychShell import * 
  5   
6 -class TerminalShell(PsychShell):
7 """An interactive terminal version of the base L{PsychShell} class.""" 8
9 - def __init__(self,entities=None, 10 classes=None, 11 agentClass=None, 12 multiagentClass=None, 13 file=None, 14 progress=None, 15 debug=0, 16 compileDynamics=True, 17 compilePolicies=None, 18 ):
19 """Almost identical to the base L{PsychShell} constructor 20 @param file: an input stream for reading commands. If none is 21 provided, this stream defaults to standard input. 22 @type file: C{str} 23 """ 24 if file: 25 self.file = open(file,'r') 26 else: 27 self.file = sys.stdin 28 PsychShell.__init__(self, 29 classes=classes, 30 agentClass=agentClass, 31 multiagentClass=multiagentClass, 32 debug=debug) 33 if self.phase == 'setup': 34 scenario = self.setupEntities(entities=self.createEntities(), 35 progress=progress, 36 compileDynamics=compileDynamics) 37 self.setupScenario(scenario)
38
39 - def createEntities(self):
40 """Interactive creation of entities for initial scenario""" 41 entityList = [] 42 setupDone = None 43 # Grab list of possible classes 44 while not setupDone: 45 cmd = self.getCommand() 46 if cmd == 'quit': 47 # Exits from setup *and* overall simulation 48 self.done = 1 49 setupDone = 1 50 elif cmd == 'done': 51 # Exits from setup and begins simulation 52 if len(entityList) > 0: 53 self.displayResult(cmd,'Entering simulation phase...') 54 setupDone = 1 55 else: 56 self.displayResult(cmd,'No entities created yet!') 57 elif cmd == 'entities': 58 # Prints out currentlist of entity names 59 self.displayResult(cmd, 60 map(lambda e:(e.name,e.__class__.__name__), 61 entityList)) 62 else: 63 # Otherwise, assume is a class name followed by instance name 64 commands = string.split(cmd) 65 try: 66 entityClass = commands[0] 67 name = commands[1] 68 except IndexError: 69 self.displayResult(cmd,'Usage: <class> <name>') 70 entityClass = None 71 if entityClass: 72 entity = createEntity(entityClass,name,self.classes, 73 self.agentClass) 74 if entity: 75 entityList.append(entity) 76 self.displayResult(cmd,entity.name) 77 else: 78 self.displayResult(cmd,'Unknown entity class: '+entityClass) 79 return entityList
80
81 - def getCommand(self):
82 """Terminal version of command entry. Prints a prompt 83 (currently assumes turn-based execution) and reads the command 84 entry from the input file.""" 85 if self.phase == 'setup': 86 prompt = '?' 87 else: 88 next = self.entities.next() 89 prompt = '%s (%d)> ' % (next[0]['name'], 90 self.entities.time) 91 print prompt, 92 try: 93 cmd = string.strip(self.file.readline()) 94 except KeyboardInterrupt: 95 cmd = 'quit' 96 return cmd
97
98 - def displayResult(self,cmd,result):
99 """Terminal version of the output display method. Uses 100 standard output.""" 101 print result 102 ## print 103 sys.stdout.flush()
104