comparison cmd2.py @ 415:d858a03fbdbb

replacing self.perror();return with raise for catchability
author Catherine Devlin <catherine.devlin@gmail.com>
date Wed, 17 Nov 2010 21:59:01 -0500
parents 731f2c93c1cd
children a9f936346399
comparison
equal deleted inserted replaced
414:731f2c93c1cd 415:d858a03fbdbb
793 self.kept_sys = Statekeeper(sys, ('stdout',)) 793 self.kept_sys = Statekeeper(sys, ('stdout',))
794 self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 794 self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
795 sys.stdout = self.stdout = self.redirect.stdin 795 sys.stdout = self.stdout = self.redirect.stdin
796 elif statement.parsed.output: 796 elif statement.parsed.output:
797 if (not statement.parsed.outputTo) and (not can_clip): 797 if (not statement.parsed.outputTo) and (not can_clip):
798 self.perror('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable') 798 raise EnvironmentError('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable')
799 return
800 self.kept_state = Statekeeper(self, ('stdout',)) 799 self.kept_state = Statekeeper(self, ('stdout',))
801 self.kept_sys = Statekeeper(sys, ('stdout',)) 800 self.kept_sys = Statekeeper(sys, ('stdout',))
802 if statement.parsed.outputTo: 801 if statement.parsed.outputTo:
803 mode = 'w' 802 mode = 'w'
804 if statement.parsed.output == '>>': 803 if statement.parsed.output == '>>':
975 if opts.long: 974 if opts.long:
976 self.poutput('%s # %s' % (result[p].ljust(maxlen), self.settable[p])) 975 self.poutput('%s # %s' % (result[p].ljust(maxlen), self.settable[p]))
977 else: 976 else:
978 self.poutput(result[p]) 977 self.poutput(result[p])
979 else: 978 else:
980 self.perror("Parameter '%s' not supported (type 'show' for list of parameters)." % param) 979 raise NotImplementedError("Parameter '%s' not supported (type 'show' for list of parameters)." % param)
981 980
982 def do_set(self, arg): 981 def do_set(self, arg):
983 ''' 982 '''
984 Sets a cmd2 parameter. Accepts abbreviated parameter names so long 983 Sets a cmd2 parameter. Accepts abbreviated parameter names so long
985 as there is no ambiguity. Call without arguments for a list of 984 as there is no ambiguity. Call without arguments for a list of
1109 1108
1110 commands are run after editor is closed. 1109 commands are run after editor is closed.
1111 "set edit (program-name)" or set EDITOR environment variable 1110 "set edit (program-name)" or set EDITOR environment variable
1112 to control which editing program is used.""" 1111 to control which editing program is used."""
1113 if not self.editor: 1112 if not self.editor:
1114 self.perror("Please use 'set editor' to specify your text editing program of choice.") 1113 raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
1115 return
1116 filename = self.default_file_name 1114 filename = self.default_file_name
1117 if arg: 1115 if arg:
1118 try: 1116 try:
1119 buffer = self.last_matching(int(arg)) 1117 buffer = self.last_matching(int(arg))
1120 except ValueError: 1118 except ValueError:
1144 | most recent command if omitted""" 1142 | most recent command if omitted"""
1145 1143
1146 try: 1144 try:
1147 args = self.saveparser.parseString(arg) 1145 args = self.saveparser.parseString(arg)
1148 except pyparsing.ParseException: 1146 except pyparsing.ParseException:
1149 self.perror(self.do_save.__doc__) 1147 self.perror('Could not understand save target %s' % arg)
1150 return 1148 raise SyntaxError(self.do_save.__doc__)
1151 fname = args.fname or self.default_file_name 1149 fname = args.fname or self.default_file_name
1152 if args.idx == '*': 1150 if args.idx == '*':
1153 saveme = '\n\n'.join(self.history[:]) 1151 saveme = '\n\n'.join(self.history[:])
1154 elif args.idx: 1152 elif args.idx:
1155 saveme = self.history[int(args.idx)-1] 1153 saveme = self.history[int(args.idx)-1]
1159 f = open(os.path.expanduser(fname), 'w') 1157 f = open(os.path.expanduser(fname), 'w')
1160 f.write(saveme) 1158 f.write(saveme)
1161 f.close() 1159 f.close()
1162 self.pfeedback('Saved to %s' % (fname)) 1160 self.pfeedback('Saved to %s' % (fname))
1163 except Exception, e: 1161 except Exception, e:
1164 self.perror('Error saving %s: %s' % (fname, str(e))) 1162 self.perror('Error saving %s' % (fname))
1163 raise
1165 1164
1166 def read_file_or_url(self, fname): 1165 def read_file_or_url(self, fname):
1167 # TODO: not working on localhost 1166 # TODO: not working on localhost
1168 if isinstance(fname, file): 1167 if isinstance(fname, file):
1169 result = open(fname, 'r') 1168 result = open(fname, 'r')