File: autojump_ipython.py

package info (click to toggle)
autojump 20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 336 kB
  • sloc: python: 482; sh: 363; xml: 81; makefile: 10
file content (36 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# This module was contributed by Mario Pastorelli <pastorelli.mario@gmail.com>
# It is released in the public domain

# This tool provides "j" for ipython
# To use it, copy it in your ~/.ipython directory
# and add the following line to ipy_user_conf.py:
# import autojump_ipython

import os
import subprocess as sub
from IPython.ipapi import get
from IPython.iplib import InteractiveShell

ip = get()

def magic_j(self,parameter_s=''):
    cmd = ['autojump']+parameter_s.split()
    # print 'executing autojump with args %s' % str(cmd)
    newpath=sub.Popen(cmd,stdout=sub.PIPE,shell=False).communicate()[0][:-1] # delete last '\n'
    # print 'Autojump answer: \'%s\'' % newpath
    if newpath:
        ip.magic('cd \'%s\'' % newpath)

def cd_decorator(f):
    def autojump_cd_monitor(self,parameter_s=''):
        f(self,parameter_s)
        sub.call(['autojump','-a',os.getcwd()])
    return autojump_cd_monitor

# Add the new magic function to the class dict and decorate magic_cd:
InteractiveShell.magic_j = magic_j
InteractiveShell.magic_cd = cd_decorator(InteractiveShell.magic_cd)

# And remove the global name to keep global namespace clean.
del magic_j
del cd_decorator