You are not logged in.

#1 2009-02-10 03:28:00

mini-man
Member
Registered: 2009-02-10
Posts: 6

Pacman completers for IPython

First off, hay all, I'm pretty much a brand-new ubuntu convert, although I have used Arch several times in the past on my VM's. smile Actually a Chakra user, but posting this here since I thought it might benefit a few arch users who may happen to use IPython.

As a Debian user primarily, I grew fond of IPython's handy aptitude/apt-get tab completers for installing packages, and I found myself in withdrawal after switching to Arch since I was trying to tab-complete all my package names when installing stuff via pacman. big_smile

So, here's a very quick and dirty completer that uses the very useful custom completer functionality in IPython. It's far from perfect, complete, or possibly useful depending on what end of the leet spectrum you're on, but I'm hoping a few poor folks like me who abuse the tab key will find this somewhat useful. smile

# pacman_search.py
"""Pacman(-frontend) search generator and custom completer for IPython.

To use, import this module from ~/.ipython/ipy_user_conf.py, and somewhere in
main(), after `ip` is defined, add the following:

    ip.set_hook('complete_command', pacman_search.completer, str_key='smth')

Where you would replace 'smth' with pacman, any pacman frontend you use, or an
IPython alias. If you are using something other than pacman, make sure to set
the constant APP before you hook the completer.
"""

__author__ = 'mini-man <mini.mcmidget AT gmail.com>'
__license__ = 'GPLv3'
__version__ = 0, 0, 1

import os
import re


# Set this to a pacman frontend.
APP = 'pacman'

# Matches output from `<app> -Ss <search-term>`:
REGEXES = {
    'pacman': '^(.+?)/(.+?)\s',
    'yaourt': '^.+?\[1;\d+m(.+?)/.+?\[1m(.+?)\s'
}


def memoize(func):
    """Memoizes a generator."""

    def inner(arg):
        if isinstance(arg, list):
            arg = tuple(arg)
        for key, val in inner.cache.iteritems():
            if arg.startswith(key):
                for i in val:
                    yield i
                return
        tmp = []
        for i in func(arg):
            tmp.append(i)
            yield i
        inner.cache[arg] = tmp
    inner.cache = {}
    return inner


@memoize
def search(term):
    """Does a search with the specified term and returns parsed results."""
    out = os.popen('%s -Ss %s' % (APP, term))
    for ln in out:
        m = re.match(REGEXES[APP], ln)
        # Throwing away .group(1) because atm there's no need for it...
        if m and m.group(2).startswith(term):
            yield m.group(2)


def completer(self, event):
    """IPython custom completer for pacman package searching."""
    cmd_param = event.line.split()
    if event.line.endswith(' '):
        cmd_param.append('')

    if cmd_param[0] == 'sudo':
        cmd_param = cmd_param[1:]

    if cmd_param[1].startswith('-S'):
        return list(search(cmd_param[2]))

Last edited by mini-man (2009-02-10 16:34:27)

Offline

#2 2009-02-10 13:53:25

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Pacman completers for IPython

Do you actually use IPython as your main shell? I use it extensively for development, but when I started trying to use it as a shell I found it a bit clunky, especially (ironically) for development -- my python namespace got clogged and if I was testing a project and needed to change a module it was hard to pick up the changes.

Just curious about your experiences using IPython as a shell.

Dusty

Offline

#3 2009-02-10 14:13:29

mini-man
Member
Registered: 2009-02-10
Posts: 6

Re: Pacman completers for IPython

I do use it as my main shell, and I actually reserve a completely clean profile for development which is always open in a screen tab for easy access. Otherwise I can't live without IPython's shell profile, it kicks other shells' collective butts. smile

Offline

Board footer

Powered by FluxBB