##// END OF EJS Templates
remove unnecessary function call
remove unnecessary function call

File last commit:

r4872:34c10438
r6654:56f06ced
Show More
ipy_rehashdir.py
140 lines | 4.2 KiB | text/x-python | PythonLexer
fperez
Fix win32 line endings.
r281 # -*- coding: utf-8 -*-
""" IPython extension: add %rehashdir magic
Usage:
%rehashdir c:/bin c:/tools
Bernardo B. Marques
remove all trailling spaces
r4872 - Add all executables under c:/bin and c:/tools to alias table, in
fperez
Fix win32 line endings.
r281 order to make them directly executable from any directory.
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 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).
"""
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 from IPython.core import ipapi
ip = ipapi.get()
fperez
Fix win32 line endings.
r281
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 import os,re,fnmatch,sys
fperez
Fix win32 line endings.
r281
vivainio
callable aliases now get _ip as first arg
r833 def selflaunch(ip,line):
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 """ Launch python script with 'this' interpreter
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipykit pylaunchers do .ipy now
r841 e.g. d:\foo\ipykit.exe a.py
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 """
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipykit pylaunchers do .ipy now
r841 tup = line.split(None,1)
if len(tup) == 1:
print "Launching nested ipython session"
os.system(sys.executable)
return
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipykit pylaunchers do .ipy now
r841 cmd = sys.executable + ' ' + tup[1]
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 print ">",cmd
os.system(cmd)
class PyLauncher:
""" Invoke selflanucher on the specified script
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 This is mostly useful for associating with scripts using::
Brian Granger
Continuing a massive refactor of everything.
r2205 _ip.define_alias('foo',PyLauncher('foo_script.py'))
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 """
def __init__(self,script):
self.script = os.path.abspath(script)
vivainio
callable aliases now get _ip as first arg
r833 def __call__(self, ip, line):
vivainio
ipykit pylaunchers do .ipy now
r841 if self.script.endswith('.ipy'):
ip.runlines(open(self.script).read())
else:
vivainio
ipykit: PyLauncher first arg strip
r861 # first word is the script/alias name itself, strip it
tup = line.split(None,1)
if len(tup) == 2:
tail = ' ' + tup[1]
else:
tail = ''
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
ipykit: PyLauncher first arg strip
r861 selflaunch(ip,"py " + self.script + tail)
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801 def __repr__(self):
return 'PyLauncher("%s")' % self.script
vivainio
ipykit pylaunchers do .ipy now
r841
fperez
Fix win32 line endings.
r281 def rehashdir_f(self,arg):
""" Add executables in all specified dirs to alias table
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 Usage:
%rehashdir c:/bin;c:/tools
Bernardo B. Marques
remove all trailling spaces
r4872 - Add all executables under c:/bin and c:/tools to alias table, in
fperez
Fix win32 line endings.
r281 order to make them directly executable from any directory.
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 Without arguments, add all executables in current directory.
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 """
# most of the code copied from Magic.magic_rehashx
def isjunk(fname):
junk = ['*~']
for j in junk:
if fnmatch.fnmatch(fname, j):
return True
return False
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
add mglob
r687 created = []
fperez
Fix win32 line endings.
r281 if not arg:
arg = '.'
path = map(os.path.abspath,arg.split(';'))
Brian Granger
Massive refactoring of of the core....
r2245 alias_table = self.shell.alias_manager.alias_table
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 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:
winext = 'exe|com|bat|py'
vivainio
.py is always executable for purposes of %rehashx and %rehashdir
r380 if 'py' not in winext:
winext += '|py'
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
Fix win32 line endings.
r281 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
Jörgen Stenarson
Search of getcwd and replace with getcwdu. Ignoring core/prompts.py
r4208 savedir = os.getcwdu()
fperez
Fix win32 line endings.
r281 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):
if isexec(ff) and not isjunk(ff):
# each entry in the alias table must be (N,name),
# where N is the number of positional arguments of the
# alias.
vivainio
add mglob
r687 src,tgt = os.path.splitext(ff)[0], os.path.abspath(ff)
Bernardo B. Marques
remove all trailling spaces
r4872 created.append(src)
fperez
Fix win32 line endings.
r281 alias_table[src] = (0,tgt)
else:
for pdir in path:
os.chdir(pdir)
for ff in os.listdir(pdir):
if isexec(ff) and not isjunk(ff):
src, tgt = execre.sub(r'\1',ff), os.path.abspath(ff)
vivainio
Aliases are now lowercase on win32
r649 src = src.lower()
Bernardo B. Marques
remove all trailling spaces
r4872 created.append(src)
fperez
Fix win32 line endings.
r281 alias_table[src] = (0,tgt)
# 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
vivainio
no auto_alias in %redashdir...
r543 # self.shell.init_auto_alias()
fperez
Fix win32 line endings.
r281 finally:
os.chdir(savedir)
vivainio
add mglob
r687 return created
vivainio
ipython_firstrun(ip) entry point for _ip.load, ipykit enhancement: pylaunchers for launching python scripts
r801
Brian Granger
Continuing a massive refactor of everything.
r2205 ip.define_magic("rehashdir",rehashdir_f)