comparison cmd2.py @ 334:99dd71cb477a

run() folded into app.cmdloop()
author cat@eee
date Fri, 12 Feb 2010 23:29:19 -0500
parents 45e70737791f
children f69211e2c570
comparison
equal deleted inserted replaced
333:45e70737791f 334:99dd71cb477a
881 else: 881 else:
882 if line[-1] == '\n': # this was always true in Cmd 882 if line[-1] == '\n': # this was always true in Cmd
883 line = line[:-1] 883 line = line[:-1]
884 return line 884 return line
885 885
886 def cmdloop(self, intro=None): 886 def _cmdloop(self, intro=None):
887 """Repeatedly issue a prompt, accept input, parse an initial prefix 887 """Repeatedly issue a prompt, accept input, parse an initial prefix
888 off the received input, and dispatch to action methods, passing them 888 off the received input, and dispatch to action methods, passing them
889 the remainder of the line as argument. 889 the remainder of the line as argument.
890 """ 890 """
891 891
1210 'continuation_prompt','current_script_dir')) 1210 'continuation_prompt','current_script_dir'))
1211 self.stdin = target 1211 self.stdin = target
1212 self.use_rawinput = False 1212 self.use_rawinput = False
1213 self.prompt = self.continuation_prompt = '' 1213 self.prompt = self.continuation_prompt = ''
1214 self.current_script_dir = os.path.split(targetname)[0] 1214 self.current_script_dir = os.path.split(targetname)[0]
1215 stop = self.cmdloop() 1215 stop = self._cmdloop()
1216 self.stdin.close() 1216 self.stdin.close()
1217 keepstate.restore() 1217 keepstate.restore()
1218 self.lastcmd = '' 1218 self.lastcmd = ''
1219 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT 1219 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT
1220 do__load = do_load # avoid an unfortunate legacy use of do_load from sqlpython 1220 do__load = do_load # avoid an unfortunate legacy use of do_load from sqlpython
1243 self.stdout.write("Couldn't read from file %s\n" % source) 1243 self.stdout.write("Couldn't read from file %s\n" % source)
1244 return '' 1244 return ''
1245 data = f.read() 1245 data = f.read()
1246 f.close() 1246 f.close()
1247 return data 1247 return data
1248
1249 def runTranscriptTests(self, callargs):
1250 class TestMyAppCase(Cmd2TestCase):
1251 CmdApp = self.__class__
1252 self.__class__.testfiles = callargs
1253 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
1254 testcase = TestMyAppCase()
1255 runner = unittest.TextTestRunner()
1256 result = runner.run(testcase)
1257 result.printErrors()
1258
1259 def cmdloop(self):
1260 parser = optparse.OptionParser()
1261 parser.add_option('-t', '--test', dest='test',
1262 action="store_true",
1263 help='Test against transcript(s) in FILE (wildcards OK)')
1264 (callopts, callargs) = parser.parse_args()
1265 if callopts.test:
1266 self.runTranscriptTests(callargs)
1267 else:
1268 # hold onto the args and run .onecmd with them
1269 # in sqlpython, first arg has implied \connect
1270 for initial_command in callargs:
1271 if self.onecmd(initial_command + '\n') == app._STOP_AND_EXIT:
1272 return
1273 self._cmdloop()
1248 1274
1249 class HistoryItem(str): 1275 class HistoryItem(str):
1250 listformat = '-------------------------[%d]\n%s\n' 1276 listformat = '-------------------------[%d]\n%s\n'
1251 def __init__(self, instr): 1277 def __init__(self, instr):
1252 str.__init__(self) 1278 str.__init__(self)
1324 list.append(self, new) 1350 list.append(self, new)
1325 new.idx = len(self) 1351 new.idx = len(self)
1326 def extend(self, new): 1352 def extend(self, new):
1327 for n in new: 1353 for n in new:
1328 self.append(n) 1354 self.append(n)
1329
1330 1355
1331 def get(self, getme=None, fromEnd=False): 1356 def get(self, getme=None, fromEnd=False):
1332 if not getme: 1357 if not getme:
1333 return self 1358 return self
1334 try: 1359 try:
1490 except StopIteration: 1515 except StopIteration:
1491 pass 1516 pass
1492 def tearDown(self): 1517 def tearDown(self):
1493 if self.CmdApp: 1518 if self.CmdApp:
1494 self.outputTrap.tearDown() 1519 self.outputTrap.tearDown()
1495
1496 def run(app):
1497 parser = optparse.OptionParser()
1498 parser.add_option('-t', '--test', dest='test', action="store_true",
1499 help='Test against transcript(s) in FILE (wildcards OK)')
1500 (callopts, callargs) = parser.parse_args()
1501 if callopts.test:
1502 class TestMyAppCase(Cmd2TestCase):
1503 CmdApp = app.__class__
1504 app.__class__.testfiles = callargs
1505 sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
1506 testcase = TestMyAppCase()
1507 runner = unittest.TextTestRunner()
1508 result = runner.run(testcase)
1509 result.printErrors()
1510 else:
1511 # hold onto the args and run .onecmd with them
1512 # in sqlpython, first arg has implied \connect
1513 for initial_command in callargs:
1514 if app.onecmd(initial_command + '\n') == app._STOP_AND_EXIT:
1515 return
1516 app.cmdloop()
1517 1520
1518 if __name__ == '__main__': 1521 if __name__ == '__main__':
1519 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) 1522 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
1520 1523
1521 ''' 1524 '''