comparison cmd2.py @ 219:35be1f8332a9

py object manipulation almost done
author catherine@dellzilla
date Wed, 18 Mar 2009 15:32:08 -0400
parents 5a45cf2e7455
children 8c277a37e2bc
comparison
equal deleted inserted replaced
218:5a45cf2e7455 219:35be1f8332a9
271 271
272 def __init__(self, *args, **kwargs): 272 def __init__(self, *args, **kwargs):
273 cmd.Cmd.__init__(self, *args, **kwargs) 273 cmd.Cmd.__init__(self, *args, **kwargs)
274 self.history = History() 274 self.history = History()
275 self._init_parser() 275 self._init_parser()
276 self.pyenvironment = {} 276 self.pystate = {}
277 277
278 def do_shortcuts(self, args): 278 def do_shortcuts(self, args):
279 """Lists single-key shortcuts available.""" 279 """Lists single-key shortcuts available."""
280 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 280 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
281 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 281 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
492 # as in "lesser than" 492 # as in "lesser than"
493 self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \ 493 self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \
494 pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|') 494 pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|')
495 self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 495 self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
496 496
497 def preparse(self, raw, **kwargs):
498 return raw
499
497 def parsed(self, raw, **kwargs): 500 def parsed(self, raw, **kwargs):
498 if isinstance(raw, ParsedString): 501 if isinstance(raw, ParsedString):
499 p = raw 502 p = raw
500 else: 503 else:
504 raw = self.preparse(raw, **kwargs)
501 s = self.inputParser.transformString(raw.lstrip()) 505 s = self.inputParser.transformString(raw.lstrip())
502 for (shortcut, expansion) in self.shortcuts.items(): 506 for (shortcut, expansion) in self.shortcuts.items():
503 if s.lower().startswith(shortcut): 507 if s.lower().startswith(shortcut):
504 s = s.replace(shortcut, expansion + ' ', 1) 508 s = s.replace(shortcut, expansion + ' ', 1)
505 break 509 break
739 743
740 def do_py(self, arg): 744 def do_py(self, arg):
741 '''Executes a python command''' 745 '''Executes a python command'''
742 if arg.strip(): 746 if arg.strip():
743 try: 747 try:
744 exec(arg, self.pyenvironment) 748 result = eval(arg, self.pystate)
749 if result is None:
750 exec(arg, self.pystate)
751 else:
752 print result
745 except Exception, e: 753 except Exception, e:
746 print e 754 print e
747 else: 755 else:
748 print 'Now accepting python commands; end with `\\py`' 756 print 'Now accepting python commands; end with `\\py`'
749 buffer = [self.pseudo_raw_input('>>> ')] 757 buffer = [self.pseudo_raw_input('>>> ')]
750 while not buffer[-1].strip().startswith('\\py'): 758 while not buffer[-1].strip().startswith('\\py'):
751 try: 759 try:
752 exec('\n'.join(buffer), self.pyenvironment) 760 buf = '\n'.join(buffer)
761 result = eval(buf, self.pystate)
762 if result is None:
763 exec(buf, self.pystate)
764 else:
765 print result
753 buffer = [self.pseudo_raw_input('>>> ')] 766 buffer = [self.pseudo_raw_input('>>> ')]
754 except SyntaxError: 767 except SyntaxError:
755 buffer.append(self.pseudo_raw_input('... ')) 768 buffer.append(self.pseudo_raw_input('... '))
769 except Exception, e:
770 print e
771 buffer = [self.pseudo_raw_input('>>> ')]
756 772
757 def do_history(self, arg): 773 def do_history(self, arg):
758 """history [arg]: lists past commands issued 774 """history [arg]: lists past commands issued
759 775
760 no arg -> list all 776 no arg -> list all