test
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 from __future__ import absolute_import
9 # TODO: sometimes results are doubled. clean global_matches list!
10 
11 import readline
12 import rlcompleter
13 import __builtin__
14 import __main__
15 
16 __all__ = ["CMSCompleter"]
17 
18 class CMSCompleter(rlcompleter.Completer):
19  def __init__(self, namespace = None):
20 
21  if namespace and not isinstance(namespace, dict):
22  raise TypeError('namespace must be a dictionary')
23 
24  # Don't bind to namespace quite yet, but flag whether the user wants a
25  # specific namespace or to use __main__.__dict__. This will allow us
26  # to bind to __main__.__dict__ at completion time, not now.
27  if namespace is None:
28  self.use_main_ns = 1
29  else:
30  self.use_main_ns = 0
31  self.namespace = namespace
32  try:
33  # loading cms namespace
34  from . import namespaceDict
35  self.cmsnamespace = namespaceDict.getNamespaceDict()
36  except:
37  print 'Could not load CMS namespace'
38 
39 
40  def global_matches(self, text):
41  """Compute matches when text is a simple name.
42 
43  Return a list of all keywords, built-in functions and names currently
44  defined in self.namespace and self.cmsnamespace that match.
45 
46  """
47  import keyword
48  matches = []
49  n = len(text)
50  for list in [keyword.kwlist,
51  __builtin__.__dict__,
52  self.cmsnamespace]:
53  for word in list:
54  if word[:n] == text and word != "__builtins__":
55  matches.append(word)
56  return matches
57 
58 # connect CMSCompleter with readline
59 readline.set_completer(CMSCompleter().complete)
60 readline.parse_and_bind('tab: complete')
61 
62