diff --git a/IPython/Extensions/ipy_completers.py b/IPython/Extensions/ipy_completers.py index 1347005..18b02a8 100644 --- a/IPython/Extensions/ipy_completers.py +++ b/IPython/Extensions/ipy_completers.py @@ -8,34 +8,60 @@ but the basic idea is to do: ip.set_hook('complete_command', svn_completer, str_key = 'svn') """ - import IPython.ipapi import glob,os,shlex,sys import inspect +from time import time ip = IPython.ipapi.get() +TIMEOUT_STORAGE = 3 #Time in seconds after which the rootmodules will be stored +TIMEOUT_GIVEUP = 20 #Time in seconds after which we give up + def getRootModules(): """ Returns a list containing the names of all the modules available in the folders of the pythonpath. """ modules = [] + if ip.db.has_key('rootmodules'): + return ip.db['rootmodules'] + t = time() + store = False for path in sys.path: - modules += moduleList(path) + modules += moduleList(path) + if time() - t >= TIMEOUT_STORAGE and not store: + store = True + print "\nCaching the list of root modules, please wait!" + print "(This will only be done once - type '%rehashx' to " + \ + "reset cache!)" + print + if time() - t > TIMEOUT_GIVEUP: + print "This is taking too long, we give up." + print + ip.db['rootmodules'] = [] + return [] + modules += sys.builtin_module_names modules = list(set(modules)) if '__init__' in modules: modules.remove('__init__') - return list(set(modules)) + modules = list(set(modules)) + if store: + ip.db['rootmodules'] = modules + return modules def moduleList(path): """ Return the list containing the names of the modules available in the given folder. """ - folder_list = glob.glob(os.path.join(path,'*')) + if os.path.isdir(path): + folder_list = os.listdir(path) + else: + folder_list = [] + #folder_list = glob.glob(os.path.join(path,'*')) folder_list = [path for path in folder_list \ - if (os.path.isdir(path) and os.path.exists(os.path.join(path,'__init__.py')))\ + if os.path.exists(os.path.join(path,'__init__.py'))\ or path[-3:] in ('.py','.so')\ or path[-4:] in ('.pyc','.pyo')] folder_list += folder_list diff --git a/IPython/Magic.py b/IPython/Magic.py index be1d546..b8cd54a 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 2225 2007-04-08 02:48:16Z jdh2358 $""" +$Id: Magic.py 2276 2007-04-26 16:35:02Z vivainio $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -2533,7 +2533,17 @@ Defaulting color scheme to 'NoColor'""" Under Windows, it checks executability as a match agains a '|'-separated string of extensions, stored in the IPython config - variable win_exec_ext. This defaults to 'exe|com|bat'. """ + variable win_exec_ext. This defaults to 'exe|com|bat'. + + This function also resets the root module cache of module completer, + used on slow filesystems. + """ + + + ip = self.api + + # for the benefit of module completer in ipy_completers.py + del ip.db['rootmodules'] path = [os.path.abspath(os.path.expanduser(p)) for p in os.environ['PATH'].split(os.pathsep)] @@ -2583,7 +2593,7 @@ Defaulting color scheme to 'NoColor'""" # 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() - db = self.getapi().db + db = ip.db db['syscmdlist'] = syscmdlist finally: os.chdir(savedir) diff --git a/doc/ChangeLog b/doc/ChangeLog index 4b220e7..c97ea0b 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,11 @@ +2007-04-26 Ville Vainio + + * Extensions/ipy_completers.py: Olivier's module completer now + saves the list of root modules if it takes > 4 secs on the first run. + + * Magic.py (%rehashx): %rehashx now clears the completer cache + + 2007-04-26 Fernando Perez * ipython.el: fix incorrect color scheme, reported by Stefan. @@ -21,8 +29,8 @@ 2007-04-22 Jörgen Stenarson - * Fix bug in iplib.py/safe_execfile when launching ipython with a script like ipython.py foo.py - which raised a IndexError. + * Fix bug in iplib.py/safe_execfile when launching ipython with a script + like ipython.py foo.py which raised a IndexError. 2007-04-21 Ville Vainio