comparison cmd2.py @ 215:e9091d9a3a60

now hooks for pre and post but after parse
author catherine@Elli.myhome.westell.com
date Mon, 16 Mar 2009 03:30:30 -0400
parents 68c03076c9e7
children dcc5f871a5b6
comparison
equal deleted inserted replaced
214:68c03076c9e7 215:e9091d9a3a60
277 def do_shortcuts(self, args): 277 def do_shortcuts(self, args):
278 """Lists single-key shortcuts available.""" 278 """Lists single-key shortcuts available."""
279 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 279 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
280 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 280 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
281 281
282 prefixParser = pyparsing.Empty()
282 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) 283 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
283 commentGrammars.addParseAction(lambda x: '') 284 commentGrammars.addParseAction(lambda x: '')
284 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) 285 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd)
285 terminators = [';'] 286 terminators = [';']
286 blankLinesAllowed = False 287 blankLinesAllowed = False
472 #self.multilineParser = self.multilineParser.setResultsName('multilineParser') 473 #self.multilineParser = self.multilineParser.setResultsName('multilineParser')
473 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') 474 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser')
474 #self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('blankLineTerminatorParser') 475 #self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('blankLineTerminatorParser')
475 self.parser = ( 476 self.parser = (
476 stringEnd | 477 stringEnd |
477 self.multilineParser | 478 self.prefixParser + self.multilineParser |
478 self.singleLineParser | 479 self.prefixParser + self.singleLineParser |
479 self.blankLineTerminationParser | 480 self.prefixParser + self.blankLineTerminationParser |
480 self.multilineCommand + pyparsing.SkipTo(stringEnd) 481 self.prefixParser + self.multilineCommand + pyparsing.SkipTo(stringEnd)
481 ) 482 )
482 self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 483 self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
483 484
484 inputMark = pyparsing.Literal('<') 485 inputMark = pyparsing.Literal('<')
485 inputMark.setParseAction(lambda x: '') 486 inputMark.setParseAction(lambda x: '')
511 p.parser = self.parsed 512 p.parser = self.parsed
512 for (key, val) in kwargs.items(): 513 for (key, val) in kwargs.items():
513 p.parsed[key] = val 514 p.parsed[key] = val
514 return p 515 return p
515 516
517 def postparsing_precmd(self, statement):
518 stop = 0
519 return stop, statement
520 def postparsing_postcmd(self, stop):
521 return stop
516 def onecmd(self, line): 522 def onecmd(self, line):
517 """Interpret the argument as though it had been typed in response 523 """Interpret the argument as though it had been typed in response
518 to the prompt. 524 to the prompt.
519 525
520 This may be overridden, but should not normally need to be; 526 This may be overridden, but should not normally need to be;
537 statement = self.parsed(statement) 543 statement = self.parsed(statement)
538 except Exception, e: 544 except Exception, e:
539 print e 545 print e
540 return 0 546 return 0
541 547
548 (stop, statement) = self.postparsing_precmd(statement)
549 if stop:
550 return self.postparsing_postcmd(stop)
551
542 if not statement.parsed.command: 552 if not statement.parsed.command:
543 return 0 553 return self.postparsing_postcmd(stop=0)
544 554
545 statekeeper = None 555 statekeeper = None
546 stop = 0
547 556
548 if statement.parsed.pipeTo: 557 if statement.parsed.pipeTo:
549 redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 558 redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
550 statekeeper = Statekeeper(self, ('stdout',)) 559 statekeeper = Statekeeper(self, ('stdout',))
551 self.stdout = redirect.stdin 560 self.stdout = redirect.stdin
557 mode = 'a' 566 mode = 'a'
558 try: 567 try:
559 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) 568 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)
560 except OSError, e: 569 except OSError, e:
561 print e 570 print e
562 return 0 571 return self.postparsing_postcmd(stop=0)
563 else: 572 else:
564 statekeeper = Statekeeper(self, ('stdout',)) 573 statekeeper = Statekeeper(self, ('stdout',))
565 self.stdout = tempfile.TemporaryFile() 574 self.stdout = tempfile.TemporaryFile()
566 if statement.parsed.output == '>>': 575 if statement.parsed.output == '>>':
567 self.stdout.write(getPasteBuffer()) 576 self.stdout.write(getPasteBuffer())
569 # "heart" of the command, replace's cmd's onecmd() 578 # "heart" of the command, replace's cmd's onecmd()
570 self.lastcmd = statement.parsed.expanded 579 self.lastcmd = statement.parsed.expanded
571 try: 580 try:
572 func = getattr(self, 'do_' + statement.parsed.command) 581 func = getattr(self, 'do_' + statement.parsed.command)
573 except AttributeError: 582 except AttributeError:
574 return self.default(statement) 583 return self.postparsing_postcmd(self.default(statement))
575 timestart = datetime.datetime.now() 584 timestart = datetime.datetime.now()
576 stop = func(statement) 585 stop = func(statement)
577 if self.timing: 586 if self.timing:
578 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart) 587 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart)
579 except Exception, e: 588 except Exception, e:
593 for result in redirect.communicate(): 602 for result in redirect.communicate():
594 statekeeper.stdout.write(result or '') 603 statekeeper.stdout.write(result or '')
595 self.stdout.close() 604 self.stdout.close()
596 statekeeper.restore() 605 statekeeper.restore()
597 606
598 return stop 607 return self.postparsing_postcmd(stop)
599 608
600 def pseudo_raw_input(self, prompt): 609 def pseudo_raw_input(self, prompt):
601 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout""" 610 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
602 611
603 if self.use_rawinput: 612 if self.use_rawinput: