comparison cmd2.py @ 300:1e4773b325d1

assume newfunc does not have to accept unparsed arguments
author catherine@dellzilla
date Tue, 26 Jan 2010 17:39:29 -0500
parents 7d256086fa1b
children 30af90fd46c5
comparison
equal deleted inserted replaced
299:7d256086fa1b 300:1e4773b325d1
218 writeToPasteBuffer = get_paste_buffer 218 writeToPasteBuffer = get_paste_buffer
219 219
220 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') 220 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
221 221
222 class ParsedString(str): 222 class ParsedString(str):
223 pass 223 def full_parsed_statement(self):
224 new = ParsedString('%s %s' % (self.parsed.command, self.parsed.args))
225 new.parsed = self.parsed
226 new.parser = self.parser
227 return new
224 228
225 class SkipToLast(pyparsing.SkipTo): 229 class SkipToLast(pyparsing.SkipTo):
226 def parseImpl( self, instring, loc, doActions=True ): 230 def parseImpl( self, instring, loc, doActions=True ):
227 resultStore = [] 231 resultStore = []
228 startLoc = loc 232 startLoc = loc
630 def parsed(self, raw, **kwargs): 634 def parsed(self, raw, **kwargs):
631 if isinstance(raw, ParsedString): 635 if isinstance(raw, ParsedString):
632 p = raw 636 p = raw
633 else: 637 else:
634 # preparse is an overridable hook; default makes no changes 638 # preparse is an overridable hook; default makes no changes
635 raw = self.preparse(raw, **kwargs) 639 s = self.preparse(raw, **kwargs)
636 s = self.inputParser.transformString(raw.lstrip()) 640 s = self.inputParser.transformString(s.lstrip())
641 s = self.commentGrammars.transformString(s)
637 for (shortcut, expansion) in self.shortcuts: 642 for (shortcut, expansion) in self.shortcuts:
638 if s.lower().startswith(shortcut): 643 if s.lower().startswith(shortcut):
639 s = s.replace(shortcut, expansion + ' ', 1) 644 s = s.replace(shortcut, expansion + ' ', 1)
640 break 645 break
641 result = self.parser.parseString(s) 646 result = self.parser.parseString(s)
647 result['raw'] = raw
642 result['command'] = result.multilineCommand or result.command 648 result['command'] = result.multilineCommand or result.command
643 result['raw'] = raw 649 p = ParsedString(result.args)
644 result['comments_removed'] = self.commentGrammars.transformString(result.args)
645 result['expanded'] = s
646 p = ParsedString(result.comments_removed)
647 p.parsed = result 650 p.parsed = result
648 p.parser = self.parsed 651 p.parser = self.parsed
649 for (key, val) in kwargs.items(): 652 for (key, val) in kwargs.items():
650 p.parsed[key] = val 653 p.parsed[key] = val
651 return p 654 return p
728 if statement.parsed.output == '>>': 731 if statement.parsed.output == '>>':
729 self.stdout.write(get_paste_buffer()) 732 self.stdout.write(get_paste_buffer())
730 try: 733 try:
731 try: 734 try:
732 # "heart" of the command, replaces cmd's onecmd() 735 # "heart" of the command, replaces cmd's onecmd()
733 self.lastcmd = statement.parsed.expanded 736 self.lastcmd = statement.parsed.raw
734 funcname = self.func_named(statement.parsed.command) 737 funcname = self.func_named(statement.parsed.command)
735 full_statement = ParsedString(statement.parsed.raw)
736 full_statement.parsed = statement.parsed
737 full_statement.parser = statement.parser
738 if not funcname: 738 if not funcname:
739 return self.postparsing_postcmd(self.default(full_statement)) 739 return self.postparsing_postcmd(self.default(statement.full_parsed_statement()))
740 try: 740 try:
741 func = getattr(self, funcname) 741 func = getattr(self, funcname)
742 except AttributeError: 742 except AttributeError:
743 return self.postparsing_postcmd(self.default(full_statement)) 743 return self.postparsing_postcmd(self.default(statement.full_parsed_statement()))
744 timestart = datetime.datetime.now() 744 timestart = datetime.datetime.now()
745 stop = func(statement) 745 stop = func(statement)
746 if self.timing: 746 if self.timing:
747 self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart)) 747 self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart))
748 except Exception, e: 748 except Exception, e: