comparison cmd2.py @ 155:8ba5127167f5

file output working again
author catherine@dellzilla
date Fri, 21 Nov 2008 17:05:26 -0500
parents 606ad25c7f7e
children 7e5a57df88aa
comparison
equal deleted inserted replaced
154:606ad25c7f7e 155:8ba5127167f5
252 - command: command 252 - command: command
253 - terminator: ; 253 - terminator: ;
254 - suffix: and suffix 254 - suffix: and suffix
255 - terminator: ; 255 - terminator: ;
256 >>> print c.parser.parseString('simple | piped').dump() 256 >>> print c.parser.parseString('simple | piped').dump()
257 ['simple', '', '|', ' piped'] 257 ['simple', '', '|', 'piped']
258 - args: 258 - args:
259 - command: simple 259 - command: simple
260 - pipeDest: piped 260 - pipeTo: piped
261 - statement: ['simple', ''] 261 - statement: ['simple', '']
262 - args: 262 - args:
263 - command: simple 263 - command: simple
264 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump() 264 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump()
265 ['command', 'with args, terminator', ';', 'sufx', '|', ' piped'] 265 ['command', 'with args, terminator', ';', 'sufx', '|', 'piped']
266 - args: with args, terminator 266 - args: with args, terminator
267 - command: command 267 - command: command
268 - pipeDest: piped 268 - pipeTo: piped
269 - statement: ['command', 'with args, terminator', ';'] 269 - statement: ['command', 'with args, terminator', ';']
270 - args: with args, terminator 270 - args: with args, terminator
271 - command: command 271 - command: command
272 - terminator: ; 272 - terminator: ;
273 - suffix: sufx 273 - suffix: sufx
274 - terminator: ; 274 - terminator: ;
275 >>> print c.parser.parseString('output into > afile.txt').dump() 275 >>> print c.parser.parseString('output into > afile.txt').dump()
276 ['output', ' into', '>', ' afile.txt'] 276 ['output', ' into', '>', 'afile.txt']
277 - args: into 277 - args: into
278 - command: output 278 - command: output
279 - output: > 279 - output: >
280 - outputDest: afile.txt 280 - outputTo: afile.txt
281 - statement: ['output', ' into'] 281 - statement: ['output', ' into']
282 - args: into 282 - args: into
283 - command: output 283 - command: output
284 >>> print c.parser.parseString('output into;sufx | pipethrume plz > afile.txt').dump() 284 >>> print c.parser.parseString('output into;sufx | pipethrume plz > afile.txt').dump()
285 ['output', 'into', ';', 'sufx', '|', ' pipethrume plz', '>', ' afile.txt'] 285 ['output', 'into', ';', 'sufx', '|', 'pipethrume plz', '>', 'afile.txt']
286 - args: into 286 - args: into
287 - command: output 287 - command: output
288 - output: > 288 - output: >
289 - outputDest: afile.txt 289 - outputTo: afile.txt
290 - pipeDest: pipethrume plz 290 - pipeTo: pipethrume plz
291 - statement: ['output', 'into', ';'] 291 - statement: ['output', 'into', ';']
292 - args: into 292 - args: into
293 - command: output 293 - command: output
294 - terminator: ; 294 - terminator: ;
295 - suffix: sufx 295 - suffix: sufx
297 >>> print c.parser.parseString('output to paste buffer >> ').dump() 297 >>> print c.parser.parseString('output to paste buffer >> ').dump()
298 ['output', ' to paste buffer', '>>', ''] 298 ['output', ' to paste buffer', '>>', '']
299 - args: to paste buffer 299 - args: to paste buffer
300 - command: output 300 - command: output
301 - output: >> 301 - output: >>
302 - outputDest:
303 - statement: ['output', ' to paste buffer'] 302 - statement: ['output', ' to paste buffer']
304 - args: to paste buffer 303 - args: to paste buffer
305 - command: output 304 - command: output
306 >>> print c.parser.parseString('ignore the /* commented | > */ stuff;').dump() 305 >>> print c.parser.parseString('ignore the /* commented | > */ stuff;').dump()
307 ['ignore', 'the /* commented | > */ stuff', ';', ''] 306 ['ignore', 'the /* commented | > */ stuff', ';', '']
347 - args: command /* with comment complete */ is done 346 - args: command /* with comment complete */ is done
348 - multilineCommand: multiline 347 - multilineCommand: multiline
349 - terminator: ; 348 - terminator: ;
350 - suffix: 349 - suffix:
351 - terminator: ; 350 - terminator: ;
352 >>> print c.parsed('hello <').dump()
353 ''' 351 '''
354 outputParser = pyparsing.oneOf(['>>','>'])('output') 352 outputParser = pyparsing.oneOf(['>>','>'])('output')
355 terminatorParser = pyparsing.oneOf(self.terminators)('terminator') 353 terminatorParser = pyparsing.oneOf(self.terminators)('terminator')
356 stringEnd = pyparsing.stringEnd ^ '\nEOF' 354 stringEnd = pyparsing.stringEnd ^ '\nEOF'
357 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') 355 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand')
358 oneLineCommand = pyparsing.Word(pyparsing.printables)('command') 356 oneLineCommand = pyparsing.Word(pyparsing.printables)('command')
359 afterElements = \ 357 afterElements = \
360 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 358 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ stringEnd).setParseAction(lambda x: [y.strip() for y in x])('pipeTo')) + \
361 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd)('outputTo')) 359 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
362 if self.caseInsensitive: 360 if self.caseInsensitive:
363 multilineCommand.setParseAction(lambda x: x[0].lower()) 361 multilineCommand.setParseAction(lambda x: x[0].lower())
364 oneLineCommand.setParseAction(lambda x: x[0].lower()) 362 oneLineCommand.setParseAction(lambda x: x[0].lower())
365 self.parser = ( 363 self.parser = (
366 (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser)('args') + terminatorParser)('statement') + 364 (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser)('args') + terminatorParser)('statement') +
465 self.stdout.seek(0) 463 self.stdout.seek(0)
466 try: 464 try:
467 writeToPasteBuffer(self.stdout.read()) 465 writeToPasteBuffer(self.stdout.read())
468 except Exception, e: 466 except Exception, e:
469 print str(e) 467 print str(e)
470 elif statement.pipe: 468 elif statement.pipeTo:
471 for result in redirect.communicate(): 469 for result in redirect.communicate():
472 statekeeper.stdout.write(result or '') 470 statekeeper.stdout.write(result or '')
473 self.stdout.close() 471 self.stdout.close()
474 statekeeper.restore() 472 statekeeper.restore()
475 473