Module tab_completion
[hide private]
[frames] | no frames]

Source Code for Module tab_completion

 1  import __builtin__ 
 2  from re import match 
 3   
 4   
5 -class tab_completion:
6 - def __init__(self, name_space={}, print_flag=0):
7 """Function for tab completion. 8 9 Some of this code is stolen from the python rlcompleter code. 10 """ 11 12 self.name_space = name_space 13 self.print_flag = print_flag
14 15
16 - def finish(self, input, state):
17 "Return the next possible completion for 'text'" 18 19 if self.print_flag: 20 "\n" 21 self.input = input 22 self.state = state 23 24 # Create a list of all possible options. 25 # Find a list of options by matching the input with self.list 26 if not "." in self.input: 27 if self.print_flag: 28 print "Creating list." 29 self.create_list() 30 if self.print_flag: 31 print "\tOk." 32 else: 33 if self.print_flag: 34 print "Creating sublist." 35 self.create_sublist() 36 if self.print_flag: 37 print "\tOk." 38 39 40 # Return the options if self.options[state] exists, or return None otherwise. 41 if self.print_flag: 42 print "Returning options." 43 if self.state < len(self.options): 44 return self.options[self.state] 45 else: 46 return None
47 48
49 - def create_sublist(self):
50 "Function to create the dictionary of options for tab completion." 51 52 string = match(r"(\w+(\.\w+)*)\.(\w*)", self.input) 53 if not string: 54 return 55 module, text = string.group(1,3) 56 object = eval(module, self.name_space) 57 self.list = dir(object) 58 if hasattr(object, '__class__'): 59 self.list.append('__class__') 60 self.list = self.list + self.get_class_members(object.__class__) 61 62 self.options = [] 63 for name in self.list: 64 if match(text, name) and name != "__builtins__": 65 self.options.append(module + '.' + name) 66 67 if self.print_flag: 68 print "self.list: " + `self.list` 69 print "self.options: " + `self.options`
70 71
72 - def create_list(self):
73 "Function to create the dictionary of options for tab completion." 74 75 self.list = self.name_space.keys() 76 for key in __builtin__.__dict__.keys(): 77 if not key in self.list: 78 self.list.append(key) 79 80 self.options = [] 81 for name in self.list: 82 if match(self.input, name) and name != "__builtins__": 83 self.options.append(name)
84 85
86 - def get_class_members(self, temp_class):
87 list = dir(temp_class) 88 if hasattr(temp_class, '__bases__'): 89 for base in temp_class.__bases__: 90 list = list + self.get_class_members(base) 91 return list
92