comparison cmd2.py @ 206:00eaec841567

do not let SET errors pass silently
author catherine@dellzilla
date Wed, 04 Mar 2009 16:45:41 -0500
parents 5d22be6d2f88
children a3bec7704e65
comparison
equal deleted inserted replaced
205:db4e8b2feb0c 206:00eaec841567
210 return result 210 return result
211 211
212 class Cmd(cmd.Cmd): 212 class Cmd(cmd.Cmd):
213 echo = False 213 echo = False
214 caseInsensitive = True 214 caseInsensitive = True
215 continuationPrompt = '> ' 215 continuationprompt = '> '
216 timing = False 216 timing = False
217 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit # make sure your terminators are not in here! 217 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit # make sure your terminators are not in here!
218 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } 218 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' }
219 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 219 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
220 noSpecialParse = 'set ed edit exit'.split() 220 noSpecialParse = 'set ed edit exit'.split()
221 defaultExtension = 'txt' 221 defaultExtension = 'txt'
222 defaultFileName = 'command.txt' 222 defaultFileName = 'command.txt'
223 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 223 settable = ['prompt', 'continuationprompt', 'defaultFileName', 'editor', 'caseInsensitive',
224 'echo', 'timing'] 224 'echo', 'timing']
225 settable.sort() 225 settable.sort()
226 226
227 editor = os.environ.get('EDITOR') 227 editor = os.environ.get('EDITOR')
228 _STOP_AND_EXIT = 2 228 _STOP_AND_EXIT = 2
517 return 0 # command was empty except for comments 517 return 0 # command was empty except for comments
518 try: 518 try:
519 statement = self.parsed(line) 519 statement = self.parsed(line)
520 while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): 520 while statement.parsed.multilineCommand and (statement.parsed.terminator == ''):
521 statement = '%s\n%s' % (statement.parsed.raw, 521 statement = '%s\n%s' % (statement.parsed.raw,
522 self.pseudo_raw_input(self.continuationPrompt)) 522 self.pseudo_raw_input(self.continuationprompt))
523 statement = self.parsed(statement) 523 statement = self.parsed(statement)
524 except Exception, e: 524 except Exception, e:
525 print e 525 print e
526 return 0 526 return 0
527 527
649 def do_EOF(self, arg): 649 def do_EOF(self, arg):
650 return True 650 return True
651 do_eof = do_EOF 651 do_eof = do_EOF
652 652
653 def showParam(self, param): 653 def showParam(self, param):
654 any_shown = False
654 param = param.strip().lower() 655 param = param.strip().lower()
655 for p in self.settable: 656 for p in self.settable:
656 if p.startswith(param): 657 if p.startswith(param):
657 val = getattr(self, p) 658 val = getattr(self, p)
658 self.stdout.write('%s: %s\n' % (p, str(getattr(self, p)))) 659 self.stdout.write('%s: %s\n' % (p, str(getattr(self, p))))
660 any_shown = True
661 if not any_shown:
662 print "Parameter '%s' not supported (type 'show' for list of parameters)." % param
659 663
660 def do_quit(self, arg): 664 def do_quit(self, arg):
661 return self._STOP_AND_EXIT 665 return self._STOP_AND_EXIT
662 do_exit = do_quit 666 do_exit = do_quit
663 do_q = do_quit 667 do_q = do_quit
800 def do_load(self, fname=None): 804 def do_load(self, fname=None):
801 """Runs command(s) from a file.""" 805 """Runs command(s) from a file."""
802 if fname is None: 806 if fname is None:
803 fname = self.defaultFileName 807 fname = self.defaultFileName
804 fname = os.path.expanduser(fname) 808 fname = os.path.expanduser(fname)
805 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) 809 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationprompt'))
806 if isinstance(fname, file): 810 if isinstance(fname, file):
807 self.stdin = fname 811 self.stdin = fname
808 else: 812 else:
809 try: 813 try:
810 self.stdin = open(os.path.expanduser(fname), 'r') 814 self.stdin = open(os.path.expanduser(fname), 'r')
814 except IOError: 818 except IOError:
815 print 'Problem opening file %s: \n%s' % (fname, e) 819 print 'Problem opening file %s: \n%s' % (fname, e)
816 keepstate.restore() 820 keepstate.restore()
817 return 821 return
818 self.use_rawinput = False 822 self.use_rawinput = False
819 self.prompt = self.continuationPrompt = '' 823 self.prompt = self.continuationprompt = ''
820 stop = self.cmdloop() 824 stop = self.cmdloop()
821 self.stdin.close() 825 self.stdin.close()
822 keepstate.restore() 826 keepstate.restore()
823 self.lastcmd = '' 827 self.lastcmd = ''
824 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT 828 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT
986 while True: 990 while True:
987 while not line.startswith(self.cmdapp.prompt): 991 while not line.startswith(self.cmdapp.prompt):
988 line = self.transcript.next() 992 line = self.transcript.next()
989 command = [line[len(self.cmdapp.prompt):]] 993 command = [line[len(self.cmdapp.prompt):]]
990 line = self.transcript.next() 994 line = self.transcript.next()
991 while line.startswith(self.cmdapp.continuationPrompt): 995 while line.startswith(self.cmdapp.continuationprompt):
992 command.append(line[len(self.cmdapp.continuationPrompt):]) 996 command.append(line[len(self.cmdapp.continuationprompt):])
993 line = self.transcript.next() 997 line = self.transcript.next()
994 command = ''.join(command) 998 command = ''.join(command)
995 self.cmdapp.onecmd(command) 999 self.cmdapp.onecmd(command)
996 result = self.outputTrap.read() 1000 result = self.outputTrap.read()
997 if line.startswith(self.cmdapp.prompt): 1001 if line.startswith(self.cmdapp.prompt):