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

Source Code for Module prompt.tab_completion

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Python module imports. 
 23  from re import match, split 
 24  from rlcompleter import get_class_members 
 25   
 26   
27 -class Tab_completion:
28 - def __init__(self, name_space={}, verbosity=0):
29 """Function for tab completion.""" 30 31 self.name_space = name_space 32 self.verbosity = verbosity
33 34
35 - def create_list(self):
36 """Function to create the dictionary of options for tab completion.""" 37 38 self.list = list(self.name_space.keys()) 39 40 self.options = [] 41 for name in self.list: 42 if match(self.input, name): 43 self.options.append(name)
44 45
46 - def create_sublist(self):
47 """Function to create the dictionary of options for tab completion.""" 48 49 # Split the input. 50 list = split('\.', self.input) 51 if len(list) == 0: 52 return 53 54 # Construct the module and get the corresponding object. 55 module = list[0] 56 for i in range(1, len(list)-1): 57 module = module + '.' + list[i] 58 object = eval(module, self.name_space) 59 60 # Get the object attributes. 61 self.list = dir(object) 62 63 # If the object is a class, get all the class attributes as well. 64 if hasattr(object, '__class__'): 65 self.list.append('__class__') 66 self.list = self.list + get_class_members(object.__class__) 67 68 # Possible completions. 69 self.options = [] 70 for name in self.list: 71 if match(list[-1], name): 72 self.options.append(module + '.' + name) 73 74 if self.verbosity: 75 print("List: " + repr(list)) 76 print("Module: " + repr(module)) 77 print("self.list: " + repr(self.list)) 78 print("self.options: " + repr(self.options))
79 80
81 - def finish(self, input, state):
82 """Return the next possible completion for 'input'""" 83 84 self.input = input 85 self.state = state 86 87 # Create a list of all possible options. 88 # Find a list of options by matching the input with self.list 89 if self.verbosity: 90 print("\nInput: " + repr(self.input)) 91 if not "." in self.input: 92 if self.verbosity: 93 print("Creating list.") 94 self.create_list() 95 if self.verbosity: 96 print("\tOk.") 97 else: 98 if self.verbosity: 99 print("Creating sublist.") 100 self.create_sublist() 101 if self.verbosity: 102 print("\tOk.") 103 104 105 # Return the options if self.options[state] exists, or return None otherwise. 106 if self.verbosity: 107 print("Returning options.") 108 if self.state < len(self.options): 109 return self.options[self.state] 110 else: 111 return None
112