comparison cmd2.py @ 282:7c6eb0fc75ef

added debug stack trace printing
author catherine@DellZilla
date Wed, 07 Oct 2009 14:24:14 -0400
parents 38198b11f48c
children bff1f19275e7
comparison
equal deleted inserted replaced
280:38198b11f48c 282:7c6eb0fc75ef
21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com 21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com
22 22
23 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 23 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2
24 """ 24 """
25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest 25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest
26 import unittest, string, datetime, urllib, glob 26 import unittest, string, datetime, urllib, glob, traceback
27 from code import InteractiveConsole, InteractiveInterpreter, softspace 27 from code import InteractiveConsole, InteractiveInterpreter, softspace
28 from optparse import make_option 28 from optparse import make_option
29 __version__ = '0.5.6' 29 __version__ = '0.5.6'
30 30
31 class OptionParser(optparse.OptionParser): 31 class OptionParser(optparse.OptionParser):
272 nonpythoncommand = 'cmd' 272 nonpythoncommand = 'cmd'
273 current_script_dir = None 273 current_script_dir = None
274 reserved_words = [] 274 reserved_words = []
275 feedback_to_output = False 275 feedback_to_output = False
276 quiet = False 276 quiet = False
277 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', 277 debug = False
278 settable = ['prompt', 'continuation_prompt', 'debug', 'default_file_name', 'editor',
278 'case_insensitive', 'feedback_to_output', 'quiet', 'echo', 'timing', 279 'case_insensitive', 'feedback_to_output', 'quiet', 'echo', 'timing',
279 'abbrev'] 280 'abbrev']
280 settable.sort() 281 settable.sort()
281 282
282 def poutput(self, msg): 283 def poutput(self, msg):
283 self.stdout.write(msg) 284 self.stdout.write(msg)
284 if msg[-1] != '\n': 285 if msg[-1] != '\n':
285 self.stdout.write('\n') 286 self.stdout.write('\n')
286 def perror(self, errmsg, statement=None): 287 def perror(self, errmsg, statement=None):
288 if self.debug:
289 traceback.print_exc()
287 print str(errmsg) 290 print str(errmsg)
288 def pfeedback(self, msg): 291 def pfeedback(self, msg):
289 """For printing nonessential feedback. Can be silenced with `quiet`. 292 """For printing nonessential feedback. Can be silenced with `quiet`.
290 Inclusion in redirected output is controlled by `feedback_to_output`.""" 293 Inclusion in redirected output is controlled by `feedback_to_output`."""
291 if not self.quiet: 294 if not self.quiet:
648 mode = 'w' 651 mode = 'w'
649 if statement.parsed.output == '>>': 652 if statement.parsed.output == '>>':
650 mode = 'a' 653 mode = 'a'
651 try: 654 try:
652 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) 655 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)
653 except OSError, e: 656 except Exception, e:
654 self.perror(e) 657 self.perror(e)
655 return self.postparsing_postcmd(stop=0) 658 return self.postparsing_postcmd(stop=0)
656 else: 659 else:
657 statekeeper = Statekeeper(self, ('stdout',)) 660 statekeeper = Statekeeper(self, ('stdout',))
658 self.stdout = tempfile.TemporaryFile() 661 self.stdout = tempfile.TemporaryFile()
707 line = 'EOF' 710 line = 'EOF'
708 else: 711 else:
709 if line[-1] == '\n': # this was always true in Cmd 712 if line[-1] == '\n': # this was always true in Cmd
710 line = line[:-1] 713 line = line[:-1]
711 return line 714 return line
712 715
713 def cmdloop(self, intro=None): 716 def cmdloop(self, intro=None):
714 """Repeatedly issue a prompt, accept input, parse an initial prefix 717 """Repeatedly issue a prompt, accept input, parse an initial prefix
715 off the received input, and dispatch to action methods, passing them 718 off the received input, and dispatch to action methods, passing them
716 the remainder of the line as argument. 719 the remainder of the line as argument.
717 """ 720 """
1096 1099
1097 class Statekeeper(object): 1100 class Statekeeper(object):
1098 def __init__(self, obj, attribs): 1101 def __init__(self, obj, attribs):
1099 self.obj = obj 1102 self.obj = obj
1100 self.attribs = attribs 1103 self.attribs = attribs
1101 self.save() 1104 if self.obj:
1105 self.save()
1102 def save(self): 1106 def save(self):
1103 for attrib in self.attribs: 1107 for attrib in self.attribs:
1104 setattr(self, attrib, getattr(self.obj, attrib)) 1108 setattr(self, attrib, getattr(self.obj, attrib))
1105 def restore(self): 1109 def restore(self):
1106 for attrib in self.attribs: 1110 if self.obj:
1107 setattr(self.obj, attrib, getattr(self, attrib)) 1111 for attrib in self.attribs:
1112 setattr(self.obj, attrib, getattr(self, attrib))
1108 1113
1109 class Borg(object): 1114 class Borg(object):
1110 '''All instances of any Borg subclass will share state. 1115 '''All instances of any Borg subclass will share state.
1111 from Python Cookbook, 2nd Ed., recipe 6.16''' 1116 from Python Cookbook, 2nd Ed., recipe 6.16'''
1112 _shared_state = {} 1117 _shared_state = {}