def h5py_attr_completer(context, command): """Compute possible attr matches for nested dict-like objects""" base, attr = re_attr_match.split(command)[1:3] base = base.strip() try: assert '(' not in base except AssertionError: raise ValueError() try: obj = eval(base, context.shell.user_ns) except: return [] attrs = dir(obj) try: attrs = generics.complete_object(obj, attrs) except TryNext: pass try: omit__names = ipget().readline_omit__names except AttributeError: # support <ipython-0.11 omit__names = ipget().options.readline_omit__names if omit__names == 1: attrs = [a for a in attrs if not a.startswith('__')] elif omit__names == 2: attrs = [a for a in attrs if not a.startswith('_')] readline.set_completer_delims(' =') return ["%s.%s" % (base, a) for a in attrs if a[:len(attr)] == attr]
def h5py_attr_completer(context, command): """Compute possible attr matches for nested dict-like objects""" base, attr = re_attr_match.split(command)[1:3] base = base.strip() try: obj = _retrieve_obj(base, context) except: return [] attrs = dir(obj) try: attrs = generics.complete_object(obj, attrs) except TryNext: pass omit__names = None try: # support >=ipython-0.12 omit__names = ipget().Completer.omit__names except AttributeError: pass if omit__names is None: try: # support ipython-0.11 omit__names = ipget().readline_omit__names except AttributeError: pass if omit__names is None: try: # support <ipython-0.11 omit__names = ipget().options.readline_omit__names except AttributeError: omit__names = 0 if omit__names == 1: attrs = [a for a in attrs if not a.startswith("__")] elif omit__names == 2: attrs = [a for a in attrs if not a.startswith("_")] readline.set_completer_delims(" =") return ["%s.%s" % (base, a) for a in attrs if a[: len(attr)] == attr]
def h5py_attr_completer(context, command): """Compute possible attr matches for nested dict-like objects""" base, attr = re_attr_match.split(command)[1:3] base = base.strip() try: obj = _retrieve_obj(base, context) except: return [] attrs = dir(obj) try: attrs = generics.complete_object(obj, attrs) except TryNext: pass omit__names = None try: # support >=ipython-0.12 omit__names = ipget().Completer.omit__names except AttributeError: pass if omit__names is None: try: # support ipython-0.11 omit__names = ipget().readline_omit__names except AttributeError: pass if omit__names is None: try: # support <ipython-0.11 omit__names = ipget().options.readline_omit__names except AttributeError: omit__names = 0 if omit__names == 1: attrs = [a for a in attrs if not a.startswith('__')] elif omit__names == 2: attrs = [a for a in attrs if not a.startswith('_')] readline.set_completer_delims(' =') return ["%s.%s" % (base, a) for a in attrs if a[:len(attr)] == attr]
def trait_completer(self,event): """A custom IPython tab-completer that is traits-aware. It tries to hide the internal traits attributes, and reveal them only when it can reasonably guess that the user really is after one of them. """ #print '\nevent is:',event # dbg symbol_parts = event.symbol.split('.') base = '.'.join(symbol_parts[:-1]) #print 'base:',base # dbg oinfo = self._ofind(base) if not oinfo['found']: raise TryNext obj = oinfo['obj'] # OK, we got the object. See if it's traits, else punt if not looks_like_isinstance(obj, 'HasTraits'): raise TryNext # Defer import until here so as not to require Traits until we get something # that looks like it might be a HasTraits instance. from enthought.traits.api import HasTraits if not isinstance(obj, HasTraits): raise TryNext # it's a traits object, don't show the tr* attributes unless the completion # begins with 'tr' attrs = dir2(obj) # Now, filter out the attributes that start with the user's request attr_start = symbol_parts[-1] if attr_start: attrs = [a for a in attrs if a.startswith(attr_start)] # Let's also respect the user's readline_omit__names setting: omit__names = ipget().options.readline_omit__names if omit__names == 1: attrs = [a for a in attrs if not a.startswith('__')] elif omit__names == 2: attrs = [a for a in attrs if not a.startswith('_')] #print '\nastart:<%r>' % attr_start # dbg if len(attr_start)<COMPLETE_THRESHOLD: attrs = list(set(attrs) - get_trait_names()) # The base of the completion, so we can form the final results list bdot = base+'.' tcomp = [bdot+a for a in attrs] #print 'tcomp:',tcomp return tcomp
def activate(complete_threshold=COMPLETE_THRESHOLD): """Activate the Traits completer. :Keywords: complete_threshold : int The minimum number of letters that a user must type in order to activate completion of traits-private names.""" if not (isinstance(complete_threshold, int) and complete_threshold > 0): e='complete_threshold must be a positive integer, not %r' % \ complete_threshold raise ValueError(e) # Set the module global global COMPLETE_THRESHOLD COMPLETE_THRESHOLD = complete_threshold # Activate the traits aware completer ip = ipget() ip.set_hook('complete_command', trait_completer, re_key='.*')
def activate(complete_threshold = COMPLETE_THRESHOLD): """Activate the Traits completer. :Keywords: complete_threshold : int The minimum number of letters that a user must type in order to activate completion of traits-private names.""" if not (isinstance(complete_threshold,int) and complete_threshold>0): e='complete_threshold must be a positive integer, not %r' % \ complete_threshold raise ValueError(e) # Set the module global global COMPLETE_THRESHOLD COMPLETE_THRESHOLD = complete_threshold # Activate the traits aware completer ip = ipget() ip.set_hook('complete_command', trait_completer, re_key = '.*')
def load_ipython_extension(ip=None): if ip is None: ip = ipget() ip.set_hook('complete_command', h5py_completer, re_key=r"(?:.*\=)?(.+?)\[")
# A sorted list of the names we'll filter out TNL = list(get_trait_names()) TNL.sort() # Make a few objects for testing class TClean(HasTraits): pass class Bunch(object): pass # A clean traits object t = TClean() # A nested object containing t f = Bunch() f.t = t # And a naked new-style object o = object() ip = ipget().IP # A few simplistic tests # Reset the threshold to the default, in case the test is running inside an # instance of ipython that changed it import ipy_traits_completer ipy_traits_completer.COMPLETE_THRESHOLD = 3 assert ip.complete('t.ed') ==[] # For some bizarre reason, these fail on the first time I run them, but not # afterwards. Traits does some really weird stuff at object instantiation # time... ta = ip.complete('t.edi') assert ta == ['t.edit_traits', 't.editable_traits']
def activate(): ip = ipget() ip.set_hook('complete_command', h5py_completer, re_key=r"(?:.*\=)?(.+?)\[")