comparison cmd2.py @ 42:b3200c6e5763

inserting exception for quit...
author catherine@cordelia
date Sat, 24 May 2008 20:30:33 -0400
parents 1275d59c08a7
children e03a8c03ac37
comparison
equal deleted inserted replaced
41:1275d59c08a7 42:b3200c6e5763
123 else: 123 else:
124 def getPasteBuffer(): 124 def getPasteBuffer():
125 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 125 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
126 setPasteBuffer = getPasteBuffer 126 setPasteBuffer = getPasteBuffer
127 127
128 class ExitException(Exception):
129 pass
130
128 class Cmd(cmd.Cmd): 131 class Cmd(cmd.Cmd):
129 caseInsensitive = True 132 caseInsensitive = True
130 multilineCommands = [] 133 multilineCommands = []
131 continuationPrompt = '> ' 134 continuationPrompt = '> '
132 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 135 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
133 excludeFromHistory = '''run r list l history hi ed li eof'''.split() 136 excludeFromHistory = '''run r list l history hi ed li eof'''.split()
134 defaultExtension = 'txt' 137 defaultExtension = 'txt'
135 defaultFileName = 'command.txt' 138 defaultFileName = 'command.txt'
136 editor = os.environ.get('EDITOR') 139 editor = os.environ.get('EDITOR')
140 _STOP_AND_EXIT = 2
137 if not editor: 141 if not editor:
138 if sys.platform[:3] == 'win': 142 if sys.platform[:3] == 'win':
139 editor = 'notepad' 143 editor = 'notepad'
140 else: 144 else:
141 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: 145 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']:
334 try: 338 try:
335 import readline 339 import readline
336 readline.set_completer(self.old_completer) 340 readline.set_completer(self.old_completer)
337 except ImportError: 341 except ImportError:
338 pass 342 pass
343 return stop
339 344
340 def do_EOF(self, arg): 345 def do_EOF(self, arg):
341 return True 346 return True
342 do_eof = do_EOF 347 do_eof = do_EOF
343 348
368 if param in self.settable: 373 if param in self.settable:
369 val = getattr(self, param) 374 val = getattr(self, param)
370 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param)))) 375 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param))))
371 376
372 def do_quit(self, arg): 377 def do_quit(self, arg):
373 return 1 378 raise ExitException
374 do_exit = do_quit 379 do_exit = do_quit
375 do_q = do_quit 380 do_q = do_quit
376 381
377 def do_show(self, arg): 382 def do_show(self, arg):
378 'Shows value of a parameter' 383 'Shows value of a parameter'
499 print 'Problem opening file %s: \n%s' % (fname, e) 504 print 'Problem opening file %s: \n%s' % (fname, e)
500 keepstate.restore() 505 keepstate.restore()
501 return 506 return
502 self.use_rawinput = False 507 self.use_rawinput = False
503 self.prompt = self.continuationPrompt = '' 508 self.prompt = self.continuationPrompt = ''
504 self.cmdloop() 509 stop = self.cmdloop()
510 # but how to detect whether to exit totally?
505 self.stdin.close() 511 self.stdin.close()
506 keepstate.restore() 512 keepstate.restore()
507 self.lastcmd = '' 513 self.lastcmd = ''
514 return (stop == self._STOP_AND_EXIT)
508 515
509 def do_run(self, arg): 516 def do_run(self, arg):
510 """run [arg]: re-runs an earlier command 517 """run [arg]: re-runs an earlier command
511 518
512 no arg -> run most recent command 519 no arg -> run most recent command