Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:07

0001 """Word completion for CMS Software.
0002 Based on rlcompleter from Python 2.4.1. Included additional namespace for not loaded modules
0003 Please read license in your lcg external python version
0004 
0005 benedikt.hegner@cern.ch
0006 
0007 """
0008 from __future__ import absolute_import
0009 from __future__ import print_function
0010 # TODO: sometimes results are doubled. clean global_matches list!
0011 
0012 import readline
0013 import rlcompleter
0014 import builtins
0015 import __main__
0016 
0017 __all__ = ["CMSCompleter"]
0018 
0019 class CMSCompleter(rlcompleter.Completer):
0020     def __init__(self, namespace = None):
0021     
0022         if namespace and not isinstance(namespace, dict):
0023             raise TypeError('namespace must be a dictionary')
0024 
0025         # Don't bind to namespace quite yet, but flag whether the user wants a
0026         # specific namespace or to use __main__.__dict__. This will allow us
0027         # to bind to __main__.__dict__ at completion time, not now.
0028         if namespace is None:
0029             self.use_main_ns = 1
0030         else:
0031             self.use_main_ns = 0
0032             self.namespace = namespace      
0033         try:
0034             # loading cms namespace
0035             from . import namespaceDict
0036             self.cmsnamespace = namespaceDict.getNamespaceDict()
0037         except:
0038             print('Could not load CMS namespace')
0039 
0040  
0041     def global_matches(self, text):
0042         """Compute matches when text is a simple name.
0043 
0044         Return a list of all keywords, built-in functions and names currently
0045         defined in self.namespace and self.cmsnamespace that match.
0046 
0047         """
0048         import keyword
0049         matches = []
0050         n = len(text)
0051         for list in [keyword.kwlist,
0052                      builtins.__dict__,
0053                      self.cmsnamespace]:
0054             for word in list:
0055                 if word[:n] == text and word != "__builtins__":
0056                     matches.append(word)
0057         return matches
0058 
0059 # connect CMSCompleter with readline
0060 readline.set_completer(CMSCompleter().complete)
0061 readline.parse_and_bind('tab: complete')
0062 
0063