##// END OF EJS Templates
Jorgen's %clean array
Jorgen's %clean array

File last commit:

r235:d4cd84b8
r245:05403116
Show More
ext_rehashdir.py
102 lines | 3.3 KiB | text/x-python | PythonLexer
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 # -*- coding: utf-8 -*-
""" IPython extension: add %rehashdir magic
Usage:
vivainio
%rehashdir mods
r128 %rehashdir c:/bin c:/tools
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 - Add all executables under c:/bin and c:/tools to alias table, in
order to make them directly executable from any directory.
This also serves as an example on how to extend ipython
with new magic functions.
Unlike rest of ipython, this requires Python 2.4 (optional
extensions are allowed to do that).
To install, add
vivainio
%rehashdir mods
r128 "import_mod ext_rehashdir"
vivainio
Created rehash_dir extensions that introduces new magic,...
r126
To your ipythonrc or just execute "import rehash_dir" in ipython
prompt.
$Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $
"""
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 import IPython.ipapi
ip = IPython.ipapi.get()
vivainio
Created rehash_dir extensions that introduces new magic,...
r126
vivainio
%rehashdir mods
r128 import os,re,fnmatch
vivainio
Created rehash_dir extensions that introduces new magic,...
r126
def rehashdir_f(self,arg):
""" Add executables in all specified dirs to alias table
Usage:
vivainio
Quote aliases w/ spaces in target name...
r235 %rehashdir c:/bin;c:/tools
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 - Add all executables under c:/bin and c:/tools to alias table, in
order to make them directly executable from any directory.
vivainio
%rehashdir mods
r128
Without arguments, add all executables in current directory.
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 """
# most of the code copied from Magic.magic_rehashx
vivainio
%rehashdir mods
r128
def isjunk(fname):
junk = ['*~']
for j in junk:
if fnmatch.fnmatch(fname, j):
return True
return False
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 if not arg:
arg = '.'
vivainio
Quote aliases w/ spaces in target name...
r235 path = map(os.path.abspath,arg.split(';'))
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 alias_table = self.shell.alias_table
if os.name == 'posix':
isexec = lambda fname:os.path.isfile(fname) and \
os.access(fname,os.X_OK)
else:
try:
winext = os.environ['pathext'].replace(';','|').replace('.','')
except KeyError:
vivainio
%rehashdir mods
r128 winext = 'exe|com|bat|py'
vivainio
Created rehash_dir extensions that introduces new magic,...
r126
execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
savedir = os.getcwd()
try:
# write the whole loop for posix/Windows so we don't have an if in
# the innermost part
if os.name == 'posix':
for pdir in path:
os.chdir(pdir)
for ff in os.listdir(pdir):
vivainio
%rehashdir mods
r128 if isexec(ff) and not isjunk(ff):
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 # each entry in the alias table must be (N,name),
# where N is the number of positional arguments of the
# alias.
vivainio
%rehashdir mods
r128 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
print "Aliasing:",src,"->",tgt
alias_table[src] = (0,tgt)
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 else:
for pdir in path:
os.chdir(pdir)
for ff in os.listdir(pdir):
vivainio
%rehashdir mods
r128 if isexec(ff) and not isjunk(ff):
src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
print "Aliasing:",src,"->",tgt
alias_table[src] = (0,tgt)
vivainio
Created rehash_dir extensions that introduces new magic,...
r126 # Make sure the alias table doesn't contain keywords or builtins
self.shell.alias_table_validate()
# Call again init_auto_alias() so we get 'rm -i' and other
# modified aliases since %rehashx will probably clobber them
self.shell.init_auto_alias()
finally:
vivainio
%rehashdir mods
r128 os.chdir(savedir)
vivainio
ipapi rehaul, moved api methods to class IPApi. Usage didn't change...
r146 ip.expose_magic("rehashdir",rehashdir_f)