comparison cmd2.py @ 58:f20dcfa69341

uber-trap exceptions maybe working?
author catherine@localhost
date Tue, 10 Jun 2008 15:22:16 -0400
parents a791d615545c
children
comparison
equal deleted inserted replaced
55:698710e88d1b 58:f20dcfa69341
235 see the precmd() and postcmd() methods for useful execution hooks. 235 see the precmd() and postcmd() methods for useful execution hooks.
236 The return value is a flag indicating whether interpretation of 236 The return value is a flag indicating whether interpretation of
237 commands by the interpreter should stop. 237 commands by the interpreter should stop.
238 238
239 """ 239 """
240 command, args = self.extractCommand(line)
241 statement = originalStatement = ' '.join([command, args])
242 if (not assumeComplete) and (command in self.multilineCommands):
243 statement = self.finishStatement(statement)
244 statekeeper = None 240 statekeeper = None
245 stop = 0 241 stop = 0
246 statement, redirect, mode = self.parseRedirectors(statement) 242 try:
247 if isinstance(redirect, subprocess.Popen): 243 command, args = self.extractCommand(line)
248 statekeeper = Statekeeper(self, ('stdout',)) 244 statement = originalStatement = ' '.join([command, args])
249 self.stdout = redirect.stdin 245 if (not assumeComplete) and (command in self.multilineCommands):
250 elif redirect == self._TO_PASTE_BUFFER: 246 statement = self.finishStatement(statement)
251 try: 247 statement, redirect, mode = self.parseRedirectors(statement)
252 clipcontents = getPasteBuffer() 248 if isinstance(redirect, subprocess.Popen):
253 if mode in ('w', 'a'): 249 statekeeper = Statekeeper(self, ('stdout',))
250 self.stdout = redirect.stdin
251 elif redirect == self._TO_PASTE_BUFFER:
252 try:
253 clipcontents = getPasteBuffer()
254 if mode in ('w', 'a'):
255 statekeeper = Statekeeper(self, ('stdout',))
256 self.stdout = tempfile.TemporaryFile()
257 if mode == 'a':
258 self.stdout.write(clipcontents)
259 else:
260 statement = '%s %s' % (statement, clipcontents)
261 except OSError, e:
262 print e
263 return 0
264 elif redirect:
265 if mode in ('w','a'):
254 statekeeper = Statekeeper(self, ('stdout',)) 266 statekeeper = Statekeeper(self, ('stdout',))
255 self.stdout = tempfile.TemporaryFile() 267 self.stdout = open(redirect, mode)
256 if mode == 'a':
257 self.stdout.write(clipcontents)
258 else: 268 else:
259 statement = '%s %s' % (statement, clipcontents) 269 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))
260 except OSError, e: 270 if isinstance(redirect, subprocess.Popen):
261 print e 271 stop = self.onecmd(statement)
262 return 0
263 elif redirect:
264 if mode in ('w','a'):
265 statekeeper = Statekeeper(self, ('stdout',))
266 self.stdout = open(redirect, mode)
267 else: 272 else:
268 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) 273 stop = cmd.Cmd.onecmd(self, statement)
269 if isinstance(redirect, subprocess.Popen): 274
270 stop = self.onecmd(statement)
271 else:
272 stop = cmd.Cmd.onecmd(self, statement)
273 try:
274 if command not in self.excludeFromHistory: 275 if command not in self.excludeFromHistory:
275 self.history.append(originalStatement) 276 self.history.append(originalStatement)
277 except Exception, e:
278 print e
276 finally: 279 finally:
277 if statekeeper: 280 if statekeeper:
278 if redirect == self._TO_PASTE_BUFFER: 281 if redirect == self._TO_PASTE_BUFFER:
279 self.stdout.seek(0) 282 self.stdout.seek(0)
280 writeToPasteBuffer(self.stdout.read()) 283 writeToPasteBuffer(self.stdout.read())
281 elif isinstance(redirect, subprocess.Popen): 284 elif isinstance(redirect, subprocess.Popen):
282 for result in redirect.communicate(): 285 for result in redirect.communicate():
283 statekeeper.stdout.write(result or '') 286 statekeeper.stdout.write(result or '')
284 self.stdout.close() 287 self.stdout.close()
285 statekeeper.restore() 288 statekeeper.restore()
286
287 return stop 289 return stop
288 290
289 statementEndPattern = re.compile(r'[%s]\s*$' % terminators) 291 statementEndPattern = re.compile(r'[%s]\s*$' % terminators)
290 def statementHasEnded(self, lines): 292 def statementHasEnded(self, lines):
291 return bool(self.statementEndPattern.search(lines)) \ 293 return bool(self.statementEndPattern.search(lines)) \