CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/PythonAnalysis/python/cmscompleter.py

Go to the documentation of this file.
00001 """Word completion for CMS Software.
00002 Based on rlcompleter from Python 2.4.1. Included additional namespace for not loaded modules
00003 Please read license in your lcg external python version
00004 
00005 benedikt.hegner@cern.ch
00006 
00007 """
00008 # TODO: sometimes results are doubled. clean global_matches list!
00009 
00010 import readline
00011 import rlcompleter
00012 import __builtin__
00013 import __main__
00014 
00015 __all__ = ["CMSCompleter"]
00016 
00017 class CMSCompleter(rlcompleter.Completer):
00018     def __init__(self, namespace = None):
00019         
00020         if namespace and not isinstance(namespace, dict):
00021             raise TypeError,'namespace must be a dictionary'
00022 
00023         # Don't bind to namespace quite yet, but flag whether the user wants a
00024         # specific namespace or to use __main__.__dict__. This will allow us
00025         # to bind to __main__.__dict__ at completion time, not now.
00026         if namespace is None:
00027             self.use_main_ns = 1
00028         else:
00029             self.use_main_ns = 0
00030             self.namespace = namespace          
00031         try:
00032             # loading cms namespace
00033             import namespaceDict
00034             self.cmsnamespace = namespaceDict.getNamespaceDict()
00035         except:
00036             print 'Could not load CMS namespace'
00037 
00038  
00039     def global_matches(self, text):
00040         """Compute matches when text is a simple name.
00041 
00042         Return a list of all keywords, built-in functions and names currently
00043         defined in self.namespace and self.cmsnamespace that match.
00044 
00045         """
00046         import keyword
00047         matches = []
00048         n = len(text)
00049         for list in [keyword.kwlist,
00050                      __builtin__.__dict__,
00051                                          self.cmsnamespace]:
00052             for word in list:
00053                 if word[:n] == text and word != "__builtins__":
00054                     matches.append(word)
00055         return matches
00056 
00057 # connect CMSCompleter with readline
00058 readline.set_completer(CMSCompleter().complete)
00059 readline.parse_and_bind('tab: complete')
00060 
00061