CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmscompleter.py
Go to the documentation of this file.
1 """Word completion for CMS Software.
2 Based on rlcompleter from Python 2.4.1. Included additional namespace for not loaded modules
3 Please read license in your lcg external python version
4 
5 benedikt.hegner@cern.ch
6 
7 """
8 # TODO: sometimes results are doubled. clean global_matches list!
9 
10 import readline
11 import rlcompleter
12 import __builtin__
13 import __main__
14 
15 __all__ = ["CMSCompleter"]
16 
17 class CMSCompleter(rlcompleter.Completer):
18  def __init__(self, namespace = None):
19 
20  if namespace and not isinstance(namespace, dict):
21  raise TypeError,'namespace must be a dictionary'
22 
23  # Don't bind to namespace quite yet, but flag whether the user wants a
24  # specific namespace or to use __main__.__dict__. This will allow us
25  # to bind to __main__.__dict__ at completion time, not now.
26  if namespace is None:
27  self.use_main_ns = 1
28  else:
29  self.use_main_ns = 0
30  self.namespace = namespace
31  try:
32  # loading cms namespace
33  import namespaceDict
34  self.cmsnamespace = namespaceDict.getNamespaceDict()
35  except:
36  print 'Could not load CMS namespace'
37 
38 
39  def global_matches(self, text):
40  """Compute matches when text is a simple name.
41 
42  Return a list of all keywords, built-in functions and names currently
43  defined in self.namespace and self.cmsnamespace that match.
44 
45  """
46  import keyword
47  matches = []
48  n = len(text)
49  for list in [keyword.kwlist,
50  __builtin__.__dict__,
51  self.cmsnamespace]:
52  for word in list:
53  if word[:n] == text and word != "__builtins__":
54  matches.append(word)
55  return matches
56 
57 # connect CMSCompleter with readline
58 readline.set_completer(CMSCompleter().complete)
59 readline.parse_and_bind('tab: complete')
60 
61