Package prompt :: Module tab_completion
[hide private]
[frames] | no frames]

Source Code for Module prompt.tab_completion

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Python module imports. 
 24  from re import match, split 
 25  from rlcompleter import get_class_members 
 26   
 27   
28 -class Tab_completion:
29 - def __init__(self, name_space={}, verbosity=0):
30 """Function for tab completion.""" 31 32 self.name_space = name_space 33 self.verbosity = verbosity
34 35
36 - def create_list(self):
37 """Function to create the dictionary of options for tab completion.""" 38 39 self.list = list(self.name_space.keys()) 40 41 self.options = [] 42 for name in self.list: 43 if match(self.input, name): 44 self.options.append(name)
45 46
47 - def create_sublist(self):
48 """Function to create the dictionary of options for tab completion.""" 49 50 # Split the input. 51 list = split('\.', self.input) 52 if len(list) == 0: 53 return 54 55 # Construct the module and get the corresponding object. 56 module = list[0] 57 for i in xrange(1, len(list)-1): 58 module = module + '.' + list[i] 59 object = eval(module, self.name_space) 60 61 # Get the object attributes. 62 self.list = dir(object) 63 64 # If the object is a class, get all the class attributes as well. 65 if hasattr(object, '__class__'): 66 self.list.append('__class__') 67 self.list = self.list + get_class_members(object.__class__) 68 69 # Possible completions. 70 self.options = [] 71 for name in self.list: 72 if match(list[-1], name): 73 self.options.append(module + '.' + name) 74 75 if self.verbosity: 76 print(("List: " + repr(list))) 77 print(("Module: " + repr(module))) 78 print(("self.list: " + repr(self.list))) 79 print(("self.options: " + repr(self.options)))
80 81
82 - def finish(self, input, state):
83 """Return the next possible completion for 'input'""" 84 85 self.input = input 86 self.state = state 87 88 # Create a list of all possible options. 89 # Find a list of options by matching the input with self.list 90 if self.verbosity: 91 print(("\nInput: " + repr(self.input))) 92 if not "." in self.input: 93 if self.verbosity: 94 print("Creating list.") 95 self.create_list() 96 if self.verbosity: 97 print("\tOk.") 98 else: 99 if self.verbosity: 100 print("Creating sublist.") 101 self.create_sublist() 102 if self.verbosity: 103 print("\tOk.") 104 105 106 # Return the options if self.options[state] exists, or return None otherwise. 107 if self.verbosity: 108 print("Returning options.") 109 if self.state < len(self.options): 110 return self.options[self.state] 111 else: 112 return None
113