[Python-checkins] CVS: python/dist/src/Lib cmd.py,1.24,1.25 pstats.py,1.18,1.19

Martin v. L?wis loewis@users.sourceforge.net
Sat, 28 Jul 2001 07:44:05 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv15792/Lib

Modified Files:
	cmd.py pstats.py 
Log Message:
Patch #416224: add readline completion to cmd.Cmd.


Index: cmd.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/cmd.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** cmd.py	2001/07/20 18:54:44	1.24
--- cmd.py	2001/07/28 14:44:03	1.25
***************
*** 16,23 ****
--- 16,33 ----
  6. The command '?' is a synonym for `help'.  The command '!' is a synonym
     for `shell', if a do_shell method exists.
+ 7. If completion is enabled, completing commands will be done automatically,
+    and completing of commands args is done by calling complete_foo() with
+    arguments text, line, begidx, endidx.  text is string we are matching
+    against, all returned matches must begin with it.  line is the current
+    input line (lstripped), begidx and endidx are the beginning and end
+    indexes of the text being matched, which could be used to provide 
+    different completion depending upon which position the argument is in.
  
  The `default' method may be overridden to intercept commands for which there
  is no do_ method.
  
+ The `completedefault' method may be overridden to intercept completions for
+ commands that have no complete_ method. 
+ 
  The data member `self.ruler' sets the character used to draw separator lines
  in the help messages.  If empty, no ruler line is drawn.  It defaults to "=".
***************
*** 57,61 ****
      use_rawinput = 1
  
!     def __init__(self): pass
  
      def cmdloop(self, intro=None):
--- 67,78 ----
      use_rawinput = 1
  
!     def __init__(self, completekey='tab'): 
!         if completekey:
!             try:
!                 import readline
!                 readline.set_completer(self.complete)
!                 readline.parse_and_bind(completekey+": complete")
!             except ImportError:
!                 pass
  
      def cmdloop(self, intro=None):
***************
*** 100,107 ****
          pass
  
!     def onecmd(self, line):
          line = line.strip()
          if not line:
!             return self.emptyline()
          elif line[0] == '?':
              line = 'help ' + line[1:]
--- 117,124 ----
          pass
  
!     def parseline(self, line):
          line = line.strip()
          if not line:
!             return None, None, line
          elif line[0] == '?':
              line = 'help ' + line[1:]
***************
*** 110,118 ****
                  line = 'shell ' + line[1:]
              else:
!                 return self.default(line)
!         self.lastcmd = line
          i, n = 0, len(line)
          while i < n and line[i] in self.identchars: i = i+1
          cmd, arg = line[:i], line[i:].strip()
          if cmd == '':
              return self.default(line)
--- 127,143 ----
                  line = 'shell ' + line[1:]
              else:
!                 return None, None, line
          i, n = 0, len(line)
          while i < n and line[i] in self.identchars: i = i+1
          cmd, arg = line[:i], line[i:].strip()
+         return cmd, arg, line
+     
+     def onecmd(self, line):
+         cmd, arg, line = self.parseline(line)
+         if not line:
+             return self.emptyline()
+         if cmd is None:
+             return self.default(line)
+         self.lastcmd = line
          if cmd == '':
              return self.default(line)
***************
*** 131,134 ****
--- 156,212 ----
          print '*** Unknown syntax:', line
  
+     def completedefault(self, *ignored):
+         return []
+ 
+     def completenames(self, text, *ignored):
+         dotext = 'do_'+text
+         return [a[3:] for a in self.get_names() if a.startswith(dotext)]
+ 
+     def complete(self, text, state):
+         """Return the next possible completion for 'text'.
+ 
+         If a command has not been entered, then complete against command list.
+         Otherwise try to call complete_<command> to get list of completions.
+         """
+         if state == 0:
+             import readline
+             origline = readline.get_line_buffer()
+             line = origline.lstrip()
+             stripped = len(origline) - len(line)
+             begidx = readline.get_begidx() - stripped
+             endidx = readline.get_endidx() - stripped
+             if begidx>0:
+                 cmd, args, foo = self.parseline(line)
+                 if cmd == '':
+                     compfunc = self.completedefault
+                 else:
+                     try:
+                         compfunc = getattr(self, 'complete_' + cmd)
+                     except AttributeError:
+                         compfunc = self.completedefault
+             else:
+                 compfunc = self.completenames
+             self.completion_matches = compfunc(text, line, begidx, endidx)
+         try:
+             return self.completion_matches[state]
+         except IndexError:
+             return None
+     
+     def get_names(self):
+         # Inheritance says we have to look in class and
+         # base classes; order is not important.
+         names = []
+         classes = [self.__class__]
+         while classes:
+             aclass = classes[0]
+             if aclass.__bases__:
+                 classes = classes + list(aclass.__bases__)
+             names = names + dir(aclass)
+             del classes[0]
+         return names
+ 
+     def complete_help(self, *args):
+         return self.completenames(*args)
+ 
      def do_help(self, arg):
          if arg:
***************
*** 148,161 ****
              func()
          else:
!             # Inheritance says we have to look in class and
!             # base classes; order is not important.
!             names = []
!             classes = [self.__class__]
!             while classes:
!                 aclass = classes[0]
!                 if aclass.__bases__:
!                     classes = classes + list(aclass.__bases__)
!                 names = names + dir(aclass)
!                 del classes[0]
              cmds_doc = []
              cmds_undoc = []
--- 226,230 ----
              func()
          else:
!             names = self.get_names()
              cmds_doc = []
              cmds_undoc = []

Index: pstats.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/pstats.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** pstats.py	2001/06/07 05:49:05	1.18
--- pstats.py	2001/07/28 14:44:03	1.19
***************
*** 539,542 ****
--- 539,543 ----
      class ProfileBrowser(cmd.Cmd):
          def __init__(self, profile=None):
+             cmd.Cmd.__init__(self)
              self.prompt = "% "
              if profile: