comparison cmd2.py @ 50:dccf27f52f51

unifying pipe and redirect
author catherine@localhost
date Mon, 09 Jun 2008 10:05:25 -0400
parents 8926b72d828e
children 26e33f64f68e
comparison
equal deleted inserted replaced
49:8926b72d828e 50:dccf27f52f51
232 The return value is a flag indicating whether interpretation of 232 The return value is a flag indicating whether interpretation of
233 commands by the interpreter should stop. 233 commands by the interpreter should stop.
234 234
235 """ 235 """
236 command, args = self.extractCommand(line) 236 command, args = self.extractCommand(line)
237 statement = ' '.join([command, args]) 237 statement = originalStatement = ' '.join([command, args])
238 if (not assumeComplete) and (command in self.multilineCommands): 238 if (not assumeComplete) and (command in self.multilineCommands):
239 statement = self.finishStatement(statement) 239 statement = self.finishStatement(statement)
240 statekeeper = None 240 statekeeper = None
241 stop = 0 241 stop = 0
242 statement, redirect = self.findPipe(statement) 242 statement, redirect = self.findPipe(statement)
244 statekeeper = Statekeeper(self, ('stdout',)) 244 statekeeper = Statekeeper(self, ('stdout',))
245 redirect = subprocess.Popen(redirect, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 245 redirect = subprocess.Popen(redirect, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
246 self.stdout = redirect.stdin 246 self.stdout = redirect.stdin
247 stop = cmd.Cmd.onecmd(self, statement) 247 stop = cmd.Cmd.onecmd(self, statement)
248 statekeeper.restore() 248 statekeeper.restore()
249 for result in redirect.communicate(): 249 for result in redirect.communicate():
250 self.stdout.write(result or '') 250 self.stdout.write(result or '')
251 return stop # didn't record in history 251 if command not in self.excludeFromHistory:
252 self.history.append(originalStatement)
253 return stop
252 else: 254 else:
253 statement, redirect, mode = self.parseRedirectors(statement) 255 statement, redirect, mode = self.parseRedirectors(statement)
254 if redirect == self._TO_PASTE_BUFFER: 256 if redirect == self._TO_PASTE_BUFFER:
255 try: 257 try:
256 clipcontents = getPasteBuffer() 258 clipcontents = getPasteBuffer()
271 else: 273 else:
272 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) 274 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))
273 stop = cmd.Cmd.onecmd(self, statement) 275 stop = cmd.Cmd.onecmd(self, statement)
274 try: 276 try:
275 if command not in self.excludeFromHistory: 277 if command not in self.excludeFromHistory:
276 self.history.append(statement) # or should we append the unmodified statement? 278 self.history.append(originalStatement)
277 finally: 279 finally:
278 if statekeeper: 280 if statekeeper:
279 if redirect == self._TO_PASTE_BUFFER: 281 if redirect == self._TO_PASTE_BUFFER:
280 self.stdout.seek(0) 282 self.stdout.seek(0)
281 writeToPasteBuffer(self.stdout.read()) 283 writeToPasteBuffer(self.stdout.read())
286 statementEndPattern = re.compile(r'[%s]\s*$' % terminators) 288 statementEndPattern = re.compile(r'[%s]\s*$' % terminators)
287 def statementHasEnded(self, lines): 289 def statementHasEnded(self, lines):
288 #import pdb; pdb.set_trace() 290 #import pdb; pdb.set_trace()
289 return bool(self.statementEndPattern.search(lines)) \ 291 return bool(self.statementEndPattern.search(lines)) \
290 or lines[-3:] == 'EOF' \ 292 or lines[-3:] == 'EOF' \
293 or self.findPipe(lines)[1] \
291 or self.parseRedirectors(lines)[1] 294 or self.parseRedirectors(lines)[1]
292 295
293 def finishStatement(self, firstline): 296 def finishStatement(self, firstline):
294 statement = firstline 297 statement = firstline
295 while not self.statementHasEnded(statement): 298 while not self.statementHasEnded(statement):