Esempio n. 1
0
def pymvpa_completer(self, event):
    """A custom IPython tab-completer that is collections-aware.
    """

    symbol_parts = event.symbol.split(".")
    base = ".".join(symbol_parts[:-1])

    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 isinstance(obj, col.Collection):
        # print "exiting for %s" % obj
        raise TryNext

    # it's a Collection object, lets add its keys
    attrs = dir2(obj)
    # print "adding ", obj.keys()
    attrs += obj.keys()

    # Let's also respect the user's readline_omit__names setting:
    omit__names = ipget().IP.Completer.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("_")]

    # The base of the completion, so we can form the final results list
    bdot = base + "."

    tcomp = [bdot + a for a in attrs]
    return tcomp
Esempio n. 2
0
def pymvpa_completer(self, event):
    """A custom IPython tab-completer that is collections-aware.
    """

    symbol_parts = event.symbol.split('.')
    base = '.'.join(symbol_parts[:-1])

    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 isinstance(obj, col.Collection):
        #print "exiting for %s" % obj
        raise TryNext

    # it's a Collection object, lets add its keys
    attrs = dir2(obj)
    #print "adding ", obj.keys()
    attrs += list(obj.keys())

    # Let's also respect the user's readline_omit__names setting:
    omit__names = ipget().IP.Completer.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('_')]

    # The base of the completion, so we can form the final results list
    bdot = base+'.'

    tcomp = [bdot+a for a in attrs]
    return tcomp
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 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
Esempio n. 5
0
def _goodies_pre011():
    """Goodies activator for ipython < 0.11
    """
    try:
        if not cfg.getboolean('ipython', 'complete protected', False):
            ipget().IP.Completer.omit__names = 2
    finally:
        pass

    if cfg.getboolean('ipython', 'complete collections attributes', True):
        from mvpa2.support.ipython.ipy_pymvpa_completer \
             import activate as ipy_completer_activate
        ipy_completer_activate()
Esempio n. 6
0
def _goodies_pre011():
    """Goodies activator for ipython < 0.11
    """
    try:
        if not cfg.getboolean('ipython', 'complete protected', False):
            ipget().IP.Completer.omit__names = 2
    finally:
        pass

    if cfg.getboolean('ipython', 'complete collections attributes', True):
        from mvpa2.support.ipython.ipy_pymvpa_completer \
             import activate as ipy_completer_activate
        ipy_completer_activate()
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 isinstance(obj, T.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) - 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
Esempio n. 8
0
def ipy_activate_pymvpa_goodies():
    """Activate PyMVPA additions to IPython

    Currently known goodies (controlled via PyMVPA configuration) are:

    * completions of collections' attributes
    * disabling by default protected attributes of instances in completions
    """
    try:
        if not cfg.getboolean('ipython', 'complete protected', False):
            ipget().IP.Completer.omit__names = 2
    finally:
        pass

    if cfg.getboolean('ipython', 'complete collections attributes', True):
        from mvpa.support.ipython.ipy_pymvpa_completer \
             import activate as ipy_completer_activate
        ipy_completer_activate()
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='.*')
Esempio n. 11
0
def activate():
    """Activate the PyMVPA Collections completer.
    """
    ipget().set_hook('complete_command', pymvpa_completer, re_key = '.*')
Esempio n. 12
0
    # The base of the completion, so we can form the final results list
    bdot = base+'.'

    tcomp = [bdot+a for a in attrs]
    return tcomp

def activate():
    """Activate the PyMVPA Collections completer.
    """
    ipget().set_hook('complete_command', pymvpa_completer, re_key = '.*')


#############################################################################
if __name__ == '__main__':
    # Testing/debugging, can be done only under interactive IPython session
    from mvpa2.datasets.base import dataset_wizard
    t = dataset_wizard([1, 2, 3], targets=1, chunks=2)

    ip = ipget().IP

    assert(not 'targets' in  ip.complete('t.sa.'))
    assert(not 'chunks' in  ip.complete('t.sa.'))

    from .ipy_pymvpa_completer import activate
    activate()
    # A few simplistic tests
    assert ip.complete('t.ed') == []
    assert('targets' in  ip.complete('t.sa.'))
    assert('chunks' in  ip.complete('t.sa.'))
    print('Tests OK')
    # 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']
Esempio n. 14
0
def activate():
    """Activate the Traits completer.
    """
    # Activate the traits aware completer
    ip = ipget()
    ip.set_hook('complete_command', remote_completer, re_key='.*')
Esempio n. 15
0
def activate():
    """Activate the PyMVPA Collections completer.
    """
    ipget().set_hook("complete_command", pymvpa_completer, re_key=".*")