Mercurial > python-cmd2
comparison cmd2.py @ 51:26e33f64f68e
trying new unified pipe and redirect
author | catherine@localhost |
---|---|
date | Mon, 09 Jun 2008 10:20:38 -0400 |
parents | dccf27f52f51 |
children | 49de899a05a8 |
comparison
equal
deleted
inserted
replaced
50:dccf27f52f51 | 51:26e33f64f68e |
---|---|
176 | 176 |
177 notAPipe = pyparsing.SkipTo('|') | 177 notAPipe = pyparsing.SkipTo('|') |
178 notAPipe.ignore(pyparsing.sglQuotedString) | 178 notAPipe.ignore(pyparsing.sglQuotedString) |
179 notAPipe.ignore(pyparsing.dblQuotedString) | 179 notAPipe.ignore(pyparsing.dblQuotedString) |
180 pipeFinder = notAPipe + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) | 180 pipeFinder = notAPipe + '|' + pyparsing.SkipTo(pyparsing.StringEnd()) |
181 def findPipe(self, statement): | 181 def parsePipe(self, statement, mustBeTerminated): |
182 try: | 182 try: |
183 statement, pipe, destination = self.pipeFinder.parseString(statement) | 183 statement, pipe, destination = self.pipeFinder.parseString(statement) |
184 return statement, destination | 184 redirect = subprocess.Popen(destination, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) |
185 return statement, redirect | |
185 except pyparsing.ParseException: | 186 except pyparsing.ParseException: |
186 return statement, None | 187 return statement, None |
187 | 188 |
188 legalFileName = re.compile(r'''^[^"'\s]+$''') | 189 legalFileName = re.compile(r'''^[^"'\s]+$''') |
189 def parseRedirector(self, statement, symbol, mustBeTerminated=False): | 190 def parseRedirector(self, statement, symbol, mustBeTerminated=False): |
210 command = command.lower() | 211 command = command.lower() |
211 return command, args | 212 return command, args |
212 | 213 |
213 def parseRedirectors(self, statement): | 214 def parseRedirectors(self, statement): |
214 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands | 215 mustBeTerminated = self.extractCommand(statement)[0] in self.multilineCommands |
216 newStatement, redirect = self.parsePipe(statement, mustBeTerminated) | |
217 if redirect: | |
218 return newStatement, redirect, 'pipe' | |
215 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated) | 219 newStatement, redirect = self.parseRedirector(statement, '>>', mustBeTerminated) |
216 if redirect: | 220 if redirect: |
217 return newStatement, redirect, 'a' | 221 return newStatement, redirect, 'a' |
218 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated) | 222 newStatement, redirect = self.parseRedirector(statement, '>', mustBeTerminated) |
219 if redirect: | 223 if redirect: |
237 statement = originalStatement = ' '.join([command, args]) | 241 statement = originalStatement = ' '.join([command, args]) |
238 if (not assumeComplete) and (command in self.multilineCommands): | 242 if (not assumeComplete) and (command in self.multilineCommands): |
239 statement = self.finishStatement(statement) | 243 statement = self.finishStatement(statement) |
240 statekeeper = None | 244 statekeeper = None |
241 stop = 0 | 245 stop = 0 |
242 statement, redirect = self.findPipe(statement) | 246 statement, redirect, mode = self.parseRedirectors(statement) |
243 if redirect: | 247 if isinstance(redirect, subprocess.Popen): |
244 statekeeper = Statekeeper(self, ('stdout',)) | 248 statekeeper = Statekeeper(self, ('stdout',)) |
245 redirect = subprocess.Popen(redirect, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
246 self.stdout = redirect.stdin | 249 self.stdout = redirect.stdin |
247 stop = cmd.Cmd.onecmd(self, statement) | 250 elif redirect == self._TO_PASTE_BUFFER: |
248 statekeeper.restore() | 251 try: |
249 for result in redirect.communicate(): | 252 clipcontents = getPasteBuffer() |
250 self.stdout.write(result or '') | 253 if mode in ('w', 'a'): |
251 if command not in self.excludeFromHistory: | |
252 self.history.append(originalStatement) | |
253 return stop | |
254 else: | |
255 statement, redirect, mode = self.parseRedirectors(statement) | |
256 if redirect == self._TO_PASTE_BUFFER: | |
257 try: | |
258 clipcontents = getPasteBuffer() | |
259 if mode in ('w', 'a'): | |
260 statekeeper = Statekeeper(self, ('stdout',)) | |
261 self.stdout = tempfile.TemporaryFile() | |
262 if mode == 'a': | |
263 self.stdout.write(clipcontents) | |
264 else: | |
265 statement = '%s %s' % (statement, clipcontents) | |
266 except OSError, e: | |
267 print e | |
268 return 0 | |
269 elif redirect: | |
270 if mode in ('w','a'): | |
271 statekeeper = Statekeeper(self, ('stdout',)) | 254 statekeeper = Statekeeper(self, ('stdout',)) |
272 self.stdout = open(redirect, mode) | 255 self.stdout = tempfile.TemporaryFile() |
256 if mode == 'a': | |
257 self.stdout.write(clipcontents) | |
273 else: | 258 else: |
274 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) | 259 statement = '%s %s' % (statement, clipcontents) |
260 except OSError, e: | |
261 print e | |
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: | |
268 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect)) | |
275 stop = cmd.Cmd.onecmd(self, statement) | 269 stop = cmd.Cmd.onecmd(self, statement) |
276 try: | 270 try: |
277 if command not in self.excludeFromHistory: | 271 if command not in self.excludeFromHistory: |
278 self.history.append(originalStatement) | 272 self.history.append(originalStatement) |
279 finally: | 273 finally: |
281 if redirect == self._TO_PASTE_BUFFER: | 275 if redirect == self._TO_PASTE_BUFFER: |
282 self.stdout.seek(0) | 276 self.stdout.seek(0) |
283 writeToPasteBuffer(self.stdout.read()) | 277 writeToPasteBuffer(self.stdout.read()) |
284 self.stdout.close() | 278 self.stdout.close() |
285 statekeeper.restore() | 279 statekeeper.restore() |
280 if isinstance(redirect, subprocess.Popen): | |
281 for result in redirect.communicate(): | |
282 self.stdout.write(result or '') | |
286 return stop | 283 return stop |
287 | 284 |
288 statementEndPattern = re.compile(r'[%s]\s*$' % terminators) | 285 statementEndPattern = re.compile(r'[%s]\s*$' % terminators) |
289 def statementHasEnded(self, lines): | 286 def statementHasEnded(self, lines): |
290 #import pdb; pdb.set_trace() | 287 #import pdb; pdb.set_trace() |