home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / pydb / pydbcmd.py < prev    next >
Encoding:
Python Source  |  1998-08-26  |  3.2 KB  |  107 lines

  1. # $Id: pydbcmd.py,v 1.2 1998/08/26 09:26:32 zeller Exp $
  2. # Support functions for ddd
  3. #
  4. # A generic class to build line-oriented command interpreters
  5. # Slightly modified version of Python library module cmd.py
  6.  
  7. import string
  8. import sys
  9. import linecache
  10.  
  11. PROMPT = '(Cmd) '
  12. IDENTCHARS = string.letters + string.digits + '_'
  13.  
  14. class Cmd:
  15.  
  16.     def __init__(self):
  17.         self.prompt = PROMPT
  18.         self.identchars = IDENTCHARS
  19.         self.lastcmd = ''
  20.  
  21.     def cmdloop(self):
  22.         stop = None
  23.         while not stop:
  24.             if self.cmdqueue:
  25.                 line = self.cmdqueue[0]
  26.                 del self.cmdqueue[0]
  27.             else:
  28.                 try:
  29.                     line = raw_input(self.prompt)
  30.                 except EOFError:
  31.                     # ignore EOF errors ... use quit to end loop
  32.                     continue
  33.             stop = self.onecmd(line)
  34.             stop = stop or self.alldie
  35.  
  36.     def onecmd(self, line):
  37.         line = string.strip(line)
  38.         if not line:
  39.             line = self.lastcmd
  40.         else:
  41.             self.lastcmd = line
  42.         if not line:
  43.             return 0
  44.         # Ignore comments
  45.         if line[0] == '#':
  46.             return 0
  47.         i, n = 0, len(line)
  48.         while i < n and line[i] in self.identchars: i = i+1
  49.         cmd, arg = line[:i], string.strip(line[i:])
  50.         if cmd == '':
  51.             return self.default(line)
  52.         else:
  53.             try:
  54.                 func = getattr(self, 'do_' + cmd)
  55.             except AttributeError:
  56.                 return self.default(line)
  57.             return func(arg)
  58.  
  59.     def default(self, line):
  60.         print '*** Unknown syntax:', line
  61.  
  62.     def do_help(self, arg):
  63.         if arg:
  64.             try:
  65.                 func = getattr(self, 'help_' + arg)
  66.             except:
  67.                 print 'Undefined command: "%s".  Try "help".' % (arg,)
  68.                 return
  69.             func()
  70.         else:
  71.             import newdir
  72.             names = newdir.dir(self.__class__)
  73.             cmds_doc = []
  74.             cmds_undoc = []
  75.             help = {}
  76.             for name in names:
  77.                 if name[:5] == 'help_':
  78.                     help[name[5:]]=1
  79.             for name in names:
  80.                 if name[:3] == 'do_':
  81.                     cmd=name[3:]
  82.                     if help.has_key(cmd):
  83.                         cmds_doc.append(cmd)
  84.                         del help[cmd]
  85.                     else:
  86.                         cmds_undoc.append(cmd)
  87.             print 
  88.             print "Type 'quit' to exit debugger"
  89.             self.print_topics("Documented commands (type help " \
  90.                                   "<topic>):",cmds_doc, 15, 80)
  91.             self.print_topics("Miscellaneous help topics:",
  92.                                   help.keys(), 15, 80)
  93.             self.print_topics("Undocumented commands:", 
  94.                                   cmds_undoc, 15, 80)
  95.  
  96.     def print_topics(self, header, cmds, cmdlen, maxcol):
  97.         if cmds:
  98.             print header;
  99.             print "="*len(header)
  100.             (cmds_per_line,junk)=divmod(maxcol,cmdlen)
  101.             col=cmds_per_line
  102.             for cmd in cmds:
  103.                 if col==0: print
  104.                 print (("%-"+`cmdlen`+"s") % cmd),
  105.                 col = (col+1) % cmds_per_line
  106.             print "\n"
  107.